Professional Documents
Culture Documents
first last
23 45 12 34 null
Double ended list (firstlast list)
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
class Dlink
{
public int data;
public Dlink next;
public Dlink previous;
//constructor
// displayLink() method
}
Traversal methods.
dLink current=last;
while (current!=null)
{
current.displayLink();
current=current.previous;
}
Insertion methods
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()