Advertisement
sanjeed2134

Linked List

Apr 16th, 2021 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. # Linked Lists Exercise - My Answer
  2. import time
  3. import os
  4.  
  5.  
  6. class Node:
  7.     def __init__(self, contents=None, next=None):
  8.         self.contents = contents
  9.         self.next = next
  10.  
  11.     def get_contents(self):
  12.         return self.contents
  13.  
  14.     def __str__(self):
  15.         return str(self.contents)
  16.  
  17.  
  18. def print_list(node):
  19.     while node:
  20.         print(node.get_contents())
  21.         node = node.next
  22.     print()
  23.  
  24.  
  25. def add_to_list():
  26.     print("Current linked list:")
  27.     print_list(node1)
  28.     try:
  29.         print("If you wish to select a different option enter BACK")
  30.         node_location = input("Enter the location you desire you data to be: ")
  31.         if node_location == "BACK":
  32.             print("Returning to menu...")
  33.             time.sleep(1)
  34.             os.system('cls')
  35.             return main()
  36.         node_location = int(node_location)
  37.     except:
  38.         print("This should be a number!")
  39.         return add_to_list()
  40.     temp = "node" + str(node_location)
  41.     temp1 = "node" + str(node_location - 1)
  42.     added_node = Node(input("Enter the desired data: "))
  43.     try:
  44.         globals()[temp1].next = added_node
  45.         added_node.next = globals()[temp]
  46.     except:
  47.         pass
  48.     print("The linked list now:")
  49.     if (node_location - 1) == 0:
  50.         added_node.next = node1
  51.         print_list(added_node)
  52.     else:
  53.         print_list(node1)
  54.     os.system('cls')
  55.     return main()
  56.  
  57.  
  58. def delete_node():
  59.     print("Current linked list:")
  60.     print_list(node1)
  61.     try:
  62.         print("If you wish to select a different option enter BACK")
  63.         location = input("Enter the location you want to delete: ")
  64.         if location == "BACK":
  65.             print("Returning to menu...")
  66.             time.sleep(1)
  67.             os.system('cls')
  68.             return main()
  69.         location = int(location)
  70.     except:
  71.         print("This should be a number!")
  72.         return delete_node()
  73.     temp = "node" + str(location)
  74.     if globals()[temp] is None:
  75.         print("This node does not exist")
  76.         return delete_node()
  77.     temp1 = "node" + str(location - 1)
  78.     temp2 = "node" + str(location + 1)
  79.     print("deleting:", globals()[temp].contents, "\n")
  80.     globals()[temp1].next = globals()[temp2]
  81.     os.system('cls')
  82.     return main()
  83.  
  84.  
  85. def main():
  86.     print("Current linked list:")
  87.     print_list(node1)
  88.     decision = input("Do you wish to add or delete: ")
  89.     os.system('cls')
  90.     if decision == "add":
  91.         add_to_list()
  92.     elif decision == "delete":
  93.         delete_node()
  94.     else:
  95.         print("Invalid input")
  96.         return main()
  97.  
  98.  
  99. node0 = None
  100. node1 = Node("Bob")
  101. node2 = Node("Car")
  102. node3 = Node("Sky")
  103. node4 = Node("Skidipap")
  104. node1.next = node2
  105. node2.next = node3
  106. node3.next = node4
  107. main()
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement