Insertion at Specific Position in a Circular Doubly Linked List | Node of the doubly linked list ; Utility function to create a node in memory ; Function to display the list ; Function to count nunmber of elements in the list ; Declare temp pointer to traverse the list ; Variable to store the count ; Iterate the list and increment the count ; As the list is circular , increment the counter at last ; Function to insert a node at a given position in the circular doubly linked list ; Declare two pointers ; Create a new node in memory ; Point temp to start ; count of total elements in the list ; If list is empty or the position is not valid , return False ; Assign the data ; Iterate till the loc ; See in Image , circle 1 ; See in Image , Circle 2 ; See in Image , Circle 3 ; See in Image , Circle 4 ; Function to create circular doubly linked list from array elements ; Declare newNode and temporary pointer ; Iterate the loop until array length ; Create new node ; Assign the array data ; If it is first element Put that node prev and next as start as it is circular ; Find the last node ; Add the last node to make them in circular fashion ; Driver Code ; Array elements to create circular doubly linked list ; Start Pointer ; Create the List ; Display the list before insertion ; Inserting 8 at 3 rd position ; Display the list after insertion
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . prev = None NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def getNode ( ) : NEW_LINE INDENT return ( Node ( 0 ) ) NEW_LINE DEDENT def displayList ( temp ) : NEW_LINE INDENT t = temp NEW_LINE if ( temp == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The ▁ list ▁ is : ▁ " , end = " ▁ " ) NEW_LINE while ( temp . next != t ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp = temp . next NEW_LINE DEDENT print ( temp . data ) NEW_LINE return 1 NEW_LINE DEDENT DEDENT def countList ( start ) : NEW_LINE INDENT temp = start NEW_LINE count = 0 NEW_LINE while ( temp . next != start ) : NEW_LINE INDENT temp = temp . next NEW_LINE count = count + 1 NEW_LINE DEDENT count = count + 1 NEW_LINE return count NEW_LINE DEDENT def insertAtLocation ( start , data , loc ) : NEW_LINE INDENT temp = None NEW_LINE newNode = None NEW_LINE i = 0 NEW_LINE count = 0 NEW_LINE newNode = getNode ( ) NEW_LINE temp = start NEW_LINE count = countList ( start ) NEW_LINE if ( temp == None or count < loc ) : NEW_LINE INDENT return start NEW_LINE DEDENT else : NEW_LINE INDENT newNode . data = data NEW_LINE i = 1 ; NEW_LINE while ( i < loc - 1 ) : NEW_LINE INDENT temp = temp . next NEW_LINE i = i + 1 NEW_LINE DEDENT newNode . next = temp . next NEW_LINE ( temp . next ) . prev = newNode NEW_LINE temp . next = newNode NEW_LINE newNode . prev = temp NEW_LINE return start NEW_LINE DEDENT return start NEW_LINE DEDENT def createList ( arr , n , start ) : NEW_LINE INDENT newNode = None NEW_LINE temp = None NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT newNode = getNode ( ) NEW_LINE newNode . data = arr [ i ] NEW_LINE if ( i == 0 ) : NEW_LINE INDENT start = newNode NEW_LINE newNode . prev = start NEW_LINE newNode . next = start NEW_LINE DEDENT else : NEW_LINE INDENT temp = ( start ) . prev NEW_LINE temp . next = newNode NEW_LINE newNode . next = start NEW_LINE newNode . prev = temp NEW_LINE temp = start NEW_LINE temp . prev = newNode NEW_LINE DEDENT i = i + 1 ; NEW_LINE DEDENT return start NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE start = None NEW_LINE start = createList ( arr , n , start ) NEW_LINE displayList ( start ) NEW_LINE start = insertAtLocation ( start , 8 , 3 ) NEW_LINE displayList ( start ) NEW_LINE DEDENT