-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDeque_Implementation.py
58 lines (42 loc) · 1.96 KB
/
Deque_Implementation.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
58
__author__ = 'Sanjay'
# A deque, also known as a double-ended queue, is an ordered collection of items similar to the queue.
# It has two ends, a front and a rear, and the items remain positioned in the collection.
# What makes a deque different is the unrestrictive nature of adding and removing items.
# New items can be added at either the front or the rear.
# Likewise, existing items can be removed from either end.
# it does not require the LIFO and FIFO orderings that are enforced by those data structures.
# It is up to you to make consistent use of the addition and removal operations.
# Deque() creates a new deque that is empty. It needs no parameters and returns an empty deque.
# addFront(item) adds a new item to the front of the deque. It needs the item and returns nothing.
# addRear(item) adds a new item to the rear of the deque. It needs the item and returns nothing.
# removeFront() removes the front item from the deque. It needs no parameters and returns the item. The deque is modified.
# removeRear() removes the rear item from the deque. It needs no parameters and returns the item. The deque is modified.
# isEmpty() tests to see whether the deque is empty. It needs no parameters and returns a boolean value.
# size() returns the number of items in the deque. It needs no parameters and returns an integer.
class Deque:
def __init__(self):
self.items = []
def addFront(self,item):
self.items.append(item)
def addRear(self,item):
self.items.insert(0,item)
def removeFront(self):
self.items.pop()
def removeRear(self):
self.items.pop(0)
def isEmpty(self):
return self.items == []
def size(self):
return len(self.items)
if __name__ == '__main__':
d=Deque()
print(d.isEmpty())
d.addRear(4)
d.addRear('dog')
d.addFront('cat')
d.addFront(True)
print(d.size())
print(d.isEmpty())
d.addRear(8.4)
print(d.removeRear())
print(d.removeFront())