Question: Queue in Standard Template Library (STL) . Queues are a type of container adaptors which operate in a first in first out (FIFO) type of

Queue in Standard Template Library (STL) .

Queues are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front.

The functions supported by queue are :

empty() Returns whether the queue is empty

size() Returns the size of the queue

front() Returns a reference to the first element of the queue

back() Returns a reference to the last element of the queue

push(g) Adds the element g at the end of the queue

pop() Deletes the first element of the queue

Try to understand the code on the left, and modify the code to do the following tasks.

Exercise:

1. Input code to ask user "Please input a whole number n:"

2. Then after a user inputs a number n, generate n random numbers into a queue, and show the queue.

3. Use a stack to reverse the order the original queue, and also input them to the end of the queue, and show the queue.

Example:

If you have a 4 random number queue: 2 5 7 4

The final queue will be 2 5 7 4 4 7 5 2 Code:

#include #include using namespace std; void showq(queue gq) { queue g = gq; while (!g.empty()) { cout << '\t' << g.front(); g.pop(); } cout << ' '; } int main() { queue gquiz; gquiz.push(10); gquiz.push(20); cout << "The queue gquiz is : "; showq(gquiz); cout << " gquiz.size() : " << gquiz.size(); cout << " gquiz.front() : " << gquiz.front(); cout << " gquiz.back() : " << gquiz.back(); cout << " gquiz.pop() : "; gquiz.pop(); showq(gquiz); return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!