C++ implementation of Circular Queue

C++ implementation of Circular 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(q==size) cout<<"The queue is full"<<endl;
    else
    {
        cout<<"Enter a number"<<endl;
        cin>>a;
        item[rear]=a;
        rear=(rear+1)%size;
        q++;
    }
}

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

void queue::display()
{
    if(q==0) cout<<"Empty";
    else{
    cout<<"The numbers in queue are"<<endl;
    for(int i=0;i<q;i++)
    {
        if(i+front<size) cout<<item[i+front]<<'\t';
        else cout<<item[i+front-10]<<'\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...