happyguy89

DSLab3Task5

May 31st, 2021
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. stack<int>st1,st2;
  4.  
  5. void show(stack <int> st1)
  6. {
  7.  
  8.     while(!st1.empty())
  9.     {
  10.         cout<<st1.top()<<" ";
  11.         st1.pop();
  12.     }
  13.     cout<<endl;
  14. }
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. void enqueue(int element)
  24. {
  25.  
  26.     while (!st1.empty())
  27.         {
  28.             st2.push(st1.top());
  29.             st1.pop();
  30.         }
  31.     st1.push(element);
  32.     while (!st2.empty())
  33.         {
  34.             st1.push(st2.top());
  35.             st2.pop();
  36.         }
  37.         show(st1);
  38.  
  39.  
  40. }
  41.  
  42. void dequeue(stack <int> st)
  43. {
  44.     st.pop();
  45.     //show(st);
  46. }
  47.  
  48. void del(stack <int> st)
  49. {
  50.     if(!st.empty())
  51.     {
  52.  
  53.             st.pop();
  54.             cout<<"SIZE:"<<" "<<"0"<<" "<<"ITEMS:"<<" "<<"NULL";
  55.  
  56.  
  57.     }
  58.     else
  59.     {
  60.         cout<<"SIZE:"<<" "<<"0"<<" "<<"ITEMS:"<<" "<<"NULL";
  61.     }
  62.  
  63. }
  64.  
  65.  
  66. int main()
  67. {
  68.     int length;
  69.     cin>>length;
  70.     int t;
  71.     cin>>t;
  72.  
  73.     while(t--)
  74.     {
  75.         int choice;
  76.         cin>>choice;
  77.         if(choice==1)
  78.         {
  79.             int element;
  80.             cin>>element;
  81.             if(st1.size()==length)
  82.             {
  83.                 cout<<"Size:"<<st1.size()<<" elements: Overflow!"<<endl;
  84.             }
  85.             else
  86.             {
  87.                 cout<<"Size:"<<st1.size()+1<<" elements: ";
  88.                 enqueue(element);
  89.  
  90.  
  91.  
  92.             }
  93.  
  94.         }
  95.  
  96.         else if(choice==2)
  97.         {
  98.             if(st1.size()>0)
  99.             {
  100.                 cout<<"Size: "<<st1.size()-1<<" elements: ";
  101.                 dequeue(st1);
  102.                 if(st1.empty())
  103.                 {
  104.                     cout<<"NULL"<<endl;
  105.                 }
  106.                 show(st1);
  107.             }
  108.             else
  109.             {
  110.                 del(st1);
  111.  
  112.             }
  113.         }
  114.     }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment