-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2StacksQueue.py
More file actions
49 lines (34 loc) · 1.04 KB
/
Copy path2StacksQueue.py
File metadata and controls
49 lines (34 loc) · 1.04 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
class Queue:
def __init__(self):
self.pushstack = []
self.popstack = []
def push(self, x: int) -> None:
self.pushstack.append(x)
def pop(self) -> int:
self.peek()
return self.popstack.pop()
def peek(self) -> int:
if not self.popstack:
while self.pushstack:
self.popstack.append(self.pushstack.pop())
return self.popstack[-1]
def empty(self) -> bool:
return not self.pushstack and not self.popstack
class QueuePush:
def __init__(self):
self.stack = []
self.stack_two = []
def push(self, x: int) -> None:
while self.stack:
top = self.stack.pop()
self.stack_two.append(top)
self.stack.append(x)
while self.stack_two:
top = self.stack_two.pop()
self.stack.append(top)
def pop(self) -> int:
return self.stack.pop()
def peek(self) -> int:
return self.stack[-1]
def empty(self) -> bool:
return not self.stack