Wednesday, 5 June 2013

Queue using Array

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>


class queue
{
    int array[15];

    int front,rear;
    public:
    queue()
    {
        front=-1;
        rear=-1;
    }
    void enqueue(int);
    void dequeue();
    void print();

};

void queue::print()
{
if(front <=rear)
{
    for(int i=front;i<=rear;i++)
    {
    cout<<endl<<array[i];
    }
}
else
cout<<"\n queue is empty ......";

}

void queue::dequeue()
{
if(rear < front)
{
rear=-1;
front=-1;
cout<<"\n queue is empty ......";

}
else
{
cout<<array[front]<<" is dequeue out form the queue";
front++;


}
}

void queue::enqueue(int data)
{

    if(rear<15)
    {

        rear=rear+1;
        array[rear]=data;
        if(front<0) front=0;
    }
    else
    cout<<"\n\t queue Full";


}


void main()
{
    clrscr();
    queue s;

    int count,data;

    char choice,option;

do
{
    clrscr();
        cout<<"\n\t   Select Operation \n";
        cout<<"\n\t1. To Create the queue";
        cout<<"\n\t2. To Display the queue";
        cout<<"\n\t3. To Add the data in the queue";
        cout<<"\n\t4. To Delete the data from the queue";
        cout<<"\n\t0. Exit";
        cout<<"\n\n\t   Enter your choice:- ";
        cin>>option;
        switch(option)
        {
            case '1':
                    {
                     cout<<"\n \t How many item you want to added in the queue : ";
                     cin>>count;
                        for(int i=0;i<count;i++)
                        {
                            cout<<"\n \t Enter data which is need to be added in the queue: ";
                            cin>>data;
                            s.enqueue(data);
                        }
                        break;
                    }
            case '2':
                    {
                     cout<<"\n \t  Data in the queue";
                     s.print();
                     break;
                    }

            case '3':
                    {
                     cout<<"\n \t Enter data which is need to be added in the queue: ";
                     cin>>data;
                     s.enqueue(data);
                     break;
                    }
            case '4':
                    {
                        s.dequeue();
                        break;
                    }
            case '0':
                    {
                     exit(0);
                     break;
                    }
        default:
                    {
                        cout<<"\n \tInvalid Option";
                        break;
                    }
        }
        cout<<"\n\n \tDo you want to continue (y/n):- ";
        cin>>choice;
    }while(choice!='n');
    getch();
}

No comments:

Post a Comment