ralphdc09

doubly

Nov 12th, 2021 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.97 KB | None | 0 0
  1. import gc
  2.  
  3.  
  4. class Node:
  5.  
  6.     def __init__(self, data):
  7.         self.data = data
  8.         self.next = None
  9.         self.prev = None
  10.  
  11.  
  12. class DoublyLinkedList:
  13.     def __init__(self):
  14.         self.head = None
  15.  
  16.     # function for inserting node at the front
  17.     def insert_front(self, data):
  18.  
  19.         # allocate memory for newNode and assign data to newNode
  20.         new_node = Node(data)
  21.  
  22.         # make newNode as a head
  23.         new_node.next = self.head
  24.  
  25.         # assign null to prev
  26.  
  27.         # previous of head
  28.         if self.head is not None:
  29.             self.head.prev = new_node
  30.  
  31.         # head point to newNode
  32.         self.head = new_node
  33.  
  34.     # function for inserting new node to a specific position
  35.     def insert_after(self, prev_node, data):
  36.  
  37.         # check if previous node is null
  38.         if prev_node is None:
  39.             print("Previous node cannot be null!")
  40.             return
  41.  
  42.         # allocate memory for newNode and assign data to newNode
  43.         new_node = Node(data)
  44.  
  45.         # set next of newNode to next of prev node
  46.         new_node.next = prev_node.next
  47.  
  48.         # set next of prev node to newNode
  49.         prev_node.next = new_node
  50.  
  51.         # set prev of newNode to the previous node
  52.         new_node.prev = prev_node
  53.  
  54.         # set prev of newNode's next to newNode
  55.         if new_node.next:
  56.             new_node.next.prev = new_node
  57.  
  58.     # function for inserting new node at the end of the list
  59.     def insert_end(self, data):
  60.  
  61.         # allocate memory for newNode and assign data to newNode
  62.         new_node = Node(data)
  63.  
  64.         # assign null to next of newNode (already done in constructor)
  65.  
  66.         # if the linked list is empty, make the newNode as head node
  67.         if self.head is None:
  68.             self.head = new_node
  69.             return
  70.  
  71.         # store the head node temporarily (for later use)
  72.         temp = self.head
  73.  
  74.         # if the linked list is not empty, traverse to the end of the linked list
  75.         while temp.next:
  76.             temp = temp.next
  77.  
  78.         # now, the last node of the linked list is temp
  79.  
  80.         # assign next of the last node (temp) to newNode
  81.         temp.next = new_node
  82.  
  83.         # assign prev of newNode to temp
  84.         new_node.prev = temp
  85.  
  86.         return
  87.  
  88.     # function for deleting node from the doubly linked list
  89.     def delete_node(self, delete):
  90.  
  91.         # if head or del is null, deletion is not possible
  92.         if self.head is None or delete is None:
  93.             return
  94.  
  95.         # if del_node is the head node, point the head pointer to the next of del_node
  96.         if self.head == delete:
  97.             self.head = delete.next
  98.  
  99.         # if del_node is not at the last node, point the prev of node next to del_node to the previous of del_node
  100.         if delete.next is not None:
  101.             delete.next.prev = delete.prev
  102.  
  103.         # if del_node is not the first node, point the next of the previous node to the next node of del_node
  104.         if delete.prev is not None:
  105.             delete.prev.next = delete.next
  106.  
  107.         # free the memory of del_node
  108.         gc.collect()
  109.  
  110.     # function for printing the doubly linked list
  111.     def display_list(self, node):
  112.  
  113.         while node:
  114.             print(node.data, end="->")
  115.             last = node
  116.             node = node.next
  117.  
  118.  
  119. # initialize an empty node
  120. doubly_linked_list = DoublyLinkedList()
  121.  
  122. doubly_linked_list.insert_end("Glen")
  123. doubly_linked_list.insert_front("Ralph")
  124. doubly_linked_list.insert_front("Espe")
  125. doubly_linked_list.insert_end("Frieda")
  126. print("The node of the doubly linked list: ")
  127. doubly_linked_list.display_list(doubly_linked_list.head)
  128. print()
  129.  
  130. # insert new node after head
  131. doubly_linked_list.insert_after(doubly_linked_list.head, "Angelo")
  132.  
  133. # insert new node after the second node
  134. doubly_linked_list.insert_after(doubly_linked_list.head.next, "Nomer")
  135.  
  136. print("\nThe node of the doubly linked list after insertion: ")
  137. doubly_linked_list.display_list(doubly_linked_list.head)
  138.  
  139.  
  140.  
Add Comment
Please, Sign In to add comment