-
Notifications
You must be signed in to change notification settings - Fork 0
/
orderedCollections.py
54 lines (42 loc) · 1.49 KB
/
orderedCollections.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import heapq
class Stack:
"""Implements a stack data structure"""
def __init__(self):
self.list = []
def push(self, item):
"""Pushes input item on top of the stack"""
self.list.append(item)
def pop(self):
"""Pops off the item on top of the stack and returns it"""
return self.list.pop()
def isEmpty(self):
"""Returns true if the stack is empty, false otherwise"""
return len(self.list) == 0
class Queue:
"""Implements a FIFO queue data structure"""
def __init__(self):
self.list = []
def push(self, item):
"""Places input item to the end of the queue"""
self.list.insert(0, item)
def pop(self):
"""Removes item from front of queue and returns it"""
return self.list.pop()
def isEmpty(self):
"""Returns true if the queue is empty, false otherwise"""
return len(self.list) == 0
class PriorityQueue:
"""Implements a priority queue data structure"""
def __init__(self):
self.heap = []
def push(self, item, priority):
"""Pushes input item to appropriate location in queue, based on priority"""
entry = (priority, item)
heapq.heappush(self.heap, entry)
def pop(self):
"""Removes item from the front of queue and returns it"""
(_, item) = heapq.heappop(self.heap)
return item
def isEmpty(self):
"""Returns true if the queue is empty, false otherwise"""
return len(self.heap) == 0