forked from OmkarPathak/Data-Structures-using-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deque.py
57 lines (48 loc) · 1.32 KB
/
Deque.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Author: OMKAR PATHAK
class Deque(object):
def __init__(self, limit = 10):
self.queue = []
self.limit = limit
def __str__(self):
return ' '.join([str(i) for i in self.queue])
# check if queue is empty
def isEmpty(self):
return len(self.queue) <= 0
# check if queue is full
def isFull(self):
return len(self.queue) >= self.limit
# for inserting at rear
def insertRear(self, data):
if self.isFull():
return
else:
self.queue.insert(0, data)
# for inserting at front end
def insertFront(self, data):
if self.isFull():
return
else:
self.queue.append(data)
# deleting from rear end
def deleteRear(self):
if self.isEmpty():
return
else:
return self.queue.pop(0)
# deleting from front end
def deleteFront(self):
if self.isFull():
return
else:
return self.queue.pop()
if __name__ == '__main__':
myDeque = Deque()
myDeque.insertFront(1) # 1
myDeque.insertRear(2) # 2 1
myDeque.insertFront(3) # 2 1 3
myDeque.insertRear(10) #10 2 1 3
print(myDeque)
myDeque.deleteRear() # 2 1 3
print(myDeque)
myDeque.deleteFront() # 2 1
print(myDeque)