Skip to content

Commit 03ce397

Browse files
committed
Added queue implementation
1 parent 578d988 commit 03ce397

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

pygorithm/data_structures/queue.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Author: OMKAR PATHAK
2+
# Created On: 3rd August July 2017
3+
4+
# queue implementation
5+
class Queue(object):
6+
'''
7+
8+
size : return the current size of the queue
9+
enqueue : insert an item into the queue
10+
dequeue : remove an item from the queue which was first inserted
11+
isEmpty : check if the queue is empty
12+
13+
'''
14+
15+
def __init__(self, limit = 10):
16+
'''
17+
@param: limit: queue size
18+
'''
19+
self.queue = []
20+
self.front = None
21+
self.rear = None
22+
self.limit = limit
23+
self.size = 0
24+
25+
# for printing the contents of the queue
26+
def __str__(self):
27+
return ' '.join([str(i) for i in self.queue])
28+
29+
# to check if queue is empty
30+
def isEmpty(self):
31+
return self.size <= 0
32+
33+
# to add an element from the rear end of the queue
34+
def enqueue(self, data):
35+
if self.size >= self.limit:
36+
return -1 # queue overflow
37+
else:
38+
self.queue.append(data)
39+
40+
# assign the rear as size of the queue and front as 0
41+
if self.front is None:
42+
self.front = self.rear = 0
43+
else:
44+
self.rear = self.size
45+
46+
self.size += 1
47+
48+
# to pop an element from the front end of the queue
49+
def dequeue(self):
50+
if self.isEmpty():
51+
return -1 # queue underflow
52+
else:
53+
self.size -= 1
54+
if self.size == 0:
55+
self.front = self.rear = 0
56+
else:
57+
self.rear = self.size - 1
58+
return self.queue.pop(0)
59+
60+
# return the size of the queue
61+
def size(self):
62+
return self.size
63+
64+
# easily retrieve the source code of the Queue class
65+
def get_code(self):
66+
import inspect
67+
return inspect.getsource(Queue)
68+
69+
class Deque(object):
70+
'''
71+
72+
isEmpty : checks whether the deque is empty
73+
isFull : checks whether the deque is full
74+
insertRear : inserts an element at the rear end of the deque
75+
insertFront : inserts an element at the front end of the deque
76+
deleteRear : deletes an element from the rear end of the deque
77+
deleteFront : deletes an element from the front end of the deque
78+
79+
'''
80+
81+
def __init__(self, limit = 10):
82+
self.queue = []
83+
self.limit = limit
84+
85+
def __str__(self):
86+
return ' '.join([str(i) for i in self.queue])
87+
88+
# check if queue is empty
89+
def isEmpty(self):
90+
return len(self.queue) <= 0
91+
92+
# check if queue is full
93+
def isFull(self):
94+
return len(self.queue) >= self.limit
95+
96+
# for inserting at rear
97+
def insertRear(self, data):
98+
if self.isFull():
99+
return
100+
else:
101+
self.queue.insert(0, data)
102+
103+
# for inserting at front end
104+
def insertFront(self, data):
105+
if self.isFull():
106+
return
107+
else:
108+
self.queue.append(data)
109+
110+
# deleting from rear end
111+
def deleteRear(self):
112+
if self.isEmpty():
113+
return
114+
else:
115+
return self.queue.pop(0)
116+
117+
# deleting from front end
118+
def deleteFront(self):
119+
if self.isFull():
120+
return
121+
else:
122+
return self.queue.pop()

0 commit comments

Comments
 (0)