Our Feeds

Saturday, 12 July 2014

Ajith KP

Queue Implementation - CPP

/*
    Implementation of Queue in CPP
    [][][]
    [][][]
    [][][]
    [][][]  Terminalcoders.Blogspot.de
    [][][]
    [][][]
    [][][]
    
    EXPLAIN VARIABLES PROGRAM
    
    [] q     ---> Queue
    [] size  ---> Size of queue
    [] fr    ---> Front
    [] rr    ---> Rear
    [] item  ---> Iteam from queue
*/
#include<iostream>
using namespace std;
class queue
{
    int q[100], size, fr, rr, i, item;
    public:
        void read()
        {
            cout<<"Enter the size of queue: ";
            cin>>size;
            fr = rr = 0;
        }
        void menu()
        {
            int opt = 0;
            while(opt!=4)
            {
                cout<<"\t\t\tMENU\n";
                cout<<"\t1. Insert item\n";
                cout<<"\t2. Delete item\n";
                cout<<"\t3. Show\n";
                cout<<"\t4. Exit\n";
                cout<<"Option: ";
                cin>>opt;
                switch(opt)
                {
                    case 1:
                        insert();
                        break;
                    case 2:
                        delet();
                        break;
                    case 3:
                        show();
                        break;
                    case 4:
                        break;
                }
            }
            cout<<"Exiting...";
        }
        void insert()
        {
            while(rr==size) // Queue is full
            {
                cout<<"Queue is full\n";
                return;
            }
            cout<<"Enter item to insert: ";
            cin>>q[rr++];
        }
        void delet()
        {
            while(fr==rr) //Queue is empty
            {
                cout<<"Queue is empty\n";
                return;
            }
            item = q[fr++];
            cout<<"The item "<<item<<" is deleted\n";
        }
        void show()
        {
            cout<<"\nQueue: ";
            for(i=fr;i<rr;i++)
            {
                cout<<q[i]<<" ";
            }
            cout<<endl;
        }
};

int main()
{
    queue q;
    q.read();
    q.menu();
}