template <typename T>
class Queue{
private:
unsigned int num;
int capacity;
T *elems; //使用数组的方式存储
public:
Queue() :elems(NULL)
, num(0)
, capacity(0)
{}
~Queue()
{
if (elems)
{
delete[] elems;
elems = NULL;
}
}
int Size()
{
return num;
}
void Push(T element)
{
checkCapacity();
if (num < capacity)
{
elems[num] = element;
num++;
}
}
void Pop()
{
int i = 0;
while (num>i)
{
elems[i] = elems[i + 1];
i++;
}
num--;
}
bool Empty()
{
return num == 0;
}
T Back()
{
return elems[num - 1];
}
T Front()
{
return elems[0];
}
void checkCapacity()
{
if (num >= capacity)
{
capacity = capacity > 0 ? capacity * 2 : 3;
T* tmp = new T[capacity];
for (int i = 0; i < num; i++)
{
tmp[i] = elems[i];
}
delete[] elems;
elems = tmp;
}
}
};
void test()
{
Queue<int> s;
s.Push(32);
s.Push(332);
s.Push(54);
s.Push(65);
s.Push(76);
while (!s.Empty())
{
cout << s.Front() << " ";
s.Pop();
}
cout << endl;
}