Question

In: Computer Science

[C++] Write an algorithm to transfer the elements from queue Q1 to queue Q2, so that...

[C++]

Write an algorithm to transfer the elements from queue Q1 to queue Q2, so that the contents in Q2 will be in reverse order as they are in Q1 (e.g. if your queue Q1 has elements A, B, and C from front to rear, your queue Q2 should have C, B, and A from front to rear). Your algorithm must explicitly use an additional stack to solve the problem. Write your algorithm in pseudo code first.

Solutions

Expert Solution

CODE

#include <bits/stdc++.h>

using namespace std;

void display(queue<char>& q)

{

  while (!q.empty()) {

    cout << q.front() << " ";

    q.pop();

  }

}

void reverse(queue<char>& q)

{

  stack<char> s;

  while (!q.empty()) {

    s.push(q.front());

    q.pop();

  }

  while (!s.empty()) {

    q.push(s.top());

    s.pop();

  }

}

int main()

{

  queue<char> q;

q.push('A');

q.push('B');

q.push('C');

  reverse(q);

  display(q);

}


Related Solutions

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.
Let Q1, Q2, Q3 be constants so that (Q1, Q2) is the critical point of the...
Let Q1, Q2, Q3 be constants so that (Q1, Q2) is the critical point of the function f(x, y) = (175)x 2 + (−150)xy + (175)y 2 + (−200)x + (400)y + (230), and Q3 = 1 if f has a local minimum at (Q1, Q2), Q3 = 2 if f has a local maximum at (Q1, Q2), Q3 = 3 if f has a saddle point at (Q1, Q2), and Q3 = 4 otherwise. Let Q = ln(3 +...
Let Q1, Q2, Q3 be constants so that (Q1, Q2) is the critical point of the...
Let Q1, Q2, Q3 be constants so that (Q1, Q2) is the critical point of the function f(x, y) = (90)x 2 + (0)xy + (90)y 2 + (−72)x + (96)y + (40), and Q3 = 1 if f has a local minimum at (Q1, Q2), Q3 = 2 if f has a local maximum at (Q1, Q2), Q3 = 3 if f has a saddle point at (Q1, Q2), and Q3 = 4 otherwise. Let Q = ln(3 +...
C(Q1, Q2) = 3,500 - 205Q1Q2 - (Q1)2 + (Q2)2 A. What do you need to...
C(Q1, Q2) = 3,500 - 205Q1Q2 - (Q1)2 + (Q2)2 A. What do you need to know from the equation above to see if there are Cost Complementarity? Are there Cost Complementarity? B. What is MC1(Q1,Q2) ? C. What is MC2(Q1,Q2) ? D. Are there economies of scope? Explain. E. What are the implications for a merger?
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.  
Concepts & Principles of Engineering Management Q1. Name and explain the elements of motivation. Q2. What...
Concepts & Principles of Engineering Management Q1. Name and explain the elements of motivation. Q2. What is the difference between self-efficacy and self-esteem? Q3. What are the purposes of interactive communication? Q4. What are some of the physical health concerns that have been linked to stress? Q5. Describe, with example, the self-focused impression management techniques.
Q1. Write notes on electric field. Q2. Write short notes electric field.
Q1. Write notes on electric field. Q2.  Write short notes electric field.
((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...
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 recursive algorithm to find the maximum element in an array of n elements and...
write a recursive algorithm to find the maximum element in an array of n elements and analyze its time efficiency. (I am using c++ programming language)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT