-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.py
More file actions
39 lines (31 loc) · 694 Bytes
/
queue.py
File metadata and controls
39 lines (31 loc) · 694 Bytes
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
# Queue implementation using a list data structure
class Queue:
def __init__(self, head=None):
self.storage = [head]
def enqueue(self, new_element):
self.storage.insert(0, new_element)
def peek(self):
return self.storage[len(self.storage) - 1]
def dequeue(self):
return self.storage.pop()
# Setup
q = Queue(1)
q.enqueue(2)
q.enqueue(3)
# Test peek
# Output should be 1
print(q.peek())
# Test dequeue
# Output should be 1
print(q.dequeue())
# Test enqueue
q.enqueue(4)
# Output should be 2
print(q.dequeue())
# Output should be 3
print(q.dequeue())
# Output should be 4
print(q.dequeue())
q.enqueue(5)
# Output should be 5
print(q.peek())