Wednesday, February 11, 2009

Doubly Linked List

A.Concept:
Doubly Linked List is the linked list redefined so that the node in list has two reference fields, one to the successor and one to the predecessor.Method for processing doubly linked list are sligthly more comlpicated than singly linked counterparts because there is one more reference field to be maintained. Only two methods are discussed: a method to inserta node at hte end of a doubly linked list and a method to remove a node from the end.
public class DoublyLink
{
public int whie;
public DoublyLink next, prev;
public DoublyLink(int a)
{
this(a, null, null);
}
public DoublyLink(int a, DoublyLInk n, DoublyLink p)
{
whie=a;
next=n;
prev=p;
}
}

public class DoublyLinkList{
private DoublyLink head, tail;
public DoublyLinkList()
{
head=null=null;
}
public boolean isEmpty()
{
return head==null;
}
public void addToTail(int a)
{
if(!isEmpty()) {
tail=new DoublyLink(a, null, tail);
tail.prev.next=tail;
}
else
head=tail=new DoublyLink(a);
}
public int removeFromTail(){
int a=tail.whie;
if(head==tail) //if only node in the list;
head=tail=null;
else{ //if more than one node in the list;
tail=tail.prev;
tail.next=null;
}
return a;
}
System.out.println(" ");
}
}


No comments:

Post a Comment