#include<iostream>
#include<algorithm>
#define OK 1
#define ERROR 0
using namespace std;
//队列的储存结构
typedef struct QNode
{
int data;
struct QNode* next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front;//队头指针
QueuePtr rear;//队尾指针
}LinkQueue;
//队列的储存结构
int InitQueue(LinkQueue& Q)//队列初始化
{
Q.front = Q.rear = new QNode;
Q.front->next = NULL;
return OK;
}
int EnQueue(LinkQueue& Q, int e)//元素入队
{
QueuePtr p = new QNode;
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return OK;
}
int DeQueue(LinkQueue& Q)//元素出队
{
if (Q.front == Q.rear) return ERROR;//堆空
QueuePtr p = new QNode;
p = Q.front->next;
Q.front->next = p->next;
if (Q.rear == p)//最后一个元素
{
Q.rear = Q.front;
}
int e = p->data;
delete p;
return e;
}
int GetHead(LinkQueue Q)//取队头元素
{
if (Q.front == Q.rear) return ERROR;
return Q.front->next->data;
}
int main()
{
//测试
LinkQueue p;//定义队列
InitQueue(p);//初始化队列
int e;
for (int i = 0; i < 3; i++)//元素入队
{
cin >> e;
EnQueue(p, e);
}
while (p.front->next)//元素出队
{
cout << DeQueue(p) << " ";
}
return 0;
//测试
}
链队
最新推荐文章于 2021-08-23 10:28:31 发布