Delete all elements that are the same as the first element in the queue


Download 214.07 Kb.
Sana20.12.2022
Hajmi214.07 Kb.
#1034136
Bog'liq
Safarov Qudrat Data



  1. Delete all elements that are the same as the first element in the queue.

// C++ program for the above approach.


#include
using namespace std;
void remove(int t, queue& q)
{
queue ref;
int s = q.size();
int cnt = 0;
while (q.front() != t and !q.empty()) {
ref.push(q.front());
q.pop();
cnt++;
}
if (q.empty()) {
cout << "element not found!!" << endl;
while (!ref.empty()) {
q.push(ref.front());
ref.pop();
}
}
else {
q.pop();
while (!ref.empty()) {
q.push(ref.front());
ref.pop();
}
int k = s - cnt - 1;
while (k--) {
int p = q.front();
q.pop();
q.push(p);
}
}
}
void print(queue qr)
{
while (!qr.empty()) {
cout << qr.front() << " ";
qr.pop();
}
cout << endl;
}
int main()
{
queue q;
q.push(10);
q.push(20);
q.push(30);
q.push(40);
q.push(50);
q.push(60);
print(q);
remove(39, q);
print(q);
remove(30, q);
print(q);
return 0;
}



Download 214.07 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling