Advertisement
momo2345

dequeue imple

Aug 28th, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. //Initial Template for C++
  2.  
  3. // CPP code to implement dequeue functions
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7.  
  8.  // } Driver Code Ends
  9.  
  10.  
  11. //User function Template for C++
  12.  
  13. /* Function to push element to front
  14. * dq : dqueue in which element is to be pushed
  15. * x : element to be pushed
  16. */
  17. void push_back_pb(deque<int> &dq, int x){
  18.    
  19.     // Your code here
  20.     dq.push_back(x);
  21.    
  22. }
  23.  
  24. /* Function to pop element from back
  25. * dq : dqueue From which element is to be popped
  26. */
  27. void pop_back_ppb(deque<int> &dq){
  28.    
  29.     if(!dq.empty()) dq.pop_back();
  30.  
  31.    
  32. }
  33.  
  34. /* Function to return element from front
  35. * dq : dqueue from which element is to be returned
  36. */
  37. int front_dq(deque<int> &dq){
  38.    
  39. if(!dq.empty()) return dq.front();
  40.     /*Your code here*/
  41.     else
  42.     return -1;
  43.    
  44. }
  45.  
  46. /* Function to push element to front
  47. * dq : dqueue in which element is to be pushed
  48. * x : element to be pushed
  49. */
  50. void push_front_pf(deque<int> &dq, int x){
  51.    
  52.     // Your code here
  53.     dq.push_front(x);
  54.    
  55. }
  56.  
  57. // { Driver Code Starts.
  58.  
  59. // Driver code
  60. int main() {
  61.    
  62.     int testcase;
  63.     cin >> testcase;
  64.    
  65.     // declaring deque
  66.     deque<int> dq;
  67.    
  68.     while(testcase--){
  69.        
  70.         int queries;
  71.         cin >> queries;
  72.        
  73.         while(queries--){
  74.         string s;
  75.         cin >> s;
  76.        
  77.         // if query is to push back
  78.         if(s == "pb"){
  79.             int x;
  80.             cin >> x;
  81.             push_back_pb(dq, x);
  82.             cout << dq.back() << endl;
  83.         }
  84.        
  85.         // if query is to push front
  86.         if(s == "pf"){
  87.             int x;
  88.             cin >> x;
  89.             push_front_pf(dq, x);
  90.             cout << dq.front() << endl;
  91.         }
  92.        
  93.         // if query is to pop back
  94.         if(s == "pp_b"){
  95.             pop_back_ppb(dq);
  96.             cout << dq.size() << endl;
  97.         }
  98.        
  99.         // if query is to return front
  100.         if(s == "f"){
  101.             int x = front_dq(dq);
  102.             cout << x << endl;
  103.         }
  104.         }
  105.         dq.clear();
  106.     }
  107.    
  108.     return 0;
  109. }  // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement