-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path3.5.stack_queue.py
More file actions
46 lines (39 loc) · 864 Bytes
/
Copy path3.5.stack_queue.py
File metadata and controls
46 lines (39 loc) · 864 Bytes
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
class Queue:
def __init__(self):
self.oldStack = []
self.newStack = []
def __str__(self):
stack = self.oldStack + self.newStack[::-1]
if stack:
return ' -> '.join(str(i) for i in stack)
else:
return 'Empty Stack!'
def _shiftStack(self):
if self.oldStack == []:
self.oldStack = self.newStack[::-1]
self.newStack = []
def append(self, data):
self.newStack.append(data)
def dequeue(self):
self._shiftStack()
if self.oldStack:
return self.oldStack.pop()
else:
return None
def peek(self):
self._shiftStack()
return self.oldStack[-1]
if __name__ == '__main__':
q = Queue()
q.append(1)
q.append(2)
print 'peek!'
print q.peek()
q.append(3)
print q.peek()
print q.dequeue()
print q
print q.dequeue()
print q.dequeue()
print q.dequeue()
print q