/**
*
* @author IZAN
*/
public class SingelLinkedList {
private int data;
private SingelLinkedList next;
public SingelLinkedList(){
data = 0;
next = null;
}
public SingelLinkedList(int value){
data = value;
next = null;
}
public SingelLinkedList InsertNext(int value){
SingelLinkedList node = new SingelLinkedList(value);
if(this.next == null){
node.next = null;
this.next = node;
}
else{
SingelLinkedList temp = this.next;
node.next = temp;
this.next = node;
}
return node;
}
public int DeleteNext(){
if(next == null)
return 0;
SingelLinkedList node = this.next;
this.next = this.next.next;
node = null;
return 1;
}
public void Traverse(SingelLinkedList node){
if(node == null)
node = this;
System.out.println("Traversing in Forward Direction");
while(node != null)
{
System.out.println(node.data);
node = node.next;
}
}
public static void main(String[] args){
SingelLinkedList node1 = new SingelLinkedList(1);
SingelLinkedList node2 = node1.InsertNext(2);
SingelLinkedList node3 = node2.InsertNext(3);
SingelLinkedList node4 = node3.InsertNext(4);
SingelLinkedList node5 = node4.InsertNext(5);
node1.Traverse(null);
node3.DeleteNext();
node2.Traverse(null);
}
}
No comments:
Post a Comment