/*定义链队*/
template <class T>
class LinkQueue {
public:
linkQueue(); //构造函数
bool isEmpty(); //判空函数
void enQueue(const T&); //进队函数
T deQueue(); //出队函数
T getHead(); //返回对头元素
~linkQueue(); //析构函数
private:
//定义结点
struct node {
T data; //结点中保存的数据
node* next; //后继指针
node():next(NULL){}; //构造函数,无参数
node(const T& x, node* n = NULL):data(x), next(n){}; //构造函数,带参数
~node(){};
};
node* head; //队头指针
node* tail; //队尾指针
};
//构造函数
template <class T>
LinkQueue<T>::linkQueue() {
head = tail = NULL;
}
//判空函数
template <class T>
bool LinkQueue<T>::isEmpty() {
return head == NULL;
}
//进队函数
template <class T>
void LinkQueue<T>::enQueue(const T& x) {
node* add = new node(x);
if (head) {
tail->next = add;
tail = add;
} else {
head = tail = add;
}
}
//出队函数
template <class T>
T LinkQueue<T>::deQueue() {
if (isEmpty()) {
throw 1;
}
T d = head->data;
node* tmp = head;
head = head->next;
delete tmp;
return d;
}
//返回对头元素
template <class T>
T LinkQueue<T>::getHead() {
if (isEmpty()) {
throw 1;
}
return head->data;
}
//析构函数
template <class T>
LinkQueue<T>::~linkQueue() {
while (head != NULL) {
node* tmp = head;
head = head->next;
delete tmp;
}
}
C++类模板实现链队
最新推荐文章于 2021-11-21 20:13:13 发布