From a0aa49e1ddf9fc991a9a8046333457b7e98ab488 Mon Sep 17 00:00:00 2001 From: GauravWalia19 Date: Thu, 10 Jan 2019 20:26:20 +0530 Subject: [PATCH] added circular linked list variant1 --- .../CIRCULAR/circular/CircularLinkedList.java | 111 -------- .../{circular => variant1}/CLLNode.java | 0 .../CIRCULAR/variant1/CircularLinkedList.java | 248 ++++++++++++++++++ .../CIRCULAR/{circular => variant1}/Main.java | 5 + Java/README.md | 1 + datastructures.md | 4 +- docs/complexity.md | 25 +- 7 files changed, 278 insertions(+), 116 deletions(-) delete mode 100644 Java/Data-Structures/LISTS/CIRCULAR/circular/CircularLinkedList.java rename Java/Data-Structures/LISTS/CIRCULAR/{circular => variant1}/CLLNode.java (100%) create mode 100644 Java/Data-Structures/LISTS/CIRCULAR/variant1/CircularLinkedList.java rename Java/Data-Structures/LISTS/CIRCULAR/{circular => variant1}/Main.java (82%) diff --git a/Java/Data-Structures/LISTS/CIRCULAR/circular/CircularLinkedList.java b/Java/Data-Structures/LISTS/CIRCULAR/circular/CircularLinkedList.java deleted file mode 100644 index 75c1326..0000000 --- a/Java/Data-Structures/LISTS/CIRCULAR/circular/CircularLinkedList.java +++ /dev/null @@ -1,111 +0,0 @@ -public class CircularLinkedList -{ - private CLLNode head; - private int length; - - //constructor - public CircularLinkedList() - { - head=null; - length=0; - } - public void addToHead(int data) - { - CLLNode newNode = new CLLNode(data); - if(head==null) //if list is empty - { - head=newNode; - head.setNext(head); - } - else - { - newNode.setNext(head); - CLLNode current=head.getNext(); - while(current.getNext()!=head) - { - current=current.getNext(); - } - current.setNext(newNode); - head=newNode; - } - length++; - } - public void addToTail(int data) - { - CLLNode newNode = new CLLNode(data); - if(head==null) //if empty - { - head=newNode; - head.setNext(head); - } - else - { - newNode.setNext(head); - CLLNode current=head.getNext(); - while(current.getNext()!=head) - { - current=current.getNext(); - } - current.setNext(newNode); - } - length++; - } - public int removeFromHead() - { - if(head==null) - { - return -1; - } - CLLNode temp = head; - CLLNode current=head.getNext(); - while(current.getNext()!=head) - { - current=current.getNext(); - } - head=head.getNext(); - current.setNext(head); - temp.setNext(null); - length--; - return temp.getData(); - } - public int removeFromTail() - { - if(head==null) - { - return -1; - } - CLLNode current=head.getNext(); - CLLNode prev=head.getNext(); - while(current.getNext()!=head) - { - prev=current; - current=current.getNext(); - } - current.setNext(null); - prev.setNext(head); - length--; - return current.getData(); - } - public int length() - { - return length; - } - public void PrintCircularListData() - { - if(head==null) - { - System.out.println("null"); - } - else - { - System.out.print(head.getData()+"->"); - CLLNode current = head.getNext(); - while(current!=head) - { - System.out.print(current.getData()+"->"); - current=current.getNext(); - } - System.out.println("HEAD"); - } - } -} \ No newline at end of file diff --git a/Java/Data-Structures/LISTS/CIRCULAR/circular/CLLNode.java b/Java/Data-Structures/LISTS/CIRCULAR/variant1/CLLNode.java similarity index 100% rename from Java/Data-Structures/LISTS/CIRCULAR/circular/CLLNode.java rename to Java/Data-Structures/LISTS/CIRCULAR/variant1/CLLNode.java diff --git a/Java/Data-Structures/LISTS/CIRCULAR/variant1/CircularLinkedList.java b/Java/Data-Structures/LISTS/CIRCULAR/variant1/CircularLinkedList.java new file mode 100644 index 0000000..bd04f41 --- /dev/null +++ b/Java/Data-Structures/LISTS/CIRCULAR/variant1/CircularLinkedList.java @@ -0,0 +1,248 @@ +public class CircularLinkedList +{ + private CLLNode head; + private int length; + + //constructor + public CircularLinkedList() + { + head=null; + length=0; + } + + /** + * INSERTION + * 1. AT HEAD + * 2. AT TAIL + * 3. AT MIDDLE + **/ + /** + * addToHead() + * it will insert node at head + * @param int for data + * @return void + **/ + public void addToHead(int data) + { + CLLNode newNode = new CLLNode(data); + if(head==null) //if list is empty + { + head=newNode; + head.setNext(head); + } + else + { + newNode.setNext(head); + CLLNode current=head.getNext(); + while(current.getNext()!=head) + { + current=current.getNext(); + } + current.setNext(newNode); + head=newNode; + } + length++; + } + + /** + * addToTail() + * it will add the node to the tail + * @param int for data + * @return void + **/ + public void addToTail(int data) + { + CLLNode newNode = new CLLNode(data); + if(head==null) //if empty + { + head=newNode; + head.setNext(head); + } + else + { + newNode.setNext(head); + CLLNode current=head.getNext(); + while(current.getNext()!=head) + { + current=current.getNext(); + } + current.setNext(newNode); + } + length++; + } + + /** + * addToMiddle() + * it will add node to middle + * @param void + * @return void + **/ + public void addToMiddle(int data,int position) + { + CLLNode newNode = new CLLNode(data); + if(position==0) + { + if(head==null) + { + head=newNode; + } + else + { + newNode.setNext(head); + CLLNode current = head.getNext(); + while(current.getNext()!=head) + { + current=current.getNext(); + } + head=newNode; + current.setNext(head); + } + } + else + { + //insert except head + CLLNode current=head; + CLLNode prev=head; + int it=0; + while(current!=null) + { + if(it==position) + { + break; + } + prev=current; + current=current.getNext(); + it++; + } + prev.setNext(newNode); + newNode.setNext(current); + } + } + + /** + * DELETION + * 1. FROM HEAD + * 2. FROM TAIL + * 3. FROM MIDDLE + **/ + + /** + * removeFromHead() + * it will remove node from head + * @param void + * @return int for removed node + **/ + public int removeFromHead() + { + if(head==null) + { + return -1; + } + CLLNode temp = head; + CLLNode current=head.getNext(); + while(current.getNext()!=head) + { + current=current.getNext(); + } + head=head.getNext(); + current.setNext(head); + temp.setNext(null); + length--; + return temp.getData(); + } + + /** + * removeFromTail() + * it will remove node from the tail + * @param void + * @return int for removed node + **/ + public int removeFromTail() + { + if(head==null) + { + return -1; + } + CLLNode current=head.getNext(); + CLLNode prev=head.getNext(); + while(current.getNext()!=head) + { + prev=current; + current=current.getNext(); + } + current.setNext(null); + prev.setNext(head); + length--; + return current.getData(); + } + + /** + * removeFromMiddle() + * @param void + * @return the removed node data + **/ + public int removeFromMiddle(int position) + { + if(position==0) + { + //delete from head + CLLNode current=head; + while(current.getNext()!=head) + { + current=current.getNext(); + } + CLLNode temp=head; + head=head.getNext(); + current.setNext(head); + return temp.getData(); + } + else + { + //delete from middle + CLLNode prev = head; + CLLNode current= head; + int it=0; + while(current!=null) + { + if(it==position) + { + break; + } + prev=current; + current=current.getNext(); + it++; + } + CLLNode nextNode = current.getNext(); + prev.setNext(nextNode); + current.setNext(null); + return current.getData(); + } + } + /** + * length() + * @param void + * @return the length of the list + **/ + public int length() + { + return length; + } + public void PrintCircularListData() + { + if(head==null) + { + System.out.println("null"); + } + else + { + System.out.print(head.getData()+"->"); + CLLNode current = head.getNext(); + while(current!=head) + { + System.out.print(current.getData()+"->"); + current=current.getNext(); + } + System.out.println("HEAD"); + } + } +} \ No newline at end of file diff --git a/Java/Data-Structures/LISTS/CIRCULAR/circular/Main.java b/Java/Data-Structures/LISTS/CIRCULAR/variant1/Main.java similarity index 82% rename from Java/Data-Structures/LISTS/CIRCULAR/circular/Main.java rename to Java/Data-Structures/LISTS/CIRCULAR/variant1/Main.java index 05308fa..b896116 100644 --- a/Java/Data-Structures/LISTS/CIRCULAR/circular/Main.java +++ b/Java/Data-Structures/LISTS/CIRCULAR/variant1/Main.java @@ -9,10 +9,15 @@ public static void main(String[] args) clist.addToHead(4); clist.PrintCircularListData(); + clist.addToMiddle(99,10); + + clist.PrintCircularListData(); + clist.addToTail(5); clist.addToTail(6); clist.addToTail(7); clist.addToTail(8); + clist.removeFromMiddle(1); clist.PrintCircularListData(); clist.removeFromHead(); diff --git a/Java/README.md b/Java/README.md index 2b7c420..cb55b6c 100644 --- a/Java/README.md +++ b/Java/README.md @@ -31,6 +31,7 @@ * [Double Linked List using object data](Data-Structures/LISTS/DOUBLE/Double-Using-Object/DoubleLinkedList.java) * CIRCULAR * [Circular Linked List (using tail node)](Data-Structures/LISTS/CIRCULAR/STANDARD/CircularLinkedList.java) + * [Circular Linked List (using head node)](Data-Structures/LISTS/CIRCULAR/variant1/CircularLinkedList.java) * MEMORY EFFICIENT DOUBLE LINKED LIST * UNROLLED LINKED LIST * SKIP LIST diff --git a/datastructures.md b/datastructures.md index 2ebb35e..ae83f4b 100644 --- a/datastructures.md +++ b/datastructures.md @@ -123,7 +123,9 @@ Indexer for Data Structures Lover * blog * docs * implementation - * [JAVA](Java/Data-Structures/LISTS/CIRCULAR/STANDARD/CircularLinkedList.java) + * JAVA + * [STANDARD](Java/Data-Structures/LISTS/CIRCULAR/STANDARD/CircularLinkedList.java) + * [Variant1: using head node](Java/Data-Structures/LISTS/CIRCULAR/variant1/CircularLinkedList.java) * complexity ### INBUILT LISTS diff --git a/docs/complexity.md b/docs/complexity.md index 07dd918..92a11f5 100644 --- a/docs/complexity.md +++ b/docs/complexity.md @@ -76,8 +76,8 @@ This page contains the complexities of different algorithms in this repository. * [DOUBLE LINKED LIST](#double-linked-list-having-headtail-and-nextprev) * [HYBRID DOUBLE LINKED LIST](#hybrid-double-linked-list-having-headprev-and-next) * CIRCULAR - * [SINGLE CIRCULAR LINKED LIST](#circular-linked-list-having-tail/head-next-and-loop) - * DOUBLE CIRCULAR LINKED LIST + * [STANDARD CIRCULAR LINKED LIST](#standard-circular-linked-list) + * [CIRCULAR LINKED LIST VARIANT 1](#circular-linked-list-variant-1) * [MEMORY EFFICIENT DOUBLE LINKED LIST](#memory-efficient-(XOR)-double-linked-list) * [UNROLLED LINKED LIST](#unrolled-linked-list) * [SKIP LIST](#skip-list) @@ -398,7 +398,9 @@ SNo. | Methods | Order of complexity O(n) | Type of Complexity #### CIRCULAR -##### SINGLE CIRCULAR LINKED LIST (having tail/head ,next and loop) +##### STANDARD CIRCULAR LINKED LIST + +(having tail ,next and loop) SNo. | Operations | Order and Type of Time Complexity O(n) | Order and Type of Space Complexity ---- | ---------- | -------------------------------------- | ---------------------------------- @@ -411,7 +413,22 @@ SNo. | Operations | Order and Type of Time Complexity O(n) | Order and Type of S 6 | Linear search | O(n) -- Linear | O(1) -- Constant 7 | Clear list | O(1) -- Constant | O(1) -- Constant -##### DOUBLE CIRCULAR LINKED LIST (having tail/head,next,prev,loops) +##### CIRCULAR LINKED LIST VARIANT 1 + +(having head ,next and loop) + +SNo. | Operations | Order and Type of Time Complexity O(n) | Order and Type of Space Complexity + ---- | ---------- | -------------------------------------- | ---------------------------------- + 1 | Traversing a list (from left to right to head) | O(n) -- Linear | O(1) -- Constant + 2 | Insertion at Head | O(n) -- Linear | O(1) -- Constant + 3 | Insertion at Tail | O(n) -- Linear | O(1) -- Constant + 4 | Insertion at Middle | O(n) -- Linear | O(1) -- Constant + 4 | Deletion from Head | O(n) -- Linear | O(1) -- Constant + 5 | Deletion from Tail | O(n) -- Linear | O(1) -- Constant + 6 | Deletion from middle | O(n) -- Linear | O(1) -- Constant + 7 | Linear search | O(n) -- Linear | O(1) -- Constant + 8 | Clear list | O(1) -- Constant | O(1) -- Constant + 9 | Length of list | O(1) -- Constant | O(1) -- Constant #### MEMORY EFFICIENT (XOR) DOUBLE LINKED LIST