Advertisement
techpaste222

DoubleList

Dec 3rd, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. class Node:
  2. def __init__(self, data):
  3. self.previous = None
  4. self.data = data
  5. self.next = None
  6. class DoublyLinkedList:
  7. def __init__(self):
  8. self.head = None
  9. self.start_node = None
  10. self.last_node = None
  11. def append(self, data):
  12. if self.last_node is None:
  13. self.head = Node(data)
  14. self.last_node = self.head
  15. else:
  16. new_node = Node(data)
  17. self.last_node.next = new_node
  18. new_node.previous = self.last_node
  19. new_node.next = None
  20. self.last_node = new_node
  21. def display(self, Type):
  22. if Type == "Left->Right":
  23. current = self.head
  24. while current is not None:
  25. print(current.data, end = ' ')
  26. current = current.next
  27. print()
  28. else:
  29. current = self.last_node
  30. while current is not None:
  31. print(current.data, end = ' ')
  32. current = current.previous
  33. print()
  34.  
  35. if __name__ == "__main__":
  36. L = DoublyLinkedList()
  37. L.append(1)
  38. L.append("Nishat")
  39. L.append("9")
  40. L.append(5)
  41. L.display("Left->Right")
  42. L.display("Right->Left")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement