Skip to content

Commit

Permalink
data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
idf committed Dec 21, 2014
1 parent ab48c9a commit adfee35
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Implement Queue by Stacks.py
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()

0 comments on commit adfee35

Please sign in to comment.