Guest User

Untitled

a guest
Jul 17th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. class Node:
  2. def __init__(self, init_data):
  3. self.data = init_data
  4. self.next = None
  5.  
  6. def get_data(self):
  7. return self.data
  8.  
  9. def get_next(self):
  10. return self.next
  11.  
  12. def set_data(self, new_data):
  13. self.data = new_data
  14.  
  15. def set_next(self, new_next):
  16. self.next = new_next
  17.  
  18. def __str__(self):
  19. return str(self.data)
  20.  
  21. prev = None
  22. cur = head
  23. while cur.next is not None:
  24. prev = cur
  25. cur = cur.next
  26. if prev: #in case the head is the tail
  27. prev.next = None
  28.  
  29. class List:
  30. def __init__(self):
  31. self._nodes = None
  32.  
  33. def push(self, node):
  34. node.set_next(self._nodes)
  35. self._nodes = node
  36. return self
  37.  
  38. def pop(self):
  39. if self._nodes is None:
  40. return None
  41.  
  42. temp = self._nodes
  43. self._nodes = temp.get_next()
  44. return temp
  45.  
  46. def __len__(self):
  47. l = 0
  48. n = self._nodes
  49. while n is not None:
  50. n = n.get_next()
  51. l += 1
  52. return l
  53.  
  54. def remove(self, node):
  55. n = self._nodes
  56. p = None
  57. while n is not None:
  58. if n is node:
  59. p.set_next(n.get_next())
  60. n.set_next(None)
  61. return True
  62.  
  63. p = n
  64. n = n.get_next()
  65.  
  66. return False
  67.  
  68. def del_from_end(self):
  69. if self.head is None:
  70. return "No node to delete"
  71. else:
  72. current = self.head
  73.  
  74. while current.next.next is not None:
  75. current = current.next
  76. current.next = None
Add Comment
Please, Sign In to add comment