I need help. Queue - C++

May 16, 2017 at 2:22pm
Hello.Please, help
Create a one-way queue with numbers from -50 to +50. After creating the queue,
Swap the minimum and maximum elements of the queue.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <queue>
#include <cstdlib>

int main() {

	std::queue<int> q;
	const int N = 10;

	for (int i = 0; i<N; ++i)
	{
		q.push(rand() % 100 - 50);
	}

	while (!q.empty())
	{
		std::cout << q.front() << " ";
		q.pop();
	}

	return 0;
}
May 16, 2017 at 3:04pm
Easier would be to use a for loop from -50 to 50 to add the numbers to the queue.
Then you take the first elem and store it as the min value, then you remove all the elements but the last which is the max value, finally add the min value the queue again.
Topic archived. No new replies allowed.