-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueasstack.py
More file actions
55 lines (44 loc) · 1.02 KB
/
queasstack.py
File metadata and controls
55 lines (44 loc) · 1.02 KB
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
class StackQueue:
def __init__(self):
self.val = None
self.que = []
self.d_q = []
# No return Value
def push(self,val):
self.val = val
self.que.append(self.val)
# Removed
def like_pop(self):
# Pop ffrom Empty stack :
if len(self.que) == 0 :
return('Pop from Empty Stack cant happen')
i = 0
# If length of queue is 1
if len(self.que) == 1 :
return(self.que.pop())
if len(self.d_q) == 0 :
while(len(self.que)==1):
self.d_q.append(self.que[i])
self.que.remove(self.que[i])
if len(self.d_q) == 0 :
ret_val = self.que.pop()
# If pop is successfully done
if len(self.que) == 0:
for i in range(len(self.d_q)):
self.que.append(self.d_q.remove(i))
return(ret_val)
# returns element
def peek(self):
return(self.que[0])
a = StackQueue()
a.push(1)
a.push(2)
a.push(3)
a.push(4)
print(a.que)
print(a.like_pop())
print(a.like_pop())
print(a.like_pop())
a.push(44)
print(a.like_pop())
print(a.like_pop())