Question

In: Computer Science

((by C++ ))Write a program that will reverse the content of a Queue using the following...

((by C++ ))Write a program that will reverse the content of a Queue using the following standard queue operations.

  • enqueue(x) : Add an item x to rear of queue.
  • dequeue() : Remove an item from front of queue.
  • empty() : Checks if a queue is empty or not.

For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a manner such that if we re-insert the elements in the queue they would get inserted in reverse order. So now our task is to choose such data-structure which can serve the purpose. According to the approach, the data-structure should have the property of ‘LIFO’ as the last element to be inserted in the data structure should be the first element of the reversed queue. Using the new data structure to store the elements of the queue temporarily should:

  • Pop the elements from the queue and insert into the new data structure. (first element of the data structure is last element of the queue)
  • Pop the elements of the data structure to insert back into the queue. (The last element of the data structure is the first one to be inserted into the queue)

Input Format

The program should accept N series of integer values where 1 < N < 10 separated by a space and stored in a queue. If the input N integer exceeds the allowed number of input the program should display an error message.

Example Input

Enter N Integer values: 1 2 3 4 5 6 7 8 9 10

Output Format

The program should display the reverser order of the queue elements in a single line separated by a space. (Note: that only queue standards operations can be used to display the queue elements).

Example Output

Reverse Queue: 10 9 8 7 6 5 4 3 2 1

Solutions

Expert Solution

C++ PROGRAM :

#include <iostream>
#include<stack>
#include<queue>

using namespace std;

int main() {

queue<int>q;
cout<<"Enter N integer values: ";
int n;
int count=0;

// take the input until N is in range.
while(cin>>n&&count<10)
{
q.push(n);
count++;
  
}

//condition to check if number exceeds N series of integers.
if(cin>>n)
{
cout<<"Error! The input N exceeds the allowed number of input.";
return 0;
}


//using data Structure STACK which follows LIFO order.
stack<int>s;
while(!q.empty())
{
int t=q.front();
q.pop();
s.push(t);
}

//poping the elements from the Stack , back into the queue.
while(!s.empty())
{
int top=s.top();
s.pop();
q.push(top);
}


//printing the reverse content from the queue.
cout<<"\nReverse Queue: ";
while(!q.empty())
{
cout<<q.front()<<" ";
q.pop();
}

   return 0;
}

EXAMPLE :


Related Solutions

Write a c++program using queue to find the minimum value in a queue. Use the above...
Write a c++program using queue to find the minimum value in a queue. Use the above program for implementing queue. You must use dequeue function to read values from queue.  
Given a queue of integers, write an algorithm and the program in c++ that, using only...
Given a queue of integers, write an algorithm and the program in c++ that, using only the queue ADT, calculates and prints the sum and the average of the integers in the queue without changing the contents of the queue.
DO THIS IN C++: Question: Write a function “reverse” in your queue class (Codes are given...
DO THIS IN C++: Question: Write a function “reverse” in your queue class (Codes are given below. Use and modify them) that reverses the whole queue. In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue. NOTE: A humble request, please don't just copy and paste the answer from chegg. I need more specific answer. Also I don't have much question remaining to...
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
Write a C program, called reverse, using standard I/O functions, to take a file as input...
Write a C program, called reverse, using standard I/O functions, to take a file as input then copies it to another file in reverse order. That is, the last byte becomes the first, the byte just before the last one becomes the second, etc. The program call should look like: reverse fileIn fileOut
Write a program to reverse each integer number on array (size 3) using while loop. C++...
Write a program to reverse each integer number on array (size 3) using while loop. C++ Input: 3678 2390 1783 Output: 8763 0932 3871
As the following statements execute, what is the content of the priority queue? Show the content...
As the following statements execute, what is the content of the priority queue? Show the content of the queue after each step: PriorityQueue<String> myPriorityQueue = new PriorityQueue<>(); myPriorityQueue.offer("Jim"); myPriorityQueue.offer ("Jess"); myPriorityQueue.offer ("Jill"); myPriorityQueue.offer ("Jane"); String name = myPriorityQueue.poll(); myPriorityQueue.offer (name); myPriorityQueue.offer (myPriorityQueue.peek()); myPriorityQueue.offer ("Jim"); myPriorityQueue.poll();
Write a C program to implement the priority queue with the operations insert and extractmax. Sample...
Write a C program to implement the priority queue with the operations insert and extractmax. Sample : ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 2 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 4 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 6 enter any key to go to main menu...
C language Write a program in C to implement Queue and its operation (like create, insert,...
C language Write a program in C to implement Queue and its operation (like create, insert, delete, search) using array data structure.
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the...
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the user to input a few lines of text and then outputs strings in same order of entry. (use of the library ArrayDeque) In Java please.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT