-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinkedpriorityqueue.py
More file actions
33 lines (29 loc) · 1.16 KB
/
linkedpriorityqueue.py
File metadata and controls
33 lines (29 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from node import Node
from linkedqueue import LinkedQueue
class LinkedPriorityQueue(LinkedQueue):
"""A link-based priority queue implementation."""
def __init__(self, sourceCollection=None):
"""Sets the initial state of self, which includes the
content of sourceCollection, if it's present."""
LinkedQueue(self, sourceCollection)
def add(self, newItem):
"""Inserts newItem after items of greater or equal
priority or ahead of items of lesser priority.
A has greater priority than B if A < B."""
if self.isEmpty() or newItem >= self._rear.data:
# New item goes at rear
LinkedQueue.add(self, newItem)
else:
# Search for a position where it's less
probe = self._front
while newItem >= probe.data:
trailer = probe
probe = probe.next
newNode = Node(newItem, probe)
if probe == self._front:
# New item gose at front
self._front = newNode
else:
# New item goes between two nodes
trailer.next = newNode
self._size += 1