Last active
December 31, 2015 04:19
-
-
Save cli248/7933690 to your computer and use it in GitHub Desktop.
Implement the Queue data structure using two Stacks
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
class Queue(): | |
def __init__(self): | |
self.inbox = [] | |
self.outbox = [] | |
def enqueue(self, item): | |
self.inbox.append(item) | |
def dequeue(self): | |
if not self.outbox: | |
while self.inbox: | |
self.outbox.append(self.inbox.pop()) | |
try: | |
return self.outbox.pop() | |
except IndexError: | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment