下面代碼是關(guān)于一個(gè)模板化C++隊(duì)列類的代碼。
#include<iostream>
#include<cstdlib>
#define default_value 16
using namespace std;
template< class T > class Queue
{
? ? public:
? ? ? ? ? {delete [] values;}
? ? ? ? bool enQueue( T );
? ? ? ? T deQueue();
? ? ? ? bool isEmpty();
? ? ? ? bool isFull();
? ? private:
? ? ? ? int size;
? ? ? ? int front;
? ? ? ? int back;
};
template< class T > Queue<T>::Queue(int x):
? ? values(new T[size]),
? ? front(0),
? ? back(0)
template< class T > bool Queue<T>::isFull()
{
? ? if((back + 1) %? size == front )
? ? ? ? return 1;
? ? else
? ? ? ? return 0;
}
template< class T > bool Queue<T>::enQueue(T x)
{
? ? bool b = 0;
? if(!Queue<T>::isFull())
? {
? ? ? values[back] = x;
? ? ? back = (back + 1) % size;
? ? ? b = 1;
? }
? return b;
}
template< class T > bool Queue<T>::isEmpty()
{
? ? ? ? return 1;
? ? else
}
template< class T > T Queue<T>::deQueue()
{
? ? T val = -1;
? ? if(!Queue<T>::isEmpty())
? ? {
? ? ? ? val = values[front];
? ? ? ? front = ( front + 1 ) % size;
? ? }
? ? else
? ? {
? ? ? ? cerr << "Queue is Empty : ";
? ? }
return val;
}
int main()
{
? ? ? ? Queue <float> qu1(32);
? ? ? ? float x = 1.1;
? ? cout << "n EnQueued values into qu1: ";
? ? ? ? {
? ? ? ? ? ? else
? ? ? ? ? ? ? ? cout << "n Queue (qu1) is full: ";
? ? ? ? }
? ? ? ? cout << "nn DeQueued values from qu1 : n";
? ? for(int j = 1 ; j < 10 ; j++)
? ? ? ? ? cout << qu1.deQueue() << endl;
? ? cout << endl << endl;
? ? return 0;
}