-
Notifications
You must be signed in to change notification settings - Fork 434
Expand file tree
/
Copy pathP05_QueueImplementationUsingTwoStacks.py
More file actions
49 lines (36 loc) · 1.04 KB
/
P05_QueueImplementationUsingTwoStacks.py
File metadata and controls
49 lines (36 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 StackedQueue:
def __init__(self):
self.stack = Stack()
self.alternateStack = Stack()
def enqueue(self, item):
while(not self.stack.is_empty()):
self.alternateStack.push(self.stack.pop())
self.alternateStack.push(item)
while(not self.alternateStack.is_empty()):
self.stack.push(self.alternateStack.pop())
def dequeue(self):
return self.stack.pop()
def __repr__(self):
return repr(self.stack)
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def size(self):
return len(self.items)
def is_empty(self):
return self.items == []
def __repr__(self):
return str(self.items)
if __name__ == "__main__":
structure = StackedQueue()
structure.enqueue(4)
structure.enqueue(3)
structure.enqueue(2)
structure.enqueue(1)
print(structure)
structure.dequeue()
print(structure)