Created
November 21, 2018 04:52
-
-
Save RafaelBroseghini/2786953804dedaa73243b7fce61885c8 to your computer and use it in GitHub Desktop.
Implement a Queue using Two Stacks
This file contains hidden or 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
""" | |
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