-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" | ||
As the title described, you should only use two stacks to implement a queue's actions. | ||
The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue. | ||
Both pop and top methods should return the value of first element. | ||
Example | ||
For push(1), pop(), push(2), push(3), top(), pop(), you should return 1, 2 and 2 | ||
Challenge | ||
implement it by two stacks, do not use any other data structure and push, pop and top should be O(1) by AVERAGE. | ||
""" | ||
__author__ = 'Danyang' | ||
class Queue: | ||
def __init__(self): | ||
self.stack1 = [] # for in | ||
self.stack2 = [] # for out | ||
|
||
def push(self, element): | ||
self.stack1.append(element) | ||
|
||
def top(self): | ||
if not self.stack2: | ||
while self.stack1: | ||
self.stack2.append(self.stack1.pop()) | ||
return self.stack2[-1] | ||
|
||
def pop(self): | ||
if not self.stack2: | ||
while self.stack1: | ||
self.stack2.append(self.stack1.pop()) | ||
return self.stack2.pop() | ||
|