Advertisement
spider68

Queue implementation

Jan 20th, 2021
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Queue{
  6.     int size,f,r,cnt;
  7.     int*a;
  8.     public:
  9.         Queue(int n){
  10.             size=n;
  11.             f=-1;
  12.             r=-1;
  13.             cnt=0;
  14.             a=new int[n];
  15.         }
  16.         void push_front(int x){
  17.             if(cnt==size)cout<<"FULL\n";
  18.             else{
  19.                 if(f==-1){
  20.                     f=r=0;
  21.                     a[f]=x;
  22.                 }
  23.                 else{
  24.                     if(f==0)f=size-1;
  25.                     else f--;
  26.                     a[f]=x;
  27.                 }
  28.                 cnt++;
  29.                 cout<<"push: "<<x<<endl;
  30.             }
  31.         }
  32.         void push_back(int x){
  33.             if(cnt==size)cout<<"FULL\n";
  34.             else{
  35.                 if(f==-1){
  36.                     f=r=0;
  37.                     a[f]=x;
  38.                 }
  39.                 else{
  40.                     if(r==size-1)r=0;
  41.                     else r++;
  42.                     a[r]=x;
  43.                 }
  44.                 cnt++;
  45.                 cout<<"push: "<<x<<endl;
  46.             }
  47.         }
  48.         int pop_front(){
  49.             if(cnt==0)cout<<"Empty\n";
  50.             else{
  51.                 int x=a[f];
  52.                 if(f==r)f=r=-1;
  53.                 else f=(f+1)%size;
  54.                 cnt--;
  55.                 return x;
  56.             }
  57.         }
  58.         int pop_back(){
  59.             if(cnt==0)cout<<"Empty\n";
  60.             else{
  61.                 int x=a[r];
  62.                 if(f==r)f=r=-1;
  63.                 else if(r==0)r=size-1;
  64.                 else r--;
  65.                 cnt--;
  66.                 return x;
  67.             }
  68.         }
  69.         int front(){
  70.             if(cnt==0)return -1;
  71.             else return a[f];
  72.         }
  73.         int rear(){
  74.             if(cnt==0)return -1;
  75.             else return a[r];
  76.         }
  77.         void show(){
  78.             if(f==-1)cout<<"empty";
  79.             else if(f<=r)for(int i=f;i<=r;i++){
  80.                 cout<<a[i]<<" ";
  81.             }
  82.             else{
  83.                 for(int i=f;i<size;i++)cout<<a[i]<<" ";
  84.                 for(int i=0;i<=r;i++)cout<<a[i]<<" ";
  85.             }
  86.             cout<<endl;
  87.         }
  88. };
  89. int main()
  90. {
  91.    Queue q(6);
  92.    q.show();
  93.    q.push_back(1);
  94.    q.show();
  95.    q.push_back(2);
  96.    q.show();
  97.    q.push_front(5);
  98.    q.show();
  99.    q.push_front(6);
  100.    q.show();
  101.    q.push_back(3);
  102.    q.show();
  103.    q.push_back(4);
  104.    q.show();
  105.    q.push_front(7);
  106.    q.show();
  107.    q.push_front(8);
  108.    q.show();
  109.    q.pop_back();
  110.    q.show();
  111.    q.pop_front();
  112.    q.show();
  113.    return 0;
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement