Queue data structure in c++

 #include<iostream>

using namespace std;
#define n 10
class queue{
    int* arr;
    int front;
    int back;

    public:
     queue(){
         arr=new int[n];
         front=-1;
         back=-1;

     }

     void push(int x){
         if(back==n-1){
             cout<<"queue is not empty "<<endl;
             return;
         }
         back++;
         arr[back]=x;
         if(front==-1){
             front++;
         }
     }

     void pop(){
         if(front>back||front==-1){
             cout<<"no element is in the queue "<<endl;
             return;
         }
         front++;
     }

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

};

int main(){

     queue q;
     q.push(1);
     q.push(2);
     q.push(3);
     q.push(4);

     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

Popular posts from this blog

priority_queue

css in phone