Skip to content

Instantly share code, notes, and snippets.

@joxer
Created March 11, 2019 18:51
Show Gist options
  • Save joxer/47f09707643bf0acef653ebe57fb7fd5 to your computer and use it in GitHub Desktop.
Save joxer/47f09707643bf0acef653ebe57fb7fd5 to your computer and use it in GitHub Desktop.
# queue using two stacks
class MyQueue:
_stack1 = list()
_stack2 = list()
def push(self, value):
while( len(self._stack2) != 0):
self._stack1.append(self._stack2.pop())
self._stack1.append(value)
def pop(self):
while( len(self._stack1) != 0):
self._stack2.append(self._stack1.pop())
value = self._stack2.pop(0)
return value
queue = MyQueue()
queue.push(3)
queue.push(2)
queue.push(10)
queue.push(4)
queue.push(6)
queue.push(39)
print(queue.pop())
print(queue.pop())
print(queue.pop())
print(queue.pop())
queue.push(44)
print(queue.pop())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment