Answer to Question #294283 in C++ for Roe

Question #294283

Add two queue elements


Requirements :


No global decelerations


Test run in main


Make use of #include<queue> header file

1
Expert's answer
2022-02-06T14:16:17-0500
#include<iostream>
#include<queue>

using namespace std;

//function to print queue
void Print(queue<int>q)
{
	while (!q.empty())
	{
		cout << q.front() << " ";
		q.pop();
	}
}

//function to add queues
queue<int> AddQueues(queue<int>q1, queue<int>q2)
{
	queue<int>result;
	while (!q1.empty() || !q2.empty())
	{
		if (!q1.empty())
		{
			result.push(q1.front());
			q1.pop();
		}
		if (!q2.empty())
		{
			result.push(q2.front());
			q2.pop();
		}
	}
	return result;
}


int main()
{
	queue<int>q1, q2;
	q1.push(10);
	q1.push(9);
	q1.push(8);
	q1.push(7);

	q2.push(1);
	q2.push(2);
	q2.push(3);
	q2.push(4);
	q2.push(5);
	queue<int>res = AddQueues(q1, q2);
	Print(res);
}	

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog