Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <forward_list>
  4. using namespace std;
  5.  
  6. bool checkQueue(queue<int> q,int x) {
  7.     int position = 0;
  8.     bool check = true;
  9.     int i = 0;
  10.     unsigned int size = q.size();
  11.     while (!q.empty()) {
  12.         if (q.front() == x) {
  13.             if (check == true) {
  14.                 position = i;
  15.                 check = false;
  16.             }
  17.             else if (i == size - 1 - position) {
  18.                 return true;
  19.             }
  20.         }
  21.         i++;
  22.         q.pop();
  23.     }
  24.     return false;
  25. }
  26.  
  27. void eraseQueue(forward_list<queue<int>>& list, int x) {
  28.     auto it = list.before_begin();
  29.     auto next = list.begin();
  30.     while (next != list.end()) {
  31.         if (checkQueue(*next, x)){
  32.             next = list.erase_after(it);
  33.       }
  34.         else {
  35.             it++;
  36.             next++;
  37.         }
  38.     }
  39. }
  40. int main() {
  41.     queue<int> q;
  42.     int a = 2;
  43.     q.push(4);
  44.     q.push(2);
  45.     q.push(3);
  46.     q.push(2);
  47.     q.push(9);
  48.     queue<int> q2;
  49.     q2.push(7);
  50.     q2.push(8);
  51.     q2.push(9);
  52.     queue<int> q3;
  53.     q3.push(1);
  54.     q3.push(2);
  55.     q3.push(3);
  56.     q3.push(1);
  57.     forward_list<queue<int>> list;
  58.     list.push_front(q);
  59.     list.push_front(q2);
  60.     list.push_front(q3);
  61.     eraseQueue(list, a);
  62.     auto it = list.begin();
  63.     while (it != list.end()) {
  64.         while (!it->empty()) {
  65.             cout << it->front() << " ";
  66.             it->pop();
  67.         }
  68.         cout << endl;
  69.         it++;
  70.     }
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement