-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpqueue.py
33 lines (26 loc) · 840 Bytes
/
pqueue.py
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
# CS 107, Fall 2018
# Map Class for HaverQuest
class PriorityQueue:
"""
An implementation of a priority queue
"""
def __init__(self):
self.lst = []
# Number of items in the queue
def length(self): return len(self.lst)
# Clear the queue
def clear(self): self.lst.clear()
# Add an element to the priority queue
def add(self,element,priority):
i = 0
while (i < len(self.lst) and self.lst[i][0] > priority):
i += 1
self.lst = self.lst[0:i] + [(priority,element)] + self.lst[i:]
# Remove an element from the queue
def remove(self,element):
i = 0
while (i < len(self.lst) and self.lst[i][1] != element):
i += 1
self.lst = self.lst[0:i] + self.lst[i+1:]
def __iter__(self):
return iter(self.lst)