/***********************************************/
/** Author       : @PrakashGautam              */
/** Date Written : 23 March 2013               */
/** Last Updated : 23 March 2013               */
/***********************************************/
/*A program to demonstrate the use of Expandable Queue data sturcture with tempelate class*/
/*The queue used here is a dynamically expanding queue and */
/*On addition of every element the size of Queue increases by one*/
/*on removal of every element the size of Queue decreases by one */
/*the variable Count holds the number of data and actually the  */
/*queue has array size of 0 through Count-2 ie when count is 5 the */
/* the array exists as Element[4] thus Element+O to Element+3 is the*/
/*allocated memory space this may be different from what conventionally */
/*is used because the valid address is upto Element+Count-2 */


#include <iostream>
#include <iomanip>
#include <typeinfo>

using namespace std;

template 
class Queue
{
    private:
        DataType *Element;
        int Count;
    public:
        Queue(DataType a)
        {

            Element= new DataType[1];
            *(Element+0)=a;
            Count=1;
        }
        Queue():Count(0) {}
        void Put(DataType a)
        {
            DataType *ExpandedAddress= new DataType[Count+1];
            for(int i=0; i&lt;Count; i++)
                *(ExpandedAddress+i)=*(Element+i);
            delete [] Element;
            Element=ExpandedAddress;
            Element[Count]=a;
            Count++;
        }
        DataType Get()
        {
            DataType Give;
            if(Count&lt;1)
            {
                cout<<"\t\tError :: Queue Empty\n \t\tThe output is a error value"<<endl;
                return static_cast(int(0));
            }
            else
            {
                Give=Element[0];
                DataType *ReducedAddress=new DataType[Count-1];
                for(int i=0; i&lt;Count-1; i++)
                    *(ReducedAddress+i)=*(Element+i+1);
                delete []Element;
                Element=ReducedAddress;
                Count--;
                return Give;
            }
        }
        void showsize()
        {
            cout<<" the queue size is "<<Count<<endl;
        }
};

int main()
{
    Queuea(14);
    for(float b=1; b&lt;=10; b++)
    {
        a.Put(b);
    }
    a.showsize();
    for(int i=1; i&lt;=11; i++)
    {
        cout<<setw(5)<<i<<")"<<setw(7)<<a.Get()<<endl;
    }
    return 0;
}
Advertisements