How to use C ++ class templates

#include<iostream>
#include<cstring>
using namespace std;
 
template<class Type>
class Queue
{
private:
    int front;
    int rear;
    Type * item;
     int length; // effective length 
    int maxsize; // maximum length

public:
    Queue(int i)
    {
        front=rear=0;
        if(i>10)
            maxsize=i;
        else
            maxsize=10;
        length=0;
        item=new Type[maxsize];
        if(item==0)
        {
            cout << " Space allocation was unsuccessful " << endl;
        }
    }
    ~Queue()
    {
        delete []item;
    }
    void Append (Type x);   // Add element 
    void GetHead (Type & x); // Fetch element 
    bool Allocation_mem (); // If there is not enough space, allocate space 
    bool IsFull ()   // Judge the team is full 
    {
         if ((rear + 1 )% maxsize == front)
             return  true ;
         else 
            return  false ;
    }
    bool IsEmputy()   //判断队空
    {
        if(rear==front)
            return true;
        else
            return false;
    }
    int Q_maxsize ()    // Return the maximum number of elements allowed to be stored in the queue 
    {
         return maxsize;
    }
    int Q_length ()   // Return the number of valid elements in the current queue 
    {
         return length;
    }
};
template<class Type>
void Queue<Type>::Append(Type x)
{
    if(IsFull())
    {
        Allocation_mem();
    }
    rear=(rear+1)%maxsize;
    item[rear]=x;
    length++;
}
template<class Type>
void Queue<Type>::GetHead(Type &x)
{
    if(IsEmputy())
    {
        cout << "The queue is empty " << endl;
    }
    front=(front+1)%maxsize;
    x=item[front];
    length--;
}
template<class Type>
bool Queue<Type>::Allocation_mem()
{
    Type *p,*temp;
    p=new Type[maxsize+10];
    if(!p)
    {
        cout << " Extended space failed " << endl;
         return  false ;
    }
    memmove(p,item,sizeof(Type)*maxsize);
    temp=item;
    item=p;
    delete []temp;
    maxsize=maxsize+10;
    return true;
}

// The test program is: 
int main ()
{
    cout << " Integer Queue 1: " << endl;
    Queue<int> queue1(8);
    int input[10],i,output;

    for(i=0;i<10;i++)
    {
        input[i]=i+1;
        queue1.Append(input[i]);
    }
    cout << "The maximum number of elements allowed to be stored in the queue is: " << queue1.Q_maxsize () << endl;
    cout << "The number of valid elements in the queue is: " << queue1.Q_length () << endl;
    cout << " The elements in the queue are: " ;
     for (i = 0 ; i < 10 ; i ++ )
    {
        queue1.GetHead(output);
        cout<<output<<" ";
    }
    cout<<endl;
    
    cout << " character queue 2: " << endl;
    Queue<char> queue2(26);
    char in[27]="abcdefghijklmnopqrstuvwxyz",a;
    int j,k;

    for(j=0;j<26;j++)
    {
        queue2.Append(in[j]);
    }
    cout << "The maximum number of elements allowed to be stored in the queue is: " << queue2.Q_maxsize () << endl;
    cout << "The number of valid elements in the queue is: " << queue2.Q_length () << endl;
    cout << " The elements in the queue are: " ;
     for (k = 0 ; k < 26 ; k ++ )
    {
        queue2.GetHead(a);
        cout<<a<<" ";
    }
    cout<<endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/lengle452p/p/12722222.html