codeparrot/xlcost-text-to-code huggingface dataset:
Morris traversal for Preorder | Python program for Morris Preorder traversal A binary tree Node ; Preorder traversal without recursion and without stack ; If left child is null , print the current node data . And , update the current pointer to right child . ; Find the inorder predecessor ; If the right child of inorder predecessor already points to the current node , update the current with it 's right child ; else If right child doesn ' t ▁ point ▁ ▁ to ▁ the ▁ current ▁ node , ▁ then ▁ print ▁ this ▁ ▁ node ' s data and update the right child pointer with the current node and update the current with it 's left child ; Function for sStandard preorder traversal ; Driver program to test
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def MorrisTraversal ( root ) : NEW_LINE INDENT curr = root NEW_LINE while curr : NEW_LINE INDENT if curr . left is None : NEW_LINE INDENT print ( curr . data , end = " ▁ " ) NEW_LINE curr = curr . right NEW_LINE DEDENT else : NEW_LINE INDENT prev = curr . left NEW_LINE while prev . right is not None and prev . right is not curr : NEW_LINE INDENT prev = prev . right NEW_LINE DEDENT if prev . right is curr : NEW_LINE INDENT prev . right = None NEW_LINE curr = curr . right NEW_LINE DEDENT else : NEW_LINE INDENT print ( curr . data , end = " ▁ " ) NEW_LINE prev . right = curr NEW_LINE curr = curr . left NEW_LINE DEDENT DEDENT DEDENT DEDENT def preorfer ( root ) : NEW_LINE INDENT if root : NEW_LINE INDENT print ( root . data , end = " ▁ " ) NEW_LINE preorfer ( root . left ) NEW_LINE preorfer ( root . right ) NEW_LINE DEDENT DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right . left = Node ( 6 ) NEW_LINE root . right . right = Node ( 7 ) NEW_LINE root . left . left . left = Node ( 8 ) NEW_LINE root . left . left . right = Node ( 9 ) NEW_LINE root . left . right . left = Node ( 10 ) NEW_LINE root . left . right . right = Node ( 11 ) NEW_LINE MorrisTraversal ( root ) NEW_LINE print ( " " ) NEW_LINE preorfer ( root ) NEW_LINE
Sum of dependencies in a graph | To add an edge ; Find the sum of all dependencies ; Just find the size at each vector 's index ; Driver code
def addEdge ( adj , u , v ) : NEW_LINE INDENT adj [ u ] . append ( v ) NEW_LINE DEDENT def findSum ( adj , V ) : NEW_LINE INDENT sum = 0 NEW_LINE for u in range ( V ) : NEW_LINE INDENT sum += len ( adj [ u ] ) NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT V = 4 NEW_LINE adj = [ [ ] for i in range ( V ) ] NEW_LINE addEdge ( adj , 0 , 2 ) NEW_LINE addEdge ( adj , 0 , 3 ) NEW_LINE addEdge ( adj , 1 , 3 ) NEW_LINE addEdge ( adj , 2 , 3 ) NEW_LINE print ( " Sum ▁ of ▁ dependencies ▁ is " , findSum ( adj , V ) ) NEW_LINE DEDENT
Linked List | Set 2 ( Inserting a node ) | This function is in LinkedList class Function to insert a new node at the beginning ; 1 & 2 : Allocate the Node & Put in the data ; 3. Make next of new Node as head ; 4. Move the head to point to new Node
def push ( self , new_data ) : NEW_LINE INDENT new_node = Node ( new_data ) NEW_LINE new_node . next = self . head NEW_LINE self . head = new_node NEW_LINE DEDENT
Linked List | Set 2 ( Inserting a node ) | This function is in LinkedList class . Inserts a new node after the given prev_node . This method is defined inside LinkedList class shown above ; 1. check if the given prev_node exists ; 2. Create new node & 3. Put in the data ; 4. Make next of new Node as next of prev_node ; 5. make next of prev_node as new_node
def insertAfter ( self , prev_node , new_data ) : NEW_LINE INDENT if prev_node is None : NEW_LINE INDENT print " The ▁ given ▁ previous ▁ node ▁ must ▁ inLinkedList . " NEW_LINE return NEW_LINE DEDENT new_node = Node ( new_data ) NEW_LINE new_node . next = prev_node . next NEW_LINE prev_node . next = new_node NEW_LINE DEDENT
Iterative Preorder Traversal | A binary tree node ; An iterative process to print preorder traveral of BT ; Base CAse ; create an empty stack and push root to it ; Pop all items one by one . Do following for every popped item a ) print it b ) push its right child c ) push its left child Note that right child is pushed first so that left is processed first ; Pop the top item from stack and print it ; Push right and left children of the popped node to stack ; Driver program to test above function
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def iterativePreorder ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT nodeStack = [ ] NEW_LINE nodeStack . append ( root ) NEW_LINE while ( len ( nodeStack ) > 0 ) : NEW_LINE INDENT node = nodeStack . pop ( ) NEW_LINE print ( node . data , end = " ▁ " ) NEW_LINE if node . right is not None : NEW_LINE INDENT nodeStack . append ( node . right ) NEW_LINE DEDENT if node . left is not None : NEW_LINE INDENT nodeStack . append ( node . left ) NEW_LINE DEDENT DEDENT DEDENT root = Node ( 10 ) NEW_LINE root . left = Node ( 8 ) NEW_LINE root . right = Node ( 2 ) NEW_LINE root . left . left = Node ( 3 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right . left = Node ( 2 ) NEW_LINE iterativePreorder ( root ) NEW_LINE
Find Length of a Linked List ( Iterative and Recursive ) | Node class ; Linked List class contains a Node object ; Function to initialize head ; This function is in LinkedList class . It inserts a new node at the beginning of Linked List . ; 1 & 2 : Allocate the Node & Put in the data ; 3. Make next of new Node as head ; 4. Move the head to point to new Node ; This function counts number of nodes in Linked List recursively , given ' node ' as starting node . ; Base case ; Count is this node plus rest of the list ; A wrapper over getCountRec ( ) ; Code execution starts here ; Start with the empty list
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT class LinkedList : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . head = None NEW_LINE DEDENT def push ( self , new_data ) : NEW_LINE INDENT new_node = Node ( new_data ) NEW_LINE new_node . next = self . head NEW_LINE self . head = new_node NEW_LINE DEDENT def getCountRec ( self , node ) : NEW_LINE INDENT if ( not node ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return 1 + self . getCountRec ( node . next ) NEW_LINE DEDENT DEDENT def getCount ( self ) : NEW_LINE INDENT return self . getCountRec ( self . head ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT llist = LinkedList ( ) NEW_LINE llist . push ( 1 ) NEW_LINE llist . push ( 3 ) NEW_LINE llist . push ( 1 ) NEW_LINE llist . push ( 2 ) NEW_LINE llist . push ( 1 ) NEW_LINE print ' Count ▁ of ▁ nodes ▁ is ▁ : ' , llist . getCount ( ) NEW_LINE DEDENT
Iterative Preorder Traversal | Tree Node ; Iterative function to do Preorder traversal of the tree ; start from root node ( set current node to root node ) ; run till stack is not empty or current is not NULL ; Print left children while exist and keep appending right into the stack . ; We reach when curr is NULL , so We take out a right child from stack ; Driver Code
class Node : NEW_LINE INDENT def __init__ ( self , data = 0 ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def preorderIterative ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT st = [ ] NEW_LINE curr = root NEW_LINE while ( len ( st ) or curr != None ) : NEW_LINE INDENT while ( curr != None ) : NEW_LINE INDENT print ( curr . data , end = " ▁ " ) NEW_LINE if ( curr . right != None ) : NEW_LINE INDENT st . append ( curr . right ) NEW_LINE DEDENT curr = curr . left NEW_LINE DEDENT if ( len ( st ) > 0 ) : NEW_LINE INDENT curr = st [ - 1 ] NEW_LINE st . pop ( ) NEW_LINE DEDENT DEDENT DEDENT root = Node ( 10 ) NEW_LINE root . left = Node ( 20 ) NEW_LINE root . right = Node ( 30 ) NEW_LINE root . left . left = Node ( 40 ) NEW_LINE root . left . left . left = Node ( 70 ) NEW_LINE root . left . right = Node ( 50 ) NEW_LINE root . right . left = Node ( 60 ) NEW_LINE root . left . left . right = Node ( 80 ) NEW_LINE preorderIterative ( root ) NEW_LINE
Write a function that counts the number of times a given int occurs in a Linked List | Counts the no . of occurrences of a node ( search_for ) in a linked list ( head )
def count ( self , temp , key ) : NEW_LINE INDENT if temp is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT if temp . data == key : NEW_LINE INDENT return 1 + count ( temp . next , key ) NEW_LINE DEDENT return count ( temp . next , key ) NEW_LINE DEDENT
Detect loop in a linked list | Link list node ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Returns true if there is a loop in linked list else returns false . ; If this node is already traverse it means there is a cycle ( Because you we encountering the node for the second time ) . ; If we are seeing the node for the first time , mark its flag as 1 ; Driver program to test above function ; Start with the empty list ; Create a loop for testing
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = 0 NEW_LINE self . next = None NEW_LINE self . flag = 0 NEW_LINE DEDENT DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( ) ; NEW_LINE new_node . data = new_data ; NEW_LINE new_node . flag = 0 ; NEW_LINE new_node . next = ( head_ref ) ; NEW_LINE ( head_ref ) = new_node ; NEW_LINE return head_ref NEW_LINE DEDENT def detectLoop ( h ) : NEW_LINE INDENT while ( h != None ) : NEW_LINE INDENT if ( h . flag == 1 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT h . flag = 1 ; NEW_LINE h = h . next ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None ; NEW_LINE head = push ( head , 20 ) ; NEW_LINE head = push ( head , 4 ) ; NEW_LINE head = push ( head , 15 ) ; NEW_LINE head = push ( head , 10 ) NEW_LINE head . next . next . next . next = head ; NEW_LINE if ( detectLoop ( head ) ) : NEW_LINE INDENT print ( " Loop ▁ found " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ Loop " ) NEW_LINE DEDENT DEDENT
Detect loop in a linked list | Python3 program to return first node of loop A binary tree node has data , pointer to left child and a pointer to right child Helper function that allocates a new node with the given data and None left and right pointers ; A utility function to pra linked list ; Function to detect first node of loop in a linked list that may contain loop ; Create a temporary node ; This condition is for the case when there is no loop ; Check if next is already pointing to temp ; Store the pointer to the next node in order to get to it in the next step ; Make next poto temp ; Get to the next node in the list ; Driver Code ; Create a loop for testing ( 5 is pointing to 3 )
class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def printList ( head ) : NEW_LINE INDENT while ( head != None ) : NEW_LINE INDENT print ( head . key , end = " ▁ " ) NEW_LINE head = head . next NEW_LINE DEDENT print ( ) NEW_LINE DEDENT def detectLoop ( head ) : NEW_LINE INDENT temp = " " NEW_LINE while ( head != None ) : NEW_LINE INDENT if ( head . next == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( head . next == temp ) : NEW_LINE INDENT return True NEW_LINE DEDENT nex = head . next NEW_LINE head . next = temp NEW_LINE head = nex NEW_LINE DEDENT return False NEW_LINE DEDENT head = newNode ( 1 ) NEW_LINE head . next = newNode ( 2 ) NEW_LINE head . next . next = newNode ( 3 ) NEW_LINE head . next . next . next = newNode ( 4 ) NEW_LINE head . next . next . next . next = newNode ( 5 ) NEW_LINE head . next . next . next . next . next = head . next . next NEW_LINE found = detectLoop ( head ) NEW_LINE if ( found ) : NEW_LINE INDENT print ( " Loop ▁ Found " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ Loop " ) NEW_LINE DEDENT
Swap nodes in a linked list without swapping data | Python program to swap two given nodes of a linked list ; head of list ; Function to swap Nodes x and y in linked list by changing links ; Nothing to do if x and y are same ; Search for x ( keep track of prevX and CurrX ) ; Search for y ( keep track of prevY and currY ) ; If either x or y is not present , nothing to do ; If x is not head of linked list ; make y the new head ; If y is not head of linked list ; make x the new head ; Swap next pointers ; Function to add Node at beginning of list . ; 1. alloc the Node and put the data ; 2. Make next of new Node as head ; 3. Move the head to point to new Node ; This function prints contents of linked list starting from the given Node ; Driver program to test above function ; The constructed linked list is : 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7
class LinkedList ( object ) : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . head = None NEW_LINE DEDENT class Node ( object ) : NEW_LINE INDENT def __init__ ( self , d ) : NEW_LINE INDENT self . data = d NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def swapNodes ( self , x , y ) : NEW_LINE INDENT if x == y : NEW_LINE INDENT return NEW_LINE DEDENT prevX = None NEW_LINE currX = self . head NEW_LINE while currX != None and currX . data != x : NEW_LINE INDENT prevX = currX NEW_LINE currX = currX . next NEW_LINE DEDENT prevY = None NEW_LINE currY = self . head NEW_LINE while currY != None and currY . data != y : NEW_LINE INDENT prevY = currY NEW_LINE currY = currY . next NEW_LINE DEDENT if currX == None or currY == None : NEW_LINE INDENT return NEW_LINE DEDENT if prevX != None : NEW_LINE INDENT prevX . next = currY NEW_LINE DEDENT else : NEW_LINE INDENT self . head = currY NEW_LINE DEDENT if prevY != None : NEW_LINE INDENT prevY . next = currX NEW_LINE DEDENT else : NEW_LINE INDENT self . head = currX NEW_LINE DEDENT temp = currX . next NEW_LINE currX . next = currY . next NEW_LINE currY . next = temp NEW_LINE DEDENT def push ( self , new_data ) : NEW_LINE INDENT new_Node = self . Node ( new_data ) NEW_LINE new_Node . next = self . head NEW_LINE self . head = new_Node NEW_LINE DEDENT def printList ( self ) : NEW_LINE INDENT tNode = self . head NEW_LINE while tNode != None : NEW_LINE INDENT print tNode . data , NEW_LINE tNode = tNode . next NEW_LINE DEDENT DEDENT DEDENT llist = LinkedList ( ) NEW_LINE llist . push ( 7 ) NEW_LINE llist . push ( 6 ) NEW_LINE llist . push ( 5 ) NEW_LINE llist . push ( 4 ) NEW_LINE llist . push ( 3 ) NEW_LINE llist . push ( 2 ) NEW_LINE llist . push ( 1 ) NEW_LINE print " Linked ▁ list ▁ before ▁ calling ▁ swapNodes ( ) ▁ " NEW_LINE llist . printList ( ) NEW_LINE llist . swapNodes ( 4 , 3 ) NEW_LINE print   " NEW_LINE Linked list after calling swapNodes ( )   " NEW_LINE llist . printList ( ) NEW_LINE
Postorder traversal of Binary Tree without recursion and without stack | A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Visited left subtree ; Visited right subtree ; Print node ; Driver program to test above functions
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def postorder ( head ) : NEW_LINE INDENT temp = head NEW_LINE visited = set ( ) NEW_LINE while ( temp and temp not in visited ) : NEW_LINE INDENT if ( temp . left and temp . left not in visited ) : NEW_LINE INDENT temp = temp . left NEW_LINE DEDENT elif ( temp . right and temp . right not in visited ) : NEW_LINE INDENT temp = temp . right NEW_LINE DEDENT else : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE visited . add ( temp ) NEW_LINE temp = head NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 8 ) NEW_LINE root . left = newNode ( 3 ) NEW_LINE root . right = newNode ( 10 ) NEW_LINE root . left . left = newNode ( 1 ) NEW_LINE root . left . right = newNode ( 6 ) NEW_LINE root . left . right . left = newNode ( 4 ) NEW_LINE root . left . right . right = newNode ( 7 ) NEW_LINE root . right . right = newNode ( 14 ) NEW_LINE root . right . right . left = newNode ( 13 ) NEW_LINE postorder ( root ) NEW_LINE DEDENT
Postorder traversal of Binary Tree without recursion and without stack | A Binary Tree Node Utility function to create a new tree node ; Visited left subtree ; Visited right subtree ; Print node ; Driver Code
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE self . visited = False NEW_LINE DEDENT DEDENT def postorder ( head ) : NEW_LINE INDENT temp = head NEW_LINE while ( temp and temp . visited == False ) : NEW_LINE INDENT if ( temp . left and temp . left . visited == False ) : NEW_LINE INDENT temp = temp . left NEW_LINE DEDENT elif ( temp . right and temp . right . visited == False ) : NEW_LINE INDENT temp = temp . right NEW_LINE DEDENT else : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp . visited = True NEW_LINE temp = head NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 8 ) NEW_LINE root . left = newNode ( 3 ) NEW_LINE root . right = newNode ( 10 ) NEW_LINE root . left . left = newNode ( 1 ) NEW_LINE root . left . right = newNode ( 6 ) NEW_LINE root . left . right . left = newNode ( 4 ) NEW_LINE root . left . right . right = newNode ( 7 ) NEW_LINE root . right . right = newNode ( 14 ) NEW_LINE root . right . right . left = newNode ( 13 ) NEW_LINE postorder ( root ) NEW_LINE DEDENT
Josephus Circle implementation using STL list | structure for a node in circular linked list ; Function to find the only person left after one in every m - th node is killed in a circle of n nodes ; Create a circular linked list of size N . ; Connect last node to first ; while only one node is left in the linked list ; Find m - th node ; Remove the m - th node ; Driver program to test above functions
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def getJosephusPosition ( m , n ) : NEW_LINE INDENT head = Node ( 1 ) NEW_LINE prev = head NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT prev . next = Node ( i ) NEW_LINE prev = prev . next NEW_LINE prev . next = head NEW_LINE DEDENT ptr1 = head NEW_LINE ptr2 = head NEW_LINE while ( ptr1 . next != ptr1 ) : NEW_LINE INDENT count = 1 NEW_LINE while ( count != m ) : NEW_LINE INDENT ptr2 = ptr1 NEW_LINE ptr1 = ptr1 . next NEW_LINE count += 1 NEW_LINE DEDENT ptr2 . next = ptr1 . next NEW_LINE ptr1 = ptr2 . next NEW_LINE DEDENT print ( " Last ▁ person ▁ left ▁ standing ▁ ( Josephus ▁ Position ) ▁ is ▁ " , ptr1 . data ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 14 NEW_LINE m = 2 NEW_LINE getJosephusPosition ( m , n ) NEW_LINE DEDENT
Diagonal Traversal of Binary Tree | A binary tree node ; root - root of the binary tree d - distance of current line from rightmost - topmost slope . diagonalPrint - multimap to store Diagonal elements ( Passed by Reference ) ; Base Case ; Store all nodes of same line together as a vector ; Increase the vertical distance if left child ; Vertical distance remains same for right child ; Print diagonal traversal of given binary tree ; Create a dict to store diagnoal elements ; Driver Program
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def diagonalPrintUtil ( root , d , diagonalPrintMap ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT try : NEW_LINE INDENT diagonalPrintMap [ d ] . append ( root . data ) NEW_LINE DEDENT except KeyError : NEW_LINE INDENT diagonalPrintMap [ d ] = [ root . data ] NEW_LINE DEDENT diagonalPrintUtil ( root . left , d + 1 , diagonalPrintMap ) NEW_LINE diagonalPrintUtil ( root . right , d , diagonalPrintMap ) NEW_LINE DEDENT def diagonalPrint ( root ) : NEW_LINE INDENT diagonalPrintMap = dict ( ) NEW_LINE diagonalPrintUtil ( root , 0 , diagonalPrintMap ) NEW_LINE print " Diagonal ▁ Traversal ▁ of ▁ binary ▁ tree ▁ : ▁ " NEW_LINE for i in diagonalPrintMap : NEW_LINE INDENT for j in diagonalPrintMap [ i ] : NEW_LINE INDENT print j , NEW_LINE DEDENT print " " NEW_LINE DEDENT DEDENT root = Node ( 8 ) NEW_LINE root . left = Node ( 3 ) NEW_LINE root . right = Node ( 10 ) NEW_LINE root . left . left = Node ( 1 ) NEW_LINE root . left . right = Node ( 6 ) NEW_LINE root . right . right = Node ( 14 ) NEW_LINE root . right . right . left = Node ( 13 ) NEW_LINE root . left . right . left = Node ( 4 ) NEW_LINE root . left . right . right = Node ( 7 ) NEW_LINE diagonalPrint ( root ) NEW_LINE
Doubly Linked List | Set 1 ( Introduction and Insertion ) | Adding a node at the front of the list ; 1 & 2 : Allocate the Node & Put in the data ; 3. Make next of new node as head and previous as NULL ; 4. change prev of head node to new node ; 5. move the head to point to the new node
def push ( self , new_data ) : NEW_LINE INDENT new_node = Node ( data = new_data ) NEW_LINE new_node . next = self . head NEW_LINE new_node . prev = None NEW_LINE if self . head is not None : NEW_LINE INDENT self . head . prev = new_node NEW_LINE DEDENT self . head = new_node NEW_LINE DEDENT
Doubly Linked List | Set 1 ( Introduction and Insertion ) | Given a node as prev_node , insert a new node after the given node ; 1. check if the given prev_node is NULL ; 2. allocate node & 3. put in the data ; 4. Make next of new node as next of prev_node ; 5. Make the next of prev_node as new_node ; 6. Make prev_node as previous of new_node ; 7. Change previous of new_node 's next node
def insertAfter ( self , prev_node , new_data ) : NEW_LINE INDENT if prev_node is None : NEW_LINE INDENT print ( " This ▁ node ▁ doesn ' t ▁ exist ▁ in ▁ DLL " ) NEW_LINE return NEW_LINE DEDENT new_node = Node ( data = new_data ) NEW_LINE new_node . next = prev_node . next NEW_LINE prev_node . next = new_node NEW_LINE new_node . prev = prev_node NEW_LINE if new_node . next is not None : NEW_LINE INDENT new_node . next . prev = new_node NEW_LINE DEDENT DEDENT
Doubly Linked List | Set 1 ( Introduction and Insertion ) | Add a node at the end of the DLL ; 1. allocate node 2. put in the data ; 3. This new node is going to be the last node , so make next of it as NULL ; 4. If the Linked List is empty , then make the new node as head ; 5. Else traverse till the last node ; 6. Change the next of last node ; 7. Make last node as previous of new node
def append ( self , new_data ) : NEW_LINE INDENT new_node = Node ( data = new_data ) NEW_LINE last = self . head NEW_LINE new_node . next = None NEW_LINE if self . head is None : NEW_LINE INDENT new_node . prev = None NEW_LINE self . head = new_node NEW_LINE return NEW_LINE DEDENT while ( last . next is not None ) : NEW_LINE INDENT last = last . next NEW_LINE DEDENT last . next = new_node NEW_LINE new_node . prev = last NEW_LINE DEDENT