,h
#pragma once
const int kong = 0;
template<class T>//类模板
struct Node
{
T data;
struct Node<T>*next;
};
template<class T>
class LinkQueue {
public:
LinkQueue() {
rear = new Node<T>;
rear->next = rear;
}
~LinkQueue();
void EnQueue(T x);
T DeQueue();
T GetFront();
bool Empty() {
return rear->next == rear ? true: false;
}
private:
Node<T>*rear;
};
.cpp
#include"标头.h"
//入队
template<class T>
void LinkQueue<T>::EnQueue(T x) {
Node<T>*f = rear->next;
rear->next = new Node<T>;
rear = rear->next;
rear->next = x;
rear->next = f;
}
//出队
template<class T>
T LinkQueue<T>::DeQueue() {
if (Empty())throw "underflow";
Node<T>*front = rear->next;
Node<T>*p = front->next;
front->next = p->next;
if ((front->next) == front)rear = front;
T x = p->data;
delete p;
return x;
}
//查找第一个元素
template<class T>
T LinkQueue<T>::GetFront() {
if (Empty())return kong;
return rear->next->next;
}
//析构函数
LinkQueue<class T>::~LinkQueue() {
Node<T>*front = rear->next;
while (front) {
rear = front->next;
delete front;
front = rear;
}
}
等一个解决办法!