Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. """Den ska innehålla klassen UnorderedList.
  2. UnorderedList klassen ska följa klassdiagrammet ovanför.
  3. Minst de metoderna och attributen måste finnas i din implementation.
  4. Det är Ok att lägga till mer. """
  5. import random
  6. from node import Node
  7. from errors import IndexException, ValueException
  8.  
  9. class UnorderedList:
  10. """ Class docstring for list uppgiften """
  11.  
  12. def __init__(self):
  13. """ konstruktor för klass UnorderedList """
  14. self.head = None
  15. self.tail = None
  16.  
  17. def is_empty(self):
  18. """ Metod kollar om massiv Ul är tomt """
  19. return self.head is None
  20.  
  21. def add(self, data):
  22. """ Lägg till nytt element/nod sist i listan.(ett kädjelänken
  23. med data from Node) """
  24. if self.head is None:
  25. self.head = Node(data)
  26. self.tail = self.head
  27. else:
  28. self.tail.next = Node(data)
  29. self.tail = self.tail.next
  30.  
  31. def insert(self, index, data):
  32. """Metod insert Lägg till nytt element/nod på specifikt index.
  33. Om index inte finns lyftas exception. """
  34. counter = 0
  35. previous = None
  36. current = self.head
  37. # next_node = None
  38. if self.head is None:
  39. self.head = Node(data)
  40. self.tail = self.head
  41. return
  42.  
  43. if counter == index:
  44. current = self.head
  45. self.head = Node(data)
  46. self.head.next = current
  47. return
  48.  
  49.  
  50. if self.size() == index:
  51. self.tail.next = Node(data)
  52. self.tail = self.tail.next
  53. return
  54.  
  55. if self.size() > index:
  56. while True:
  57. if counter == index:
  58. if previous and current.next:
  59. previous.next = Node(data)
  60. previous.next.next = current
  61. return
  62. counter += 1
  63. if counter:
  64. previous = current
  65. current = current.next
  66. else:
  67. raise IndexException()
  68.  
  69. def set(self, index, data):
  70. """ Metod set:Skrivas över element med ny data som finns på index.
  71. Om index inte finns lyftas exception. """
  72. counter = 0
  73. current = self.head
  74. if self.head is None:
  75. raise IndexException()
  76.  
  77. if self.size() > index:
  78. while True:
  79.  
  80. if counter == index:
  81. current.data = data
  82.  
  83. if current.next is None:
  84. break
  85. current = current.next
  86. counter += 1
  87. else:
  88. raise IndexException()
  89.  
  90. def size(self):
  91. """Returnera antaler element i listan. """
  92. current = self.head
  93. if current is None:
  94. return 0
  95. count = 1
  96. while current.next != None:
  97. current = current.next
  98. count += 1
  99. return count
  100.  
  101. def get(self, index):
  102. """ Returnerar värde på index. Om index inte finns lyftas exception."""
  103. count = 0
  104. current = self.head
  105.  
  106. while True:
  107.  
  108. if count == index:
  109. return current.data
  110. if current.next is None:
  111. raise IndexException()
  112. current = current.next
  113. count += 1
  114.  
  115. def index_of(self, data):
  116. """ Om data finns i listan returnera dess index.
  117. Om nod med data inte finns lyftas exception. """
  118. current = self.head
  119. index = 0
  120. if current is None:
  121. pass
  122. else:
  123. while True:
  124. if current.data == data:
  125. return index
  126. if current.next is None:
  127. break
  128. current = current.next
  129. index += 1
  130. raise ValueException()
  131.  
  132. def print_list(self):
  133. """ Skrivas ut listans innehåll av alla elementer. """
  134. current = self.head
  135. string = "["
  136. if current != None:
  137. while current.next != None:
  138. string += str(current.data) + ", "
  139. current = current.next
  140. string += str(current.data)
  141. else:
  142. pass
  143. string += "]"
  144. print(string)
  145.  
  146. def remove(self, data):
  147. """ Ta bort nod med samma data.
  148. Om nod med data inte finns lyftas exception. """
  149. counter = 0
  150. previous = None
  151. current = self.head
  152. next_node = None
  153. if current is None:
  154. return
  155.  
  156. while True:
  157. if current.next:
  158. next_node = current.next
  159.  
  160. if current.data == data:
  161. if previous and next_node:
  162. previous.next = next_node
  163. if previous and not current.next:
  164. previous.next = None
  165. if not previous and next_node:
  166. self.head = next_node
  167. return
  168.  
  169. counter += 1
  170.  
  171. if current.next is None:
  172. raise ValueException()
  173. if counter:
  174. previous = current
  175. current = current.next
  176.  
  177. def insertion_sort(self,):
  178. head = self.head
  179. if not head:
  180. return head
  181. dummy = Node(0)
  182. dummy.next = head
  183. lastSorted = head
  184. while lastSorted.next:
  185. if lastSorted.next.data < lastSorted.data:
  186. tmp = dummy
  187. while tmp.next.data < lastSorted.next.data:
  188. tmp = tmp.next
  189. nextToSort = lastSorted.next
  190. lastSorted.next = nextToSort.next
  191. nextToSort.next = tmp.next
  192. tmp.next = nextToSort
  193. else:
  194. lastSorted = lastSorted.next
  195. self.head = dummy.next
  196.  
  197. def bubble_sort(self):
  198. flags = False
  199. current = self.head
  200. if current is None:
  201. return
  202. if current.next is None:
  203. return
  204. while True:
  205. next_node = current.next
  206. if next_node:
  207. if current.data > next_node.data:
  208. temp_curr = current.data
  209. temp_next = next_node.data
  210.  
  211. current.data = temp_next
  212. next_node.data = temp_curr
  213. flags = True
  214. current = current.next
  215.  
  216. if current.next is None:
  217. if flags:
  218. self.bubble_sort()
  219. break
  220.  
  221.  
  222. if __name__ == '__main__':
  223. unolist = UnorderedList()
  224. for _ in range(10):
  225. unolist.add(random.randint(1, 10))
  226. unolist.print_list()
  227. print("=============")
  228. # unolist.bubble_sort()
  229. unolist.insertion_sort()
  230. unolist.print_list()
  231. print(unolist.tail.data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement