-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueue.py
42 lines (26 loc) · 881 Bytes
/
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
from typing import Optional, List
class Element:
def __init__(self, value: int) -> None:
self.value = value
def __str__(self) -> str:
return f'<Element(value={self.value})>'
class Queue:
def __init__(self, head: Optional[Element] = None) -> None:
self.storage: List[Element] = [head]
def enqueue(self, new_element: Element) -> None:
self.storage.append(new_element)
def peek(self):
return self.storage[0]
def dequeue(self):
return self.storage.pop(0)
if __name__ == "__main__":
first_el = Element(1)
second_el = Element(2)
third_el = Element(3)
queue = Queue(first_el)
queue.enqueue(second_el)
queue.enqueue(third_el)
assert queue.peek() is first_el
assert queue.dequeue() is first_el
assert queue.dequeue() is second_el
assert queue.peek() is third_el