📜  使用另一个队列反转一个队列

📅  最后修改于: 2021-06-25 20:42:54             🧑  作者: Mango

给定一个队列。任务是使用另一个空队列来反转队列。

例子:

Input: queue[] = {1, 2, 3, 4, 5}
Output: 5 4 3 2 1

Input: queue[] = {10, 20, 30, 40}
Output: 40 30 20 10

方法:

  • 给定一个队列和一个空队列。
  • 队列的最后一个元素应该是新队列的第一个元素。
  • 要获取最后一个元素,需要一个一个地弹出队列,并将其添加到队列的末尾,大小为1次。
  • 因此,在那之后,我们将获得队列前面的最后一个元素。现在弹出该元素并将其添加到新队列中。重复步骤s – 1次,其中s是队列的原始大小。

下面是该方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
  
// Function to return the reversed queue
queue reverse(queue q)
{
    // Size of ueue
    int s = q.size();
  
    // Second queue
    queue ans;
  
    for (int i = 0; i < s; i++) {
  
        // Get the last element to the
        // front of queue
        for (int j = 0; j < q.size() - 1; j++) {
            int x = q.front();
            q.pop();
            q.push(x);
        }
  
        // Get the last element and
        // add it to the new queue
        ans.push(q.front());
        q.pop();
    }
    return ans;
}
  
// Driver Code
int main()
{
    queue q;
  
    // Insert elements
    q.push(1);
    q.push(2);
    q.push(3);
    q.push(4);
    q.push(5);
  
    q = reverse(q);
  
    // Print the queue
    while (!q.empty()) {
        cout << q.front() << " ";
        q.pop();
    }
  
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
class GFG
{
  
// Function to return the reversed queue
static Queue reverse(Queue q)
{
    // Size of ueue
    int s = q.size();
  
    // Second queue
    Queue ans = new LinkedList<>();
  
    for (int i = 0; i < s; i++)
    {
  
        // Get the last element to the
        // front of queue
        for (int j = 0; j < q.size() - 1; j++) 
        {
            int x = q.peek();
            q.remove();
            q.add(x);
        }
  
        // Get the last element and
        // add it to the new queue
        ans.add(q.peek());
        q.remove();
    }
    return ans;
}
  
// Driver Code
public static void main(String[] args) 
{
    Queue q = new LinkedList<>();
  
    // Insert elements
    q.add(1);
    q.add(2);
    q.add(3);
    q.add(4);
    q.add(5);
  
    q = reverse(q);
  
    // Print the queue
    while (!q.isEmpty()) 
    {
        System.out.print(q.peek() + " ");
        q.remove();
    }
    }
} 
  
// This code is contributed by Princi Singh


Python3
# Python3 implementation of the above approach
from collections import deque
  
# Function to return the reversed queue
def reverse(q):
      
    # Size of ueue
    s = len(q)
  
    # Second queue
    ans = deque()
  
    for i in range(s):
  
        # Get the last element to the
        # front of queue
        for j in range(s - 1):
            x = q.popleft()
            q.appendleft(x)
  
        # Get the last element and
        # add it to the new queue
        ans.appendleft(q.popleft())
    return ans
  
# Driver Code
q = deque()
  
# Insert elements
q.append(1)
q.append(2)
q.append(3)
q.append(4)
q.append(5)
  
q = reverse(q)
  
# Print the queue
while (len(q) > 0):
    print(q.popleft(), end = " ")
  
# This code is contributed by Mohit Kumar


C#
// C# Program to print the given pattern
using System;
using System.Collections.Generic; 
      
class GFG
{
  
// Function to return the reversed queue
static Queue reverse(Queue q)
{
    // Size of ueue
    int s = q.Count;
  
    // Second queue
    Queue ans = new Queue();
  
    for (int i = 0; i < s; i++)
    {
  
        // Get the last element to the
        // front of queue
        for (int j = 0; j < q.Count - 1; j++) 
        {
            int x = q.Peek();
            q.Dequeue();
            q.Enqueue(x);
        }
  
        // Get the last element and
        // add it to the new queue
        ans.Enqueue(q.Peek());
        q.Dequeue();
    }
    return ans;
}
  
// Driver Code
public static void Main(String[] args) 
{
    Queue q = new Queue();
  
    // Insert elements
    q.Enqueue(1);
    q.Enqueue(2);
    q.Enqueue(3);
    q.Enqueue(4);
    q.Enqueue(5);
  
    q = reverse(q);
  
    // Print the queue
    while (q.Count!=0) 
    {
        Console.Write(q.Peek() + " ");
        q.Dequeue();
    }
    }
}
  
// This code is contributed by Princi Singh


输出:
5 4 3 2 1

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。