|
| 1 | +# Circular Linked List |
| 2 | + |
| 3 | +Circular Linked List is an end-connected data structure made of Nodes. Similar to the linear and doubly linked list, each node is composed of a variable ```data``` where its content is stored and a pointer to the next Node on the list. |
| 4 | +The Linked List has a ```pointer``` to the adjacent elements but the last node is connected towards the head node i.e the first node itself, thus forming a circular shape. |
| 5 | + |
| 6 | +### Advantages over Arrays & Linear Linked List & Doubly Linked List |
| 7 | + |
| 8 | +- Any node can be a starting point |
| 9 | +- Useful for implementation of queue |
| 10 | +- Circular lists are useful in applications to repeatedly go around the list |
| 11 | +- Circular Doubly Linked Lists are used for the implementation of advanced data structures like Fibonacci Heap. |
| 12 | +- The size of a linked list is not fixed (dynamic size) |
| 13 | +- Deleting and adding an element is not expensive compared to an array |
| 14 | + |
| 15 | +### Drawbacks |
| 16 | + |
| 17 | +- Circular lists are complex as compared to singly linked lists. |
| 18 | +- Reversing of circular list is a complex as compared to singly or doubly lists. |
| 19 | +- If not traversed carefully, then we could end up in an infinite loop |
| 20 | +- Elements can be accessed sequentially not randomly compared to an array |
| 21 | +- Extra memory allocation needs to be done for pointers which connects elements in a linked list |
| 22 | + |
| 23 | +### Time Complexity |
| 24 | + |
| 25 | +| Operation | Average | Worst | |
| 26 | +|-----------|---------|-------| |
| 27 | +| Initialize| O(1) | - | |
| 28 | +| Access | O(n) | O(n) | |
| 29 | +| Search | O(n) | O(n) | |
| 30 | +| Insertion | O(1) | O(n) | |
| 31 | +| Deletion | O(1) | O(n) | |
| 32 | + |
| 33 | +### Real World Application |
| 34 | + |
| 35 | +- Allocating CPU to resources |
| 36 | +- Multiplayer Board games |
| 37 | + |
| 38 | +### SLL v.s. CLL |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +### Example |
| 43 | + |
| 44 | +<u>Insertion</u> |
| 45 | +```java |
| 46 | +public void insertHead(int data) |
| 47 | +{ |
| 48 | + Node temp = new Node(data); |
| 49 | + Node cur = head; |
| 50 | + while(cur.getNext() != head) |
| 51 | + cur = cur.getNext(); |
| 52 | + if(head == null) |
| 53 | + { |
| 54 | + head = temp; |
| 55 | + head.setNext(head); |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + temp.setNext(head); |
| 60 | + head = temp; |
| 61 | + cur.setNext(temp); |
| 62 | + } |
| 63 | + size++; |
| 64 | +} |
| 65 | + ``` |
| 66 | + |
| 67 | +## Code Implementation Links |
| 68 | + |
| 69 | +- [JavaScript](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/SingleCircularLinkedList.js.js) |
| 70 | +- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/cll/cll.cpp) |
| 71 | +- [Python](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/circular_linked_list.py) |
| 72 | + |
| 73 | +## Video Explanation |
| 74 | + |
| 75 | +[Video explanation on YouTube](https://youtu.be/HMkdlu5sP4A) |
0 commit comments