-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStackUsingQueues.py
More file actions
55 lines (43 loc) · 1.19 KB
/
Copy pathStackUsingQueues.py
File metadata and controls
55 lines (43 loc) · 1.19 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
from collections import deque
class Stack(object):
def __init__(self):
self.q1 = deque()
self.q2 = deque()
self.current = False
def _current(self):
return self.q1 if self.current else self.q2
def _other(self):
return self.q2 if self.current else self.q1
def push(self, x):
c, o = self._current(), self._other()
while c:
o.appendleft(c.pop())
c.appendleft(x)
def pop(self):
c, o = self._current(), self._other()
if not c:
while len(o) > 1:
c.appendleft(o.pop())
self.current = not self.current
self._current().pop()
def top(self):
c, o = self._current(), self._other()
if not c:
while len(o) > 1:
c.appendleft(o.pop())
self.current = not self.current
v1 = self._current().pop()
self._current().appendleft(v1)
return v1
def empty(self):
return not self.q1 and not self.q2
if __name__ == "__main__":
s = Stack()
s.push(1)
s.push(2)
assert 2 == s.top()
s.pop()
assert 1 == s.top()
s.pop()
s.push(3)
assert 3 == s.top()