Thursday, February 12, 2009

//This is the implementation of doubly linked list

public class IntDLLNode{
public int info;
public IntDLLNode next,prev;
public IntDLLNode(int el){
this(el,null,null);
}
public IntDLLNode(int el,IntDLLNode n,IntDLLNode p){
info=el;next=n;prev=p;
}
}

// This is the class of IntDLList

public class IntDLList{
private IntDLLNode head,tail;
public IntDLList(){
head=tail=null;
}
public boolean isEmpty(){
return head==null;
}
public void addToTail(int el){
if(!isEmpty()){
tail=new IntDLLNode(el,null,tail);
tail.prev.next=tail;
}
else head=tail=new IntDLLNode(el);
}
public int removeFromTail(){
int el=tail.info;
if(head==tail)//if only one node in the list;
head=tail=null;
else{ // if more than one node in the list;
tail=tail.prev;
tail.next=null;
}
return el;
}
}
[Data Structures and algorithms in Java 2nd Edition by: Adam Drozdek]

No comments:

Post a Comment