Shuvit game master repo. http://shuvit.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DList.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. """ A node is a container which encapsulates any type of data.
  2. The node object also has a reference to the NODE preceding and following
  3. said object."""
  4. class Node(object):
  5. def __init__(self, data, prev, next):
  6. self.data = data
  7. self.prev = prev
  8. self.next = next
  9. def getNext(self):
  10. if self.next is not None:
  11. return self.next
  12. print('There\'s nothing next, trying prev')
  13. if self.prev is not None:
  14. return self.prev
  15. print('There\s nothing before either, just this one')
  16. return self
  17. def getPrev(self):
  18. if self.prev is not None:
  19. return self.prev
  20. print('There\'s nothing before, trying next')
  21. if self.next is not None:
  22. return self.next
  23. print('There\'s nothing next either, just this one')
  24. return self
  25. def getData(self):
  26. return self.data
  27. """ A doubly-linked list implementation
  28. NOTE: the getters and setters return a reference to a Node object,
  29. inside which is the data you're probably looking for. If you want to
  30. access data, remember; after you call a get or find, use .data"""
  31. class DoubleList(object):
  32. head = None
  33. tail = None
  34. length = 0;
  35. """ Add a node to the list """
  36. def append(self, data):
  37. new_node = Node(data, None, None)
  38. if self.head is None:
  39. self.head = self.tail = new_node
  40. else:
  41. new_node.prev = self.tail
  42. new_node.next = None
  43. self.tail.next = new_node
  44. self.tail = new_node
  45. self.length = self.length + 1
  46. """ Find a node in the list, then remove it """
  47. def remove(self, node):
  48. current_node = self.head
  49. while current_node is not None:
  50. if current_node == node:
  51. # if it's not the first element
  52. if current_node.prev is not None:
  53. current_node.prev.next = current_node.next
  54. current_node.next.prev = current_node.prev
  55. # if its the last element
  56. elif current_node.next is None:
  57. self.tail = current_node.prev
  58. self.tail.prev = current_node.prev.prev
  59. self.tail.next = None
  60. else:
  61. # otherwise we have no prev (it's None), head is the next one, and prev becomes None
  62. self.head = current_node.next
  63. current_node.next.prev = None
  64. current_node = current_node.next
  65. self.length = self.length - 1
  66. def show(self):
  67. print("Show list data:")
  68. current_node = self.head
  69. while current_node is not None:
  70. print(current_node.data)
  71. current_node = current_node.next
  72. print("*"*50)
  73. """ Find a node in the list by value comparison """
  74. def findNode(self, node_value):
  75. current_node = self.head
  76. while current_node is not None:
  77. if current_node.data == node_value:
  78. return current_node
  79. current_node = current_node.next
  80. """ Get a node by index position """
  81. def get(self, index):
  82. if index > self.length:
  83. return none
  84. elif index == 1:
  85. return self.head
  86. elif index == self.length:
  87. return self.tail
  88. else:
  89. current_node = self.head
  90. pos = 1
  91. while pos < index:
  92. current_node = current_node.next
  93. pos = pos + 1
  94. return current_node
  95. """ Checks if a node is in this list """
  96. def contains(self, node):
  97. current_node = self.head
  98. while current_node is not None:
  99. if current_node == node:
  100. return true
  101. current_node = current_node.next
  102. return false
  103. """ Get the length of this list """
  104. def getLength(self):
  105. return self.length
  106. """ Get the head of this list """
  107. def getHead(self): # get head (;
  108. return self.head
  109. """ Get the tail of this list """
  110. def getTail(self):
  111. return self.tail