Reversing the first K elements of a Queue

Given an integer k and a queue of integers, we need to reverse the order of the first k elements of the queue, leaving the other elements in the same relative order.

Only following standard operations are allowed on queue.

  • enqueue(x) : Add an item x to rear of queue
  • dequeue() : Remove an item from front of queue
  • size(( : Returns number of elements in queue.
  • front() : Finds front item.

Examples:

Input : Q = [10, 20, 30, 40, 50, 60, 
            70, 80, 90, 100]
        k = 5
Output : Q = [50, 40, 30, 20, 10, 60, 
             70, 80, 90, 100]

Input : Q = [10, 20, 30, 40, 50, 60, 
            70, 80, 90, 100]
        k = 4
Output : Q = [40, 30, 20, 10, 50, 60, 
             70, 80, 90, 100]


The idea is to use an auxiliary stack.

1) Create an empty stack.
2) One by one dequeue items from given queue and push the dequeued items to stack.
3) Enqueue the contents of stack at the back of the queue
4) Reverse the whole queue.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to reverse first k elements of a queue.
#include <bits/stdc++.h>
using namespace std;
  
/* Function to reverse the first K elements of the Queue */
void reverseQueueFirstKElements(int k, queue<int>& Queue)
{
    if (Queue.empty() == true || k > Queue.size())
        return;
    if (k <= 0)
        return;
  
    stack<int> Stack;
  
    /* Push the first K elements into a Stack*/
    for (int i = 0; i < k; i++) {
        Stack.push(Queue.front());
        Queue.pop();
    }
  
    /* Enqueue the contents of stack
       at the back of the queue*/
    while (!Stack.empty()) {
        Queue.push(Stack.top());
        Stack.pop();
    }
  
    /* Remove the remaining elements and 
       enqueue them at the end of the Queue*/
    for (int i = 0; i < Queue.size() - k; i++) {
        Queue.push(Queue.front());
        Queue.pop();
    }
}
  
/* Utility Function to print the Queue */
void Print(queue<int>& Queue)
{
    while (!Queue.empty()) {
        cout << Queue.front() << " ";
        Queue.pop();
    }
}
  
// Driver code
int main()
{
    queue<int> Queue;
    Queue.push(10);
    Queue.push(20);
    Queue.push(30);
    Queue.push(40);
    Queue.push(50);
    Queue.push(60);
    Queue.push(70);
    Queue.push(80);
    Queue.push(90);
    Queue.push(100);
  
    int k = 5;
    reverseQueueFirstKElements(k, Queue);
    Print(Queue);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to reverse first k elements 
// of a queue.
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
  
public class Reverse_k_element_queue {
  
    static Queue<Integer> queue;
  
    // Function to reverse the first K elements
    // of the Queue 
    static void reverseQueueFirstKElements(int k) {
        if (queue.isEmpty() == true || k > queue.size())
            return;
        if (k <= 0)
            return;
  
        Stack<Integer> stack = new Stack<Integer>();
  
        // Push the first K elements into a Stack 
        for (int i = 0; i < k; i++) {
            stack.push(queue.peek());
            queue.remove();
        }
  
          
        // Enqueue the contents of stack at the back 
        // of the queue
        while (!stack.empty()) {
            queue.add(stack.peek());
            stack.pop();
        }
  
          
        // Remove the remaining elements and enqueue 
        // them at the end of the Queue
        for (int i = 0; i < queue.size() - k; i++) {
            queue.add(queue.peek());
            queue.remove();
        }
    }
  
    // Utility Function to print the Queue 
    static void Print() {
        while (!queue.isEmpty()) {
            System.out.print(queue.peek() + " ");
            queue.remove();
        }
    }
  
    // Driver code
    public static void main(String args[]) {
        queue = new LinkedList<Integer>();
        queue.add(10);
        queue.add(20);
        queue.add(30);
        queue.add(40);
        queue.add(50);
        queue.add(60);
        queue.add(70);
        queue.add(80);
        queue.add(90);
        queue.add(100);
  
        int k = 5;
        reverseQueueFirstKElements(k);
        Print();
    }
}
// This code is contributed by Sumit Ghosh

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to reverse first k elements 
// of a queue. 
using System;
using System.Collections.Generic;
  
class GFG
{
  
public static LinkedList<int> queue;
  
// Function to reverse the first K
// elements of the Queue 
public static void reverseQueueFirstKElements(int k)
{
    if (queue.Count == 0 || k > queue.Count)
    {
        return;
    }
    if (k <= 0)
    {
        return;
    }
  
    Stack<int> stack = new Stack<int>();
  
    // Push the first K elements into a Stack 
    for (int i = 0; i < k; i++)
    {
        stack.Push(queue.First.Value);
        queue.RemoveFirst();
    }
  
    // Enqueue the contents of stack at  
    // the back of the queue 
    while (stack.Count > 0)
    {
        queue.AddLast(stack.Peek());
        stack.Pop();
    }
  
  
    // Remove the remaining elements and 
    // enqueue them at the end of the Queue 
    for (int i = 0; i < queue.Count - k; i++)
    {
        queue.AddLast(queue.First.Value);
        queue.RemoveFirst();
    }
}
  
// Utility Function to print the Queue 
public static void Print()
{
    while (queue.Count > 0)
    {
        Console.Write(queue.First.Value + " ");
        queue.RemoveFirst();
    }
}
  
// Driver code 
public static void Main(string[] args)
{
    queue = new LinkedList<int>();
    queue.AddLast(10);
    queue.AddLast(20);
    queue.AddLast(30);
    queue.AddLast(40);
    queue.AddLast(50);
    queue.AddLast(60);
    queue.AddLast(70);
    queue.AddLast(80);
    queue.AddLast(90);
    queue.AddLast(100);
  
    int k = 5;
    reverseQueueFirstKElements(k);
    Print();
}
}
  
// This code is contributed by Shrikant13

chevron_right


Output:

 50 40 30 20 10 60 70 80 90 100

This article is contributed by Raghav Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : shrikanth13



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.