-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.py
More file actions
36 lines (34 loc) · 1.19 KB
/
Queue.py
File metadata and controls
36 lines (34 loc) · 1.19 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
import Node as nd
# Queue class has a head and tail variables
class Queue:
def __init__(self):
self.head = None
self.tail = None
# adding elements to the end of the queue
def enqueue(self, data):
# checking if the queue is empty
# if the queue is empty
if self.tail is None and self.head is None:
# head and tail is assigned to new node
self.tail = self.head = nd.Node(data, None)
return
# else, new node is added to the next node after tail
self.tail.next = nd.Node(data, None)
# tail is set to new node
self.tail = self.tail.next
return
# removing elements from the front of the queue
def dequeue(self):
# checking if the queue is empty
# if the queue is empty, None is returned
if self.head is None:
return None
# head is assigned to popped_value variable
popped_value = self.head
# setting head to next node
self.head = self.head.next
# if head is None, tail is also set to None
if self.head is None:
self.tail = None
# 'popped_value' is returned
return popped_value