-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.py
29 lines (23 loc) · 878 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
# A class that implements the queue data structure using python list
class Queue:
# constructor initiating an empty list as queue
def __init__(self):
self.items = []
# places a new item at the back of queue
def enqueue(self, item):
self.items.insert(0, item)
# removes the frontmost item from queue and returns it
def dequeue(self):
return self.items.pop()
# returns True if the queue is empty, False otherwise
def is_empty(self):
return self.items == []
# returns the number of items present in queue
def size(self):
return len(self.items)
# returns a list containing all the elements of the queue in the reverse order
def traverse(self):
q_items = []
for item in self.items:
q_items.append(item)
return q_items