-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo_stacks_one_queue.py
More file actions
64 lines (49 loc) · 1.38 KB
/
two_stacks_one_queue.py
File metadata and controls
64 lines (49 loc) · 1.38 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
56
57
58
59
60
61
62
63
64
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
if not self.items: return None
return self.items[len(self.items)-1]
# Queue implemented with 2 stacks
class SpecialQueue(Stack):
def __init__(self):
self.in_stack = Stack()
self.out_stack = Stack()
def enqueue(self, item):
self.in_stack.push(item)
def dequeue(self):
if not self.out_stack.isEmpty():
return self.out_stack.pop()
while not self.in_stack.isEmpty():
self.out_stack.push(self.in_stack.pop())
return self.out_stack.pop()
class MaxStack(Stack):
def __init__(self):
self.items = []
self.max_vals = Stack()
def push(self, item):
self.items.append(item)
if self.max_vals.peek() < item:
self.max_vals.push(item)
def pop(self):
if self.max_vals == self.peek():
self.max_vals.pop()
return self.items.pop()
def get_max(self):
return self.max_vals.peek()
if __name__ == "__main__":
sq = SpecialQueue()
sq.enqueue(8)
sq.enqueue(2)
ms = MaxStack()
ms.push(1)
ms.push(7)
ms.push(8)
ms.push(3)
print ms.get_max()