C++ implementation of front and rear end varying queue

C++ implementation of front and rear end varying queue

#include <iostream.h>
#include <conio.h>
#define size 10

class queue
{
    int q,item[size],rear,front;
    public:
        queue(){q=0;rear=0;front=0;}
        void enqueue();
        void dequeue();
        void display();
};

void queue::enqueue()
{
    int a;
    if(rear>=size) cout<<"The queue is full"<<endl;
    else
    {
        cout<<"Enter a number"<<endl;
        cin>>a;
        item[rear]=a;
        rear++;
        q++;
    }
}

void queue::dequeue()
{
    if(q==0) cout<<"The queue is empty"<<endl;
    else
    {
        cout<<"The number is"<<endl<<item[front];
        front++;
        q--;
    }
}

void queue::display()
{
    if(q==0) cout<<"Empty";
    else{
    cout<<"The numbers in queue are"<<endl;
    for(int i=front;i<rear;i++)
    {
        cout<<item[i]<<'\t';
    }
    }
}

void main()
{
    queue a1;
    int ch;
    do{
    clrscr();
    cout<<"Menu"<<endl;
    cout<<"1.Enqueue operation"<<endl;
    cout<<"2.Dequeue operation"<<endl;
    cout<<"3.Display all"<<endl;
    cout<<"Enter your choice"<<endl;
    cin>>ch;
    switch(ch)
    {
        case 1:
            a1.enqueue();
            break;
        case 2:
            a1.dequeue();
            break;
        case 3:
            a1.display();
            break;
    }
    getch();
    }while(ch<4);

}



No comments :

Post a Comment

Your Comment and Question will help to make this blog better...