C ++ IMPLEMENTATION OF QUEUE USING CLASSES


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


#define MAX 5 // MAXIMUM CONTENTS IN QUEUE


class queue
{
private:
    int t[MAX];
    int al; // Addition End
    int dl; // Deletion End

public:
queue()
{
    dl=-1;
    al=-1;
}

void del()
{
int tmp;
if(dl==-1)
{
    cout<<"Queue is Empty";
}
else
{
        for(int j=0;j<=al;j++)
        {
            if((j+1)<=al)
            {
                tmp=t[j+1];
                t[j]=tmp;
            }
            else
            {
                al--;

            if(al==-1)
                dl=-1;
            else
                dl=0;
            }
        }
}
}

void add(int item)
{
    if(dl==-1 && al==-1)
    {
        dl++;
        al++;
    }
else
{
        al++;
        if(al==MAX)
    {
            cout<<"Queue is Full\n";
            al--;
            return;
        }
    }
    t[al]=item;

}

void display()
{
    if(dl!=-1)
{
    for(int i=0;i<=al;i++)
    cout<<t[i]<<" ";
}
else
    cout<<"EMPTY";
}

};

void main()
{
queue a;
int data[5]={32,23,45,99,24};

cout<<"Queue before adding Elements: ";
a.display();
cout<<endl<<endl;

for(int i=0;i<5;i++)
{
a.add(data[i]);
cout<<"Addition Number : "<<(i+1)<<" : ";
a.display();
cout<<endl;
}
cout<<endl;
cout<<"Queue after adding Elements: ";
a.display();
cout<<endl<<endl;

for(int i=0;i<5;i++)
{
a.del();
cout<<"Deletion Number : "<<(i+1)<<" : ";
a.display();
cout<<endl;
}
getch();
}


OUTPUT:

Queue before adding Elements: EMPTY

Addition Number : 1 : 32
Addition Number : 2 : 32 23
Addition Number : 3 : 32 23 45
Addition Number : 4 : 32 23 45 99
Addition Number : 5 : 32 23 45 99 24

Queue after adding Elements: 32 23 45 99 24

Deletion Number : 1 : 23 45 99 24
Deletion Number : 2 : 45 99 24
Deletion Number : 3 : 99 24
Deletion Number : 4 : 24
Deletion Number : 5 : EMPTY

As you can clearly see through the output of this program that addition is
always done at the end of the queue while deletion is done from the front end of
the queue.