Skip to content

Commit 8a2ff79

Browse files
yashaswiadyaluabranhe
authored andcommitted
code for dequeue
1 parent 14745bf commit 8a2ff79

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

data-structures/dequeue.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Dequeue:
2+
def __init__(self):
3+
self.items = []
4+
5+
def is_empty(self):
6+
return self.items == []
7+
8+
def append(self, data):
9+
self.items.append(data)
10+
11+
def append_left(self, data):
12+
self.items.insert(0, data)
13+
14+
def pop(self):
15+
return self.items.pop()
16+
17+
def pop_left(self):
18+
return self.items.pop(0)
19+
20+
21+
q = Dequeue()
22+
print('Menu')
23+
print('append <value>')
24+
print('appendleft <value>')
25+
print('pop')
26+
print('popleft')
27+
print('quit')
28+
29+
while True:
30+
do = input('What would you like to do? ').split()
31+
32+
operation = do[0].strip().lower()
33+
if operation == 'append':
34+
q.append(int(do[1]))
35+
elif operation == 'appendleft':
36+
q.append_left(int(do[1]))
37+
elif operation == 'pop':
38+
if q.is_empty():
39+
print('Dequeue is empty.')
40+
else:
41+
print('Popped value from right: ', q.pop())
42+
elif operation == 'popleft':
43+
if q.is_empty():
44+
print('Dequeue is empty.')
45+
else:
46+
print('Popped value from left: ', q.pop_left())
47+
elif operation == 'quit':
48+
break

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy