Draw a circular doubly linked list. Give an advantage of a circular doubly 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 the representation of a Circular doubly linked list node in C/C++:
- C
Insertion in Circular Doubly Linked List:
1. Insertion at the end of the list or in an empty list:
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.
2. 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 the last node’s next pointer points to this M node, and first node’s previous pointer points to this M node.
Below is the implementation of the above operations:
- C++
- Java
- Python3
- C#
- Javascript
3. 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 of 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.
Below is the implementation of the above operation:
- C++
- Java
- Python3
- C#
- Javascript
4. Insertion in between the nodes of the list:
To insert a node in between the list, two data values are required one after which new node will be inserted and another is the data of the new node.
Below is the implementation of the above operation:
- C++
- Java
- Python3
- C#
- Javascript
Following is a complete program that uses all of the above methods to create a circular doubly linked list.
- C++
- Java
- Python3
- C#
- Javascript
Created circular doubly linked list is: Traversal in forward direction 4 5 6 7 8 Traversal in reverse direction 8 7 6 5 4
Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.
Advantages of circular doubly linked list:
- 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.
Disadvantages of circular doubly linked list:
- 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 of the list may get lost.
Applications of Circular doubly linked list:
- Managing song playlists in media player applications.
- Managing shopping carts in online shopping.
Tirthankar Pal
MBA from IIT Kharagpur with GATE, GMAT, IIT Kharagpur Written Test, and Interview
2 year PGDM (E-Business) from Welingkar, Mumbai
4 years of Bachelor of Science (Hons) in Computer Science from the National Institute of Electronics and Information Technology
Google and Hubspot Certification
Brain Bench Certification in C++, VC++, Data Structure and Project Management
10 years of Experience in Software Development out of that 6 years 8 months in Wipro
Selected in Six World Class UK Universities:-
King's College London, Durham University, University of Exeter, University of Sheffield, University of Newcastle, University of Leeds
No comments:
Post a Comment