Doubly Circular Linked List | Set 1 ( Introduction and Insertion ) | C ++ program to illustrate inserting a Node in a Cicular Doubly Linked list in begging , end and middle ; Structure of a Node ; Function to insert at the end ; If the list is empty , create a single node circular and doubly list ; Find last node ; Create Node dynamically ; Start is going to be next of new_node ; Make new node previous of start ; Make last preivous of new node ; Make new node next of old last ; Function to insert Node at the beginning of the List , ; Pointer points to last Node ; Inserting the data ; setting up previous and next of new node ; Update next and previous pointers of start and last . ; Update start pointer ; Function to insert node with value as value1 . The new node is inserted after the node with with value2 ; Inserting the data ; Find node having value2 and next node of it ; insert new_node between temp and next . ; Driver program to test above functions ; Start with the empty list ; Insert 5. So linked list becomes 5 -> NULL ; Insert 4 at the beginning . So linked list becomes 4 -> 5 ; Insert 7 at the end . So linked list becomes 4 -> 5 -> 7 ; Insert 8 at the end . So linked list becomes 4 -> 5 -> 7 -> 8 ; Insert 6 , after 5. So linked list becomes 4 -> 5 -> 6 -> 7 -> 8
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; struct Node * prev ; } ; void insertEnd ( struct Node * * start , int value ) { if ( * start == NULL ) { struct Node * new_node = new Node ; new_node -> data = value ; new_node -> next = new_node -> prev = new_node ; * start = new_node ; return ; } Node * last = ( * start ) -> prev ; struct Node * new_node = new Node ; new_node -> data = value ; new_node -> next = * start ; ( * start ) -> prev = new_node ; new_node -> prev = last ; last -> next = new_node ; } void insertBegin ( struct Node * * start , int value ) { struct Node * last = ( * start ) -> prev ; struct Node * new_node = new Node ; new_node -> data = value ; new_node -> next = * start ; new_node -> prev = last ; last -> next = ( * start ) -> prev = new_node ; * start = new_node ; } void insertAfter ( struct Node * * start , int value1 , int value2 ) { struct Node * new_node = new Node ; new_node -> data = value1 ; struct Node * temp = * start ; while ( temp -> data != value2 ) temp = temp -> next ; struct Node * next = temp -> next ; temp -> next = new_node ; new_node -> prev = temp ; new_node -> next = next ; next -> prev = new_node ; } void display ( struct Node * start ) { struct Node * temp = start ; printf ( " Traversal in forward direction " while ( temp -> next != start ) { printf ( " % d ▁ " , temp -> data ) ; temp = temp -> next ; } printf ( " % d ▁ " , temp -> data ) ; printf ( " Traversal in reverse direction " Node * last = start -> prev ; temp = last ; while ( temp -> prev != last ) { printf ( " % d ▁ " , temp -> data ) ; temp = temp -> prev ; } printf ( " % d ▁ " , temp -> data ) ; } int main ( ) { struct Node * start = NULL ; insertEnd ( & start , 5 ) ; insertBegin ( & start , 4 ) ; insertEnd ( & start , 7 ) ; insertEnd ( & start , 8 ) ; insertAfter ( & start , 6 , 5 ) ; printf ( " Created ▁ circular ▁ doubly ▁ linked ▁ list ▁ is : ▁ " ) ; display ( start ) ; return 0 ; }