-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue_Stacks.py
37 lines (25 loc) · 891 Bytes
/
Queue_Stacks.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
class MyQueue:
#Implementing queue using stacks.
def __init__(self):
self.in_stack=[]
self.out_stack=[]
def push(self,value):
for i in range(len(self.out_stack)):
self.in_stack.append(self.out_stack.pop())
self.in_stack.append(value)
return self.in_stack
def pop(self):
for i in range(len(self.in_stack)):
self.out_stack.append(self.in_stack.pop())
item=self.out_stack.pop()
return item
def peek(self):
if len(self.out_stack)>0:
return self.out_stack[-1]
elif len(self.in_stack)>0:
return self.in_stack[0]
def empty(self):
if len(self.in_stack)==0 and len(self.out_stack)==0:
return True
else:
return False