-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.py
78 lines (51 loc) · 1.55 KB
/
queue.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""FIFO queues implemented as a list.
Warning: this is not the best way to implement a Queue."""
class Queue(object):
"""FIFO queue."""
def __init__(self):
self._list = []
def __repr__(self):
if not self._list:
return "<Queue (empty)>"
else:
return "<Queue %s>" % self._list
def dequeue(self):
"""Remove item from front of queue and return it."""
return self._list.pop(0)
def length(self):
"""Return length of queue."""
return len(self._list)
def empty(self):
"""Empty queue."""
remaining = self._list
self._list = []
return remaining
def is_empty(self):
"""Is queue empty?"""
return not self._list
def enqueue(self, item):
"""Add item to end of queue::
>>> q = Queue()
>>> q.enqueue("buy flight")
>>> q.enqueue("pack")
>>> q.enqueue("enjoy vacation")
>>> q
<Queue ['buy flight', 'pack', 'enjoy vacation']>
>>> q.length
3
"""
# FIXME
self._list.append(item)
def peek(self):
"""Return but don't remove the first item in the queue.
>>> q = Queue()
>>> q.enqueue("buy flight")
>>> q.enqueue("pack")
>>> q.enqueue("enjoy vacation")
>>> q.peek()
'buy flight'
>>> q
<Queue ['buy flight', 'pack', 'enjoy vacation']>
"""
# FIXME
return self._list[0]