Skip to content

Instantly share code, notes, and snippets.

@RafaelBroseghini
Created November 21, 2018 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RafaelBroseghini/2786953804dedaa73243b7fce61885c8 to your computer and use it in GitHub Desktop.
Save RafaelBroseghini/2786953804dedaa73243b7fce61885c8 to your computer and use it in GitHub Desktop.
Implement a Queue using Two Stacks
"""
Stack and Queue being implemented with Python List.
Both structures follow the design below:
Bottom -> [...] -> Top.
"""
def dequeue(array: list) -> int:
return array.pop()
def empty_one_fill_the_other(stack1: list, stack2:list):
while len(stack1) > 0:
value = stack1.pop()
stack2.append(value)
return stack2
def main():
stck_one = [1,2,3]
stck_two = []
queue = empty_one_fill_the_other(stck_one, stck_two)
# After this, we should re do the same step in the other
# direction, to keep the initial state intact.
# Should be equal to 1.
print(dequeue(queue))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment