Tuesday, September 8, 2009

Java Structure, Java Doule LinkList

public class DoubleNode {
DoubleNode pre;
Object obj;
DoubleNode post;
}


public class DoubleLinkList {
DoubleNode linkList;
public DoubleLinkList(){
linkList = null;
}
public void insert(Object obj){
DoubleNode temp = new DoubleNode();
temp.pre = null;
temp.obj = obj;
temp.post = null;
if(linkList == null){
linkList = temp;
}
else{
DoubleNode curr = new DoubleNode();
curr = linkList;
while(curr.post!=null){
curr=curr.post;
}
temp.pre = curr;
curr.post=temp;
}
}
public void insertFirst(Object obj){
DoubleNode temp = new DoubleNode();
temp.pre = null;
temp.obj = obj;
temp.post = null;
if(linkList == null){
linkList = temp;
}
else{
temp.post = linkList;
linkList = temp;
}
}
public void remove(){
if(linkList == null){
System.out.println("Link List does not exist");
}else{
DoubleNode curr = new DoubleNode();
curr = linkList;
while(curr.post!=null){
curr = curr.post;
}
System.out.println("Deleting Element :: "+curr.obj.toString());
if(curr == linkList){linkList = null;}
else{curr.pre.post = null;}
}
}
public void show(){
DoubleNode curr = new DoubleNode();
curr = linkList;
while(curr!=null){
System.out.println("Data :: "+curr.obj.toString());
curr = curr.post;
}
}
public static void main(String[] args) {
DoubleLinkList doubleLinkList = new DoubleLinkList();
doubleLinkList.insert("Binod");
doubleLinkList.insert("Suman");
doubleLinkList.insert("IGNOU");
doubleLinkList.insertFirst("India");
doubleLinkList.insert("SATYAM");
doubleLinkList.show();
doubleLinkList.remove();
doubleLinkList.remove();
doubleLinkList.show();
}
}

No comments:

Post a Comment

Please put your feedback or your any question related to this blog. Thanks.