You are on page 1of 18

1.

First Last Lists(Double


ended lists)

2.Doubly linked lists


Double ended lists(first last lists)
● A double ended list has a reference to the last link as well as to the first.
● A reference to the last link permits you to insert a new node directly at the end of the list.
● Accessing to both ends of the list makes it suitable for situations like implementing a
Queue

first last

23 45 12 34 null
Double ended list (firstlast list)

Class Link is same as Singly linked lists.

class FirstLastList
{
private Link first;
private Link last;
public First LastList()
{
first=null;
last=null;
}
public boolean isEmpty()
{
return (first==null);
}
Inserting in first position

public insertFirst(int d)
{
Link nl=new Link(d);
if (isEmpty())
{
first=nl;
last=nl;
}
else
{
nl.next=first;
first=nl;
}
}
Inserting in last position

public void insertLast(int d)


{
Link nl=new Link(d);
if (isEmpty())
{
first=nl;
last=nl;
}
else
{
last.next=nl;
last=nl;
}
}
Deleting the first link- deleteFirst()

public int deleteFirst()


{
int temp=first.data;
if(first.next==null)
{ first=null;
last=null;
}
else
first=first.next;
return temp;
}

Add displayList() method also.


Why not deleteLast() ??
Linkedlist Efficiency

● Insertion , deletion at the beginning of a list - Very fast.


● find(),insert() or delete() next to a specific item - requires search through, on
the avg half the items through the list.- For Searching, same time as in
array.But, insertion, deletion are faster.
Doubly Linked Lists
Node Structure

Doubly linked lists


● Solves the problem with singly linked lists, that it can’t traverse backward along
the list.
● The doubly linked lists provide backward traversal facility.
● Each link has TWO references instead of one to other links. One is pointing to the
next link (called next) and the other pointing to the previous link (called previous)
Single node of DLL- Implementation

class Dlink
{
public int data;
public Dlink next;
public Dlink previous;

//constructor
// displayLink() method
}
Traversal methods.

1. displayForward()- Same as Singly linked lists.


2. displayBackward() - Starts at the last element in the list and proceeds towards the
start of the list, going through each element’s previous field.
displayBackward()

dLink current=last;
while (current!=null)
{
current.displayLink();
current=current.previous;
}
Insertion methods

1. insertFirst()- Insert at the first position of the list.


2. insertLast() - Insert at the last position of the list.
3. insertAfter() - Inserts the new element following an element with a specified key.
insertFirst()

nl=new dLink(35);
if (isEmpty())
{ first=nl;
last=nl;
}
else
{
nl.next=first;
first.previous=nl;
first=nl;
}
insertAfter()

●First, the link with the specified key value must be found.- same as find() method.
●If we are not at the end of the list, TWO connections must be made between the newlink
and the subsequent link in the list, and TWO more between the current link and the new
one.
●If new link to be inserted at the end of the list,then current’s next field must be set to the
new link and last must be pointed to new link.
insertAfter() method

if (current==last)
{
last.next=nl;
nl.previous=last
last=nl;
}
else
{
nl.next=current.next;
current.next.previous=nl;
nl.previous=current;
current.next=nl;
}
Deletion routines
1. deleteFirst()
2. deleteLast()
3. deletekey()

You might also like