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

Program in Java Write an algorithm to transfer the elements from queue Q1 to queue Q2,...
Program in Java 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, and...
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 +...
Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2)...
Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2) Write a program to implement the merge sort algorithm in a one dimensional array?
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?
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.
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.  
Q1. Write notes on electric field. Q2. Write short notes electric field.
Q1. Write notes on electric field. Q2.  Write short notes electric field.
Two point charges, Q1 = 3.3?C and Q2 = -1.5?C ,are placed on the x axis....
Two point charges, Q1 = 3.3?C and Q2 = -1.5?C ,are placed on the x axis. Suppose that Q2 is placed at the origin, and Q1 is placed at the coordinatex1 = ? 6.0cm (Figure 1) . Part A: At what point(s) along the x axis is the electric field zero? Determine the x-coordinate(s) of the point(s). Express your answer using two significant figures. If there is more than one answer, enter your answers in ascending order separated by commas....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT