Advertisement
ralphdc09

circular

Nov 12th, 2021
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.31 KB | None | 0 0
  1. # Python code to perform circular linked list operations
  2.  
  3. class Node:
  4.     def __init__(self, data):
  5.         self.data = data
  6.         self.next = None
  7.  
  8.  
  9. class CircularLinkedList:
  10.     def __init__(self):
  11.         self.last = None
  12.  
  13.     def add_to_empty(self, data):
  14.  
  15.         if self.last is not None:
  16.             return self.last
  17.  
  18.         # allocate memory to the new node and add data to the node
  19.         new_node = Node(data)
  20.  
  21.         # assign last to new_node
  22.         self.last = new_node
  23.  
  24.         # create link to itself
  25.         self.last.next = self.last
  26.         return self.last
  27.  
  28.     # add node to the front
  29.     def add_front(self, data):
  30.  
  31.         # check if the list is empty
  32.         if self.last is None:
  33.             return self.add_to_empty(data)
  34.  
  35.         # allocate memory to the new node and add data to the node
  36.         new_node = Node(data)
  37.  
  38.         # store the address of the current first node in the new_node
  39.         new_node.next = self.last.next
  40.  
  41.         # make new_node as last
  42.         self.last.next = new_node
  43.  
  44.         return self.last
  45.  
  46.     # add node to the end
  47.     def add_end(self, data):
  48.         # check if the node is empty
  49.         if self.last is None:
  50.             return self.add_to_empty(data)
  51.  
  52.         # allocate memory to the new node and add data to the node
  53.         newNode = Node(data)
  54.  
  55.         # store the address of the last node to next of newNode
  56.         newNode.next = self.last.next
  57.  
  58.         # point the current last node to the newNode
  59.         self.last.next = newNode
  60.  
  61.         # make newNode as the last node
  62.         self.last = newNode
  63.  
  64.         return self.last
  65.  
  66.     # insert node after a specific node
  67.     def add_after(self, data, item):
  68.  
  69.         # check if the list is empty
  70.         if self.last is None:
  71.             return None
  72.  
  73.         new_node = Node(data)
  74.         p = self.last.next
  75.         while p:
  76.  
  77.             # if the item is found, place new_node after it
  78.             if p.data == item:
  79.  
  80.                 # make the next of the current node as the next of new_node
  81.                 new_node.next = p.next
  82.  
  83.                 # put new_node to the next of p
  84.                 p.next = new_node
  85.  
  86.                 if p == self.last:
  87.                     self.last = new_node
  88.                     return self.last
  89.                 else:
  90.                     return self.last
  91.             p = p.next
  92.             if p == self.last.next:
  93.                 print(item, "The given node is not present in the list")
  94.                 break
  95.  
  96.     # delete a node
  97.     def delete_node(self, last, key):
  98.  
  99.         # If linked list is empty
  100.         if last is None:
  101.             return
  102.  
  103.         # If the list contains only a single node
  104.         if last.data == key and last.next == last:
  105.             last = None
  106.  
  107.         temp = last
  108.         d = None
  109.  
  110.         # if last node is to be deleted
  111.         if last.data == key:
  112.  
  113.             # find the node before the last node
  114.             while temp.next != last:
  115.                 temp = temp.next
  116.  
  117.             # point temp node to the next of last i.e. first node
  118.             temp.next = last.next
  119.             last = temp.next
  120.  
  121.         # travel to the node to be deleted
  122.         while temp.next != last and temp.next.data != key:
  123.             temp = temp.next
  124.  
  125.         # if node to be deleted was found
  126.         if temp.next.data == key:
  127.             d = temp.next
  128.             temp.next = d.next
  129.  
  130.         return last
  131.  
  132.     def traverse(self):
  133.         if self.last is None:
  134.             print("The list is empty")
  135.             return
  136.  
  137.         new_node = self.last.next
  138.         while new_node:
  139.             print(new_node.data, end=" ")
  140.             new_node = new_node.next
  141.             if new_node == self.last.next:
  142.                 break
  143.  
  144.  
  145. if __name__ == "__main__":
  146.     circular = CircularLinkedList()
  147.  
  148.     # add node to the circular linked list
  149.     last = circular.add_to_empty("Glen")
  150.     last = circular.add_end("Espe")
  151.     last = circular.add_front("Ralph")
  152.     last = circular.add_after("Frieda", "Ralph")
  153.  
  154.     print("The node of the circular linked list: ")
  155.     circular.traverse()
  156.     print()
  157.  
  158.     # delete last node of the circular linked list
  159.     last = circular.delete_node(last, "Espe")
  160.     print("\nThe node of the circular linked list after deletion: ")
  161.     circular.traverse()
  162.    
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement