In Circular Linked List Insertion of Node Requires Modification of: In a circular linked list, insertion of a record involves modification of a two-pointer. A circular linked list is a linked list in which the last node points to the head or front node making the data structure look like a circle. There is no NULL at the end. A circular linked list can be a singly circular linked list or a doubly circular linked list.
For the insertion of a node in the beginning we need to traverse the whole list. Also, for insertion and the end, the whole list has to be traversed. If instead of a start pointer we take a pointer to the last node then in both cases there won’t be any need to traverse the whole list. So insertion in the begging or at the end takes constant time irrespective of the length of the list.
एक सर्कुलर लिंक्ड सूची में, एक रिकॉर्ड के सम्मिलन में दो-सूचक का संशोधन शामिल है। सर्कुलर लिंक्ड लिस्ट एक लिंक्ड लिस्ट होती है जिसमें आखिरी नोड हेड या फ्रंट नोड को इंगित करता है जिससे डेटा संरचना एक सर्कल की तरह दिखती है। अंत में कोई NULL नहीं है। सर्कुलर लिंक्ड लिस्ट सिंगल सर्कुलर लिंक्ड लिस्ट या डबल सर्कुलर लिंक्ड लिस्ट हो सकती है। शुरुआत में एक नोड डालने के लिए हमें पूरी सूची को पार करना होगा। इसके अलावा, सम्मिलन और अंत के लिए, पूरी सूची को पार करना होगा। यदि एक स्टार्ट पॉइंटर के बजाय हम एक पॉइंटर को अंतिम नोड तक ले जाते हैं तो दोनों ही मामलों में पूरी सूची को पार करने की कोई आवश्यकता नहीं होगी। इसलिए भीख मांगने या अंत में प्रविष्टि में सूची की लंबाई के बावजूद निरंतर समय लगता है।
Insertion into the circular singly linked list at the beginning
There are two scenarios in which a node can be inserted in a circular singly linked list at the beginning. Either the node will be inserted in an empty list or the node is to be inserted in an already filled list.
Firstly, allocate the memory space for the new node by using the malloc method of the C language.
struct node *ptr = (struct node *)malloc(sizeof(struct node));
In the first scenario, the condition head == NULL will be true. Since, the list in which, we are inserting the node is a circular singly linked list, therefore the only node of the list (which is just inserted into the list) will point to itself only. We also need to make the head pointer point to this node.
This will be done by using the following statements.
if(head == NULL)
head = ptr;
ptr -> next = head;
In the second scenario, the condition head == NULL will become false which means that the list contains at least one node. In this case, we need to traverse the list in order to reach the last node of the list.
This will be done by using the following statement.
temp = head;
while(temp->next != head)
temp = temp->next;
At the end of the loop, the pointer temp would point to the last node of the list. Since, in a circular singly linked list, the last node of the list contains a pointer to the first node of the list. Therefore, we need to make the next pointer of the last node point to the head node of the list and the new node which is being inserted into the list will be the new head node of the list therefore the next pointer of temp will point to the new node ptr.
This will be done by using the following statements.
temp -> next = ptr;
the next pointer of temp will point to the existing head node of the list.
ptr->next = head;
Now, make the new node ptr, the new head node of the circular singly linked list.
head = ptr;
In this way, the node ptr has been inserted into the circular singly linked list at the beginning.
- IF PTR = NULL
- Write OVERFLOW
- Go to Step 11
- [END OF IF]
- SET NEW_NODE = PTR
- SET PTR = PTR -> NEXT
- Then SET NEW_NODE -> DATA = VAL
- SET TEMP = HEAD
- Repeat Step 8 while TEMP -> NEXT != HEAD
- SET TEMP = TEMP -> NEXT
- [END OF LOOP]
- SET NEW_NODE -> NEXT = HEAD
- Then SET TEMP → NEXT = NEW_NODE
- SET HEAD = NEW_NODE
Doubly Circular Linked List | Set 1 (Introduction and Insertion)
Prerequisite: Doubly Linked list, Circular Linked List
Circular Doubly Linked List has properties of both doubly linked list and circular linked list in which two consecutive elements are linked or connected by the previous and next pointer and the last node points to the first node by the next pointer and also the first node points to the last node by the previous pointer.
Following is a representation of a Circular doubly linked list node in C/C++:
// Structure of the node
struct node *next; // Pointer to next node
struct node *prev; // Pointer to previous node
Insertion in Circular Doubly Linked List
- Insertion at the end of the list or in an empty list
- Empty List (start = NULL): A node(Say N) is inserted with data = 5, so the previous pointer of N points to N and the next pointer of N also points to N. But now start pointer points to the first node of the list.
- The list initially contains some nodes, start points to the first node of the List: A node(Say M) is inserted with data = 7, so the previous pointer of M points to the last node, the next pointer of M points to the first node and last node’s next pointer points to this M node and first node’s previous pointer points to this M node.
- Insertion at the beginning of the list: To insert a node at the beginning of the list, create a node(Say T) with data = 5, T next pointer points to the first node of the list, T previous pointer points to the last node the list, last node’s next pointer points to this T node, first node’s previous pointer also points this T node and at last don’t forget to shift ‘Start’ pointer to this T node.
- The list can be traversed from both directions i.e. from head to tail or from tail to head.
- Jumping from head to tail or from tail to head is done in constant time O(1).
- Circular Doubly Linked Lists are used for the implementation of advanced data structures like the Fibonacci Heap.
- It takes slightly extra memory in each node to accommodate the previous pointer.
- Lots of pointers are involved while implementing or doing operations on a list. So, pointers should be handled carefully otherwise data from the list may get lost.
Applications of Circular doubly linked list
- Managing songs playlist in media player applications.
- Managing shopping cart in online shopping.
FAQs on In Circular Linked List Insertion of Node Requires Modification of
How many pointers are modified while inserting a node in a circular linked list?
So There needs modification of two pointers.
Which of the following is a limitation of using a circular linked list?
The disadvantages of using a circular linked list are below:
- Circular lists are complex as compared to singly linked lists.
- The reverse of circular lists is complex as compared to singly or doubly lists.
- If not handled carefully, then the code may go into an infinite loop
What is the time complexity of inserting a node in a circular linked list?
Circular Linked List Complexity. The insertion operations that do not require traversal have the time complexity of O(1). And, an insertion that requires traversal has a time complexity of O(n). The space complexity is O(1)
How many pointers will have to be changed when a new node is to be added?
This depends on whether we are inserting the new node in the middle of the list (surrounded by two nodes), or at the head or tail of the list. Insertion at the middle node will affect 4 pointers whereas at the head or tail will affect only 2 pointers.
How many modifications are required to delete a node at the beginning?
Deleting a node from the beginning of the list is the simplest operation of all. It just needs a few adjustments in the node pointers. Since the first node of the list is to be deleted, therefore, we just need to make the head and point to the next of the head.
What is the principle of a circular linked list?
Circular Linked List is a variation of a Linked list in which the first element points to the last element and the last element points to the first element. Both Singly Linked List and Doubly Linked List can be made into a circular linked list.