Guest User

Untitled

a guest
Jan 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. class Node:
  2. def __init__(self, data, next=None, prev=None):
  3. self.data = data
  4. self.next = next
  5. self.prev = prev
  6.  
  7. class LinkedList:
  8. def __init__(self):
  9. self.head = None
  10. self.tail = None
  11.  
  12. def appendNode(self, node):
  13. '''
  14. Adds a node to the end of the list
  15. '''
  16. if not self.head:
  17. #if the first node, set it as the head, and tail of the ll
  18. self.head = node
  19. self.tail = node
  20. node.next = None
  21. node.prev = None
  22. else:
  23. #else set it as the tail of the ll
  24. node.prev = self.tail
  25. node.prev.next = node
  26. self.tail = node
  27.  
  28. def insertNode(self, nodeBefore, nodeAfter, node):
  29. '''
  30. Inserts a node between two other nodes on the list
  31. '''
  32. if not nodeBefore:
  33. self.head = node
  34. node.prev = None
  35. node.next = nodeAfter
  36. nodeAfter.prev = node
  37. elif not nodeAfter:
  38. self.tail = node
  39. node.prev = nodeBefore
  40. node.next = None
  41. nodeBefore.next = node
  42. else:
  43. nodeBefore.next = node
  44. nodeAfter.prev = node
  45. node.prev = nodeBefore
  46. node.next = nodeAfter
  47.  
  48. def searchNodes(self, search):
  49. currentNode = self.head
  50. while currentNode != None:
  51. if currentNode.data == search:
  52. return currentNode
  53. currentNode = currentNode.next
  54. return -1
  55.  
  56. def printNodes(self):
  57. currentNode = self.head
  58. while currentNode != None:
  59. print(currentNode.data)
  60. currentNode = currentNode.next
  61.  
  62. n1 = Node("First Node")
  63. ll = LinkedList()
  64. ll.appendNode(n1)
  65. n2 = Node("Second Node")
  66. ll.appendNode(n2)
  67. n4 = Node("Fourth Node")
  68. ll.appendNode(n4)
  69. print(n4.prev.data)
  70. print(n2.next.data)
  71. print(n2.prev.data)
  72. print(n1.next.data)
  73. n3 = Node("Third Node")
  74. ll.insertNode(n2, n4, n3)
  75. print("These are the nodes: {}, {}, {}, {}".format(
  76. n1.data, n1.next.data, n1.next.next.data, n1.next.next.next.data
  77. ))
  78. print("This is the head and tail of the Linked List: {}, {}".format(
  79. ll.head.data, ll.tail.data
  80. ))
  81. n5 = Node("Fifth Node")
  82. ll.insertNode(None, n1, n5)
  83. print("This is the head and tail of the Linked List: {}, {}".format(
  84. ll.head.data, ll.tail.data
  85. ))
  86. n6 = Node("Sixth Node")
  87. ll.insertNode(ll.tail, None, n6);
  88. print("This is the head and tail of the Linked List: {}, {}".format(
  89. ll.head.data, ll.tail.data
  90. ))
  91. print("These are the nodes: {}, {}, {}, {}, {}, {}".format(
  92. ll.head.data,
  93. ll.head.next.data,
  94. ll.head.next.next.data,
  95. ll.head.next.next.next.data,
  96. ll.head.next.next.next.next.data,
  97. ll.head.next.next.next.next.next.data
  98. ))
  99. print(ll.searchNodes("Fifth Node"))
  100. print(ll.searchNodes("Seventh Node"))
  101. ll.printNodes()
Add Comment
Please, Sign In to add comment