1. 程式人生 > >C++學習筆記 -- 迴圈佇列的模板

C++學習筆記 -- 迴圈佇列的模板

#include<iostream>
#include<cassert>


using namespace std;
template<class T,int SIZE = 50>
class queue
{
public:
queue():front(0),rear(0),count(0){}
~queue();
void add(const T &a);
T remove();
int getcount();
void clear();


private:
int front;
int rear;
int count;
T list[SIZE];
};
template<class T,int SIZE>
void queue<T,SIZE>::add(const T &a)
{
assert(count!=SIZE);
list[rear] = a;
rear = (rear+1)%SIZE;
coutnt++;
}
template<class T,int SIZE>
T queue<T,SIZE>::remove()
{
assert(a!=0);
int temp = front;
count--;
front = (front+1)%SIZE;
return list[temp];
}
template<class T,int SIZE>
int queue<T,SIZE>::getcount()
{
return count;
}
template<class T,int SIZE>
void queue<T,SIZE>::clear()
{
count = 0;
front = 0;
rear = 0;
}