-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.py
More file actions
55 lines (35 loc) · 1.39 KB
/
Copy pathqueue.py
File metadata and controls
55 lines (35 loc) · 1.39 KB
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
from data_structures.linked_list import Node
from data_structures.invalid_operation_error import InvalidOperationError
class Queue:
def __init__(self):
self.front = None
self.rear = None
def enqueue(self, value):
#adds a new node with that value to the back of the queue with the provided value
new_node = Node(value)
#if queue is empty, add new node to the front
if self.rear is None:
self.front = new_node
self.rear = new_node
#if queue is not empty, add new node to the rear
else:
self.rear.next = new_node
self.rear = new_node
def dequeue(self):
#removes the node from the front of the queue, and returns the node’s value.
if self.front is None:
raise InvalidOperationError("Method not allowed on empty collection")
#get the value of the front
dequeue_value = self.front.value
#move the pointer to its next node
self.front = self.front.next
#queue is empty, so need to update rear - edge case
if self.front is None:
self.rear = None
return dequeue_value
def peek(self):
if self.front is None:
raise InvalidOperationError("Method not allowed on empty collection")
return self.front.value
def is_empty(self):
return self.front is None