Skip to main content

Queue in C++

 


#include<bits/stdc++.h>

using namespace std;
#define n 10


class que{
    int *arr;
    int front;
    int back;
    public:
        que(){
     arr = new int[n];
     front = -1;
     back = -1;
    };
    void push(int x){
        if(back == n-1){
            cout<<"queue overflow"<<endl;
            return;
        }
        back++;
        arr[back] = x;

        if(front == -1){
            front++;
        }
    }
    void pop(){
        if(front == -1 || front>back){
            cout<<"No element in queue"<<endl;
            return;
        }
        else{
            for (int i = 0; i < back; i++)
            {
                arr[i]=arr[i+1];
            }
            back--;
        }

    }
    int peek(){
        if(front == -1 || front>back){
            cout<<"No element in quee"<<endl;
            return -1;
        }
        return arr[front];
    }
    bool empty(){
           if(front == -1 || front>back){
         
            return true;
        }
        return false;

    }
    void display(){
        for (int i = front; i <=back; i++)
        {
            cout<<arr[i]<<endl;
        }
       
    }

};
int main()
{
    que q ;
    q.push(21);
    q.push(2);
    q.push(3);
    q.push(4);

    q.display();
    
    cout<<endl;

    q.pop();
    q.display();

    // cout<<q.peek()<<endl;
    // q.pop();
    // cout<<q.peek()<<endl;
    // q.pop();
    // cout<<q.peek()<<endl;
    // q.pop();
    // cout<<q.peek()<<endl;
    // q.pop();

    // cout<<q.empty()<<endl;
   
    return 0;
}

Comments