- 队列是一种 先进先出(First In First Out,FILO) 的种线性数据结构 。
代码 Array.h 点它
代码清单
#ifndef C___ARRAYQUEUE_H
#define C___ARRAYQUEUE_H
#include "Array.h"
template<typename T>
class ArrayQueue{
public:
ArrayQueue();
ArrayQueue(const int capacity);
int getSize()const;
int getCapacity()const;
bool isEmpty()const;
void enqueue(const T&t);
T dequeue();
T getFront();
void print()const;
~ArrayQueue();
private:
Array<T>*arr;
};
template<typename T>
ArrayQueue<T>::ArrayQueue() {
arr = new Array<T>();
}
template<typename T>
ArrayQueue<T>::ArrayQueue(const int capacity) {
arr = new Array<T>(capacity);
}
template<typename T>
int ArrayQueue<T>::getSize()const {
return arr->getSize();
}
template<typename T>
int ArrayQueue<T>::getCapacity()const{
return arr->getCapacity();
}
template<typename T>
bool ArrayQueue<T>::isEmpty()const {
return arr->isEmpty();
}
template<typename T>
void ArrayQueue<T>::enqueue(const T &t) {
arr->addLast(t);
}
template<typename T>
T ArrayQueue<T>::dequeue() {
return arr->removeFirst();
}
template<typename T>
T ArrayQueue<T>::getFront() {
return arr->getFirst();
}
template<typename T>
void ArrayQueue<T>::print() const {
std::cout << "ArrayQueue: size = " << arr->getSize() << ", capacity = " << arr->getCapacity() << std::endl;
std::cout << "bottom ";
arr->print();
}
template<typename T>
ArrayQueue<T>::~ArrayQueue() {
delete arr;
arr = nullptr;
}
#endif //C___ARRAYQUEUE_H
main.cpp
int main()
{
ArrayQueue<int>*aq = new ArrayQueue<int>();
for(int i = 0;i<10;++i)
{
aq->enqueue(i);
}
aq->print();
cout<<endl;
cout<<"aq->isEmpty()"<<aq->isEmpty()<<endl;
cout<<endl;
cout<<"aq->getCapacity"<<aq->getCapacity()<<endl;
cout<<endl;
cout<<"aq->getSize()"<<aq->getSize()<<endl;
cout<<endl;
cout<<"aq->dequeue()"<<aq->dequeue()<<endl;
cout<<endl;
cout<<"aq->getFront()"<<aq->getFront()<<endl;
cout<<endl;
aq->print();
return 0;
}