Skip to content

Commit 0fee1c8

Browse files
committed
Queue Reversal
1 parent 2f04136 commit 0fee1c8

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Problem Link: https://practice.geeksforgeeks.org/problems/queue-reversal/1
3+
4+
Given a Queue Q containing N elements. The task is to reverse the Queue.
5+
Your task is to complete the function rev(), that reverses the N elements of the queue.
6+
7+
Input Format:
8+
The first line of input contains an integer T denoting the Test cases. Then T test cases follow.
9+
The first line contains N which is the number of elements which will be reversed.
10+
Second line contains N space seperated elements.
11+
12+
Output Format:
13+
For each testcase, in a new line, print the reversed queue.
14+
15+
Your Task:
16+
This is a function problem. You only need to complete the function rev that takes a queue as
17+
parameter and returns the reversed queue. The printing is done automatically by the driver code.
18+
19+
Constraints:
20+
1 ≤ T ≤ 100
21+
1 ≤ N ≤ 105
22+
1 ≤ elements of Queue ≤ 105
23+
24+
Example:
25+
Input :
26+
2
27+
6
28+
4 3 1 10 2 6
29+
4
30+
4 3 2 1
31+
32+
Output :
33+
6 2 10 1 3 4
34+
1 2 3 4
35+
"""
36+
if __name__=='__main__':
37+
t=int(input())
38+
for i in range(t):
39+
n=int(input())
40+
q=list(map(int,input().split()))
41+
reverseQueue(q)
42+
43+
''' This is a function problem.You only need to complete the function given below '''
44+
#User function Template for python3
45+
def reverseQueue(q):
46+
#add code here
47+
stack = []
48+
while q:
49+
stack.append(q.pop(0))
50+
while stack:
51+
q.append(stack.pop())
52+
print(*q)

0 commit comments

Comments
 (0)