Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. class Node:
  2. def __init__(self, cargo=None, next=None):
  3. self.cargo = cargo
  4. self.next = next
  5.  
  6. def __str__(self):
  7. return str(self.cargo)
  8.  
  9. class Queue:
  10. def __init__(self):
  11. self.length = 0
  12. self.head = None
  13.  
  14. def is_empty(self):
  15. return self.length == 0
  16.  
  17. def insert(self, cargo):
  18. node = Node(cargo)
  19. if self.head is None:
  20. # If list is empty the new node goes first
  21. self.head = node
  22. else:
  23. # Find the last node in the list
  24. last = self.head
  25. while last.next:
  26. last = last.next
  27. # Append the new node
  28. last.next = node
  29. self.length += 1
  30.  
  31. def remove(self):
  32. cargo = self.head.cargo
  33. self.head = self.head.next
  34. self.length -= 1
  35. return cargo
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. class ImprovedQueue:
  44. def __init__(self):
  45. self.length = 0
  46. self.head = None
  47. self.last = None
  48.  
  49. def is_empty(self):
  50. return self.length == 0
  51.  
  52. def insert(self, cargo):
  53. node = Node(cargo)
  54. if self.length == 0:
  55. # If list is empty, the new node is head and last
  56. self.head = self.last = node
  57. else:
  58. # Find the last node
  59. last = self.last
  60. # Append the new node
  61. last.next = node
  62. self.last = node
  63. self.length += 1
  64.  
  65. def remove(self):
  66. cargo = self.head.cargo
  67. self.head = self.head.next
  68. self.length -= 1
  69. if self.length == 0:
  70. self.last = None
  71. return cargo
  72.  
  73. X=ImprovedQueue()
  74. X.insert(1)
  75. X.insert(2)
  76. X.insert(15)
  77.  
  78. class PriorityQueue:
  79. def __init__(self):
  80. self.items = []
  81. self.head = None
  82. self.last = None
  83.  
  84. def is_empty(self):
  85. return not self.items
  86.  
  87. def insert(self, node):
  88.  
  89. if self.length == 0:
  90. # If list is empty, the new node is head and last
  91. self.head = self.last = node
  92. else:
  93. # Find the last node
  94. last = self.last
  95. # Append the new node
  96. last.next = node
  97. self.last = node
  98. self.length += 1
  99.  
  100. def remove(self):
  101. maxi = 0
  102. for i in range(1, len(self.items)):
  103. if self.items[i] > self.items[maxi]:
  104. maxi = i
  105. item = self.items[maxi]
  106. del self.items[maxi]
  107. return item
  108. def printqueue(self):
  109. for i in range(0, len(self.items)):
  110. print(self.items[i])
  111.  
  112. def selectionSort(self):
  113.  
  114. for i in range(0, len(self.items)):
  115. positionOfMin=i
  116. for j in range(i+1,len(self.items)):
  117. if self.items[j]<self.items[positionOfMin]:
  118. positionOfMin = j
  119. temp = self.items[i]
  120. self.items[i] = self.items[positionOfMin]
  121. self.items[positionOfMin] = temp
  122.  
  123.  
  124. q = PriorityQueue()
  125. for num in [11, 12, 14, 13]:
  126. q.insert(num)
  127.  
  128. q.selectionSort()
  129. q.printqueue()
  130.  
  131. z = PriorityQueue()
  132. node1 = Node(11)
  133. node2 = Node(12)
  134. node3 = Node(14)
  135. node4 = Node(13)
  136. node5 = Node(10)
  137. z.insert(node1)
  138. z.insert(node2)
  139. z.insert(node3)
  140. z.insert(node4)
  141. z.insert(node5)
  142.  
  143. #z.selectionSort()
  144. #print(z.items)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement