Advertisement
Imran_Mohammed

Everything_of_queue_deque_priority queue

Jan 30th, 2021
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1.        // In The Name Of Allah
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6.  
  7.  
  8. int main()
  9. {
  10.     //Queue(not unique not sort) :Complexity 0(1)
  11.     //FIFO(first input first output):
  12.     queue <int> q;
  13.     q.push(3);
  14.     q.push(7);
  15.     q.push(5);
  16.     q.push(3);
  17.     q.push(4);
  18.     cout << q.size() << endl;//5
  19.     while(!q.empty()){
  20.         cout << q.front() <<" ";//3 7 5 3 4 first element output
  21.         q.pop();//first element delete
  22.     }
  23.     cout << endl;
  24.  
  25.     //deque (not unique not sort) :Complexity 0(1)
  26.     //elements are added in first or last (both place)
  27.     deque <int> dq;
  28.     dq.push_front(3);
  29.     dq.push_front(7);
  30.     dq.push_front(5);
  31.     dq.push_back(3);
  32.     dq.push_back(4);
  33.     cout << dq.size() << endl;//5
  34.     cout << dq.front() << " " << dq.back() << endl;// 5 4
  35.     dq.pop_front();
  36.     dq.pop_back();
  37.     cout << dq.front() << " " << dq.back() << endl;// 7 3
  38.  
  39.     //dq.empty (check empty or not)
  40.  
  41.     //Priority queue:complexity log2(n):
  42.     //biggest value stay in top
  43.     priority_queue <int> q;
  44.     q.push(3);
  45.     q.push(7);
  46.     q.push(5);
  47.     q.push(3);
  48.     q.push(4);
  49.     cout << q.size() << endl;//5
  50.     while(!q.empty()){
  51.         cout << q.top() <<" ";//7 5 4 3 3
  52.         q.pop();//top element delete
  53.     }
  54.     cout << endl;
  55.  
  56.     //small to big element :
  57.      priority_queue <int,vector<int>, greater<int> >q;
  58.     q.push(3);
  59.     q.push(7);
  60.     q.push(5);
  61.     q.push(3);
  62.     q.push(4);
  63.     cout << q.size() << endl;//5
  64.     while(!q.empty()){
  65.         cout << q.top() <<" ";//3 3 4 5 7
  66.         q.pop();//top element delete
  67.     }
  68.     cout << endl;
  69.  
  70.     //first value greater to small and second value small to greater:
  71.     priority_queue <pair <int,int>>q;
  72.     q.push({1,-2});
  73.     q.push({1,-3});
  74.     q.push({2,-2});
  75.     q.push({2,-1});
  76.     q.push({1,-1});
  77.     while(!q.empty()){
  78.        cout << q.top().first << " " << q.top().second*-1 << endl;
  79.         q.pop();//top element delete
  80.     }
  81.     /*2 1
  82.     2 2
  83.     1 1
  84.     1 2
  85.     1 3*/
  86.    
  87.  
  88.     return 0;
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement