```c++
#include <stdio.h>
#include <malloc.h>
#include<iostream>
using namespace std;
template<class ElemType>
struct Node
{
ElemType data;//数据
struct Node<ElemType>* next;//后继指针
};
template<class ElemType>
class Queue
{
public:
Node<ElemType>* tail;//尾指针
public:
Queue();
bool InQueue(ElemType x);
bool OutQueue();
bool GetHead();
};
template<class ElemType>
Queue<ElemType>::Queue()
{
Queue<ElemType>* queue = new Queue<ElemType>;
//malloc(sizeof(Queue));
Node<ElemType>* node = new Node<ElemType>;
//malloc(sizeof(Node));
queue->tail = node;
queue->tail->nex = queue->tail;//指向自己
return queue;
}
template<class ElemType>
bool Queue<ElemType>::InQueue(ElemType x)
{
Node<ElemType>* p = new Node<ElemType>;
//malloc(sizeof(Node<ElemType>));
p->data = x;
p->next = this->tail->next;
this->tail->next = p;
this->tail = p;
return true;
}
template<class ElemType>
bool Queue<ElemType>::OutQueue()
{
Node<ElemType>* node;
node = this->tail->next->next;
if (this->tail->next->next == this->tail->next)
{
cout << "队列为空" << endl;
return false;//判断为空
}
ElemType x = node->data;
if (node == this->tail)//只有一个结点
{
this->tail = this->tail->next;
this->tail->next = this->tail;
}
else
this->tail->next->next = node->next;
free(node);//清除数据
cout << "删除数据为:" << x << endl;
return true;
}
template<class ElemType>
bool Queue<ElemType>::GetHead()
{
Node<ElemType>* node;
node = this->tail->next->next;
if (this->tail->next->next == this->tail->next)
{
cout << "队列为空" << endl;
return false;//判断为空
}
ElemType x = node->data;
cout << "队头数据为" << x << endl;
return true;
}
int main()
{
Queue<int>* queue=NULL;
queue->InQueue(1);
queue->OutQueue();
queue->GetHead();
}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/743944322366142.jpg "#left")