Answer to Question #294942 in C++ for Roe

Question #294942

Write a program that deletes duplicate elements from a queue.



Requirements:


No global decelerations


Test run the code in main

1
Expert's answer
2022-02-07T15:40:27-0500
#include <iostream>
#include <queue>   
#include<unordered_set>
using namespace std;

void printQueue(queue<int> q) {
	while (!q.empty()) {
		cout << q.front() << " ";
		q.pop();
	}
	cout << endl;
}

void remDuplicates(queue<int> &q) {
	unordered_set<int> copies;


	while (!q.empty()) {
		copies.insert(q.front());
		q.pop();
	}

	unordered_set<int> ::iterator it;

	for (it = copies.begin(); it != copies.end(); it++)
		q.push(*it);
}


int main() {
	queue<int> que;
	que.push(4);
	que.push(4);
	que.push(4);
	que.push(2);
	que.push(2);
	que.push(3);

	printQueue(que);
	remDuplicates(que);
	printQueue(que);
	return 0;
}

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