Doubly Circular Linked List | Set 1 ( Introduction and Insertion ) | 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 Code ; Start with the empty list ; Insert 5. So linked list becomes 5. None ; 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
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE self . prev = None NEW_LINE DEDENT DEDENT def insertEnd ( value ) : NEW_LINE INDENT global start NEW_LINE if ( start == None ) : NEW_LINE INDENT new_node = Node ( 0 ) NEW_LINE new_node . data = value NEW_LINE new_node . next = new_node . prev = new_node NEW_LINE start = new_node NEW_LINE return NEW_LINE DEDENT last = ( start ) . prev NEW_LINE new_node = Node ( 0 ) NEW_LINE new_node . data = value NEW_LINE new_node . next = start NEW_LINE ( start ) . prev = new_node NEW_LINE new_node . prev = last NEW_LINE last . next = new_node NEW_LINE DEDENT def insertBegin ( value ) : NEW_LINE INDENT global start NEW_LINE last = ( start ) . prev NEW_LINE new_node = Node ( 0 ) NEW_LINE new_node . data = value NEW_LINE new_node . next = start NEW_LINE new_node . prev = last NEW_LINE last . next = ( start ) . prev = new_node NEW_LINE start = new_node NEW_LINE DEDENT def insertAfter ( value1 , value2 ) : NEW_LINE INDENT global start NEW_LINE new_node = Node ( 0 ) NEW_LINE new_node . data = value1 NEW_LINE temp = start NEW_LINE while ( temp . data != value2 ) : NEW_LINE INDENT temp = temp . next NEW_LINE DEDENT next = temp . next NEW_LINE temp . next = new_node NEW_LINE new_node . prev = temp NEW_LINE new_node . next = next NEW_LINE next . prev = new_node NEW_LINE DEDENT def display ( ) : NEW_LINE INDENT global start NEW_LINE temp = start NEW_LINE print ( " Traversal ▁ in ▁ forward ▁ direction : " ) NEW_LINE while ( temp . next != start ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp = temp . next NEW_LINE DEDENT print ( temp . data ) NEW_LINE print ( " Traversal ▁ in ▁ reverse ▁ direction : " ) NEW_LINE last = start . prev NEW_LINE temp = last NEW_LINE while ( temp . prev != last ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp = temp . prev NEW_LINE DEDENT print ( temp . data ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT global start NEW_LINE start = None NEW_LINE insertEnd ( 5 ) NEW_LINE insertBegin ( 4 ) NEW_LINE insertEnd ( 7 ) NEW_LINE insertEnd ( 8 ) NEW_LINE insertAfter ( 6 , 5 ) NEW_LINE print ( " Created ▁ circular ▁ doubly ▁ linked ▁ list ▁ is : ▁ " ) NEW_LINE display ( ) NEW_LINE DEDENT