As I said in the first Linked List post there is a thing called Circular Singly Linked List. So what this do is that its like a Singly Linked List but at the tail it directed back to the Head. So it takes a loop form.
Like the Singly linked List I did the implementation in two classes. one to make node and other one to initiate.
NodeClass
The next is the implementation. I did only he insert at head and tail.
Like the Singly linked List I did the implementation in two classes. one to make node and other one to initiate.
NodeClass
1 2 3 4 5 6 7 8 9 10 11 | public class nodeClass { public int info; //contains the information part (Data) public nodeClass next; // The next node public nodeClass(int i){ //constructor to assign this(i,null); } public nodeClass(int x, nodeClass n){// assign values to info and next node info = x; next = n; } } |
The next is the implementation. I did only he insert at head and tail.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | public class CLinkedList { protected NodeClass tail,head; public CLinkedList(){ tail = head = null; } public boolean isEmmpty(){ return head == null; } public void InsertAtHead(int x){ if(isEmmpty()){ tail = head = new NodeClass(x,head); }else{ head = new NodeClass(x,head); tail.next = head; } } public void InsertAtTail(int x){ if(isEmmpty()){ head = tail = new NodeClass(x); tail.next = head; }else{ tail.next = new NodeClass(x,head); tail = tail.next; tail.next = head; } } public static void main(String[] args) { CLinkedList list = new CLinkedList(); list.InsertAtHead(23); list.InsertAtHead(34); list.InsertAtHead(67); list.InsertAtTail(90); list.InsertAtHead(24); list.InsertAtTail(100); System.out.println("tail :"+list.tail.info); System.out.println("Head :"+list.head.info); System.out.println("Head.next :"+list.head.next.info); System.out.println("Tail.next :"+list.tail.next.info); System.out.println("head.next.next :"+list.head.next.next.info); System.out.println("head.next.next.next :"+list.head.next.next.next.info); } } } |
The Linked List after all Insertions will be like
(24 -->67-->34-->23-->90-->100)