Created
June 21, 2021 01:40
-
-
Save tpugh/86ca8c54a019235a223aeaacda9e1aab to your computer and use it in GitHub Desktop.
Reversing a Queue
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
# Python3 program to reverse a queue | |
from queue import Queue | |
# Utility function to print the queue | |
def Print(queue): | |
while (not queue.empty()): | |
print(queue.queue[0], end = ", ") | |
queue.get() | |
# Function to reverse the queue | |
def reversequeue(queue): | |
Stack = [] | |
while (not queue.empty()): | |
Stack.append(queue.queue[0]) | |
queue.get() | |
while (len(Stack) != 0): | |
queue.put(Stack[-1]) | |
Stack.pop() | |
# Driver code | |
if __name__ == '__main__': | |
queue = Queue() | |
queue.put(10) | |
queue.put(20) | |
queue.put(30) | |
queue.put(40) | |
queue.put(50) | |
queue.put(60) | |
queue.put(70) | |
queue.put(80) | |
queue.put(90) | |
queue.put(100) | |
reversequeue(queue) | |
Print(queue) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment