严蔚敏 数据结构(C语言) P60
///单链队列---队列的链式存储结构
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <malloc.h>
using namespace std;
#define OK 1
#define ERROR -1
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int QElemType;
typedef struct QNode
{
QElemType data;//元素的值
struct QNode *next;
} QNode, *QueuePtr;
typedef struct
{
QueuePtr Qfront;//队头指针
QueuePtr Qrear;//队尾指针
}LinkQueue;
//构造一个空队列Q
Status InitQueue(LinkQueue &Q);
//销毁队列Q, 队列Q不再存在
Status DestroyQueue(LinkQueue &Q);
//把Q置为空队列
Status ClearQueue(LinkQueue &Q);
//若Q为空队列,则返回true,否则返回FALSE
Status QueueEmpty(LinkQueue Q);
//返回Q的元素个数,即为队列的长度
int QueueLength(LinkQueue Q);
/*获取队头元素,
若队列不空,则用e返回Q的队头元素,并返回OK,
否则返回ERROR*/
Status GetHead(LinkQueue Q, QElemType &e);
//插入元素e作为新的队列元素
Status EnQueue(LinkQueue &Q, QElemType e);
//若队列不空,则删除Q的队首元素,用e返回其值,并返回OK;否则返回ERROR
Status DeQueue(LinkQueue &Q, QElemType &e);
//读出元素
Status vi(QElemType e);
//从队头到队尾依次对队中的每个元素调用函数visit(). 一旦visit()失败, 则操作失败
Status QueueTraverse(LinkQueue Q, Status(*visit)(QElemType));
int main()
{
LinkQueue Q;
InitQueue(Q);
if(QueueEmpty(Q))
{
printf("创建栈之后, 队列为空\n");
}
for(int i = 0; i < 6; i++)
{
EnQueue(Q, i);
}
if(!QueueEmpty(Q))
{
printf("添加元素之后, 队列不空\n");
}
QueueTraverse(Q, vi);//遍历队列中元素
int length = QueueLength(Q);
printf("队列的长度: %d\n", length);
QElemType e;
GetHead(Q, e);
printf("队首元素: %d\n", e);
printf("队尾元素: %d\n", Q.Qrear->data);
DeQueue(Q, e);
printf("删除的队首元素: %d\n", e);
QueueTraverse(Q, vi);//遍历队列中元素
ClearQueue(Q);
if(QueueEmpty(Q))
{
printf("清空队列之后, 队列为空\n");
}
DestroyQueue(Q);
return 0;
}
//构造一个空队列Q
Status InitQueue(LinkQueue &Q)
{
Q.Qfront = Q.Qrear = (QueuePtr)malloc(sizeof(QNode));
if(!Q.Qfront)
{
printf("存储分配失败!\n");
return ERROR;
}
Q.Qfront = Q.Qrear;
Q.Qfront->next = NULL;
return OK;
}
//销毁队列Q, 队列Q不再存在
Status DestroyQueue(LinkQueue &Q)
{
while(Q.Qfront)
{
Q.Qrear = Q.Qfront->next;
free(Q.Qfront);
Q.Qfront = Q.Qrear;
}
return OK;
}
//把Q置为空队列
Status ClearQueue(LinkQueue &Q)
{
while(Q.Qfront->next)
{
Q.Qrear = Q.Qfront->next;
free(Q.Qfront);
Q.Qfront = Q.Qrear;
}
Q.Qfront->next = NULL;
return OK;
}
//若Q为空队列,则返回true,否则返回FALSE
Status QueueEmpty(LinkQueue Q)
{
if(!Q.Qfront->next)
{
return TRUE;
}
return FALSE;
}
//返回Q的元素个数,即为队列的长度
int QueueLength(LinkQueue Q)
{
int length = 0;
while(Q.Qfront != Q.Qrear)
{
length++;
Q.Qfront = Q.Qfront->next;
}
return length;
}
/*获取队头元素,
若队列不空,则用e返回Q的队头元素,并返回OK,
否则返回ERROR*/
Status GetHead(LinkQueue Q, QElemType &e)
{
if(!Q.Qfront->next)
{
return ERROR;
}
e = Q.Qfront->next->data;
return OK;
}
//插入元素e作为新的队列元素
Status EnQueue(LinkQueue &Q, QElemType e)
{
QueuePtr q = (QueuePtr)malloc(sizeof(QNode));
if(!q)
{
return ERROR;//存储分配失败
}
q->data = e;
q->next = NULL;
Q.Qrear->next = q;
Q.Qrear = q;
return OK;
}
//若队列不空,则删除Q的队首元素,用e返回其值,并返回OK;否则返回ERROR
Status DeQueue(LinkQueue &Q, QElemType &e)
{
if(Q.Qfront == Q.Qrear)
{
return ERROR;
}
QueuePtr q = Q.Qfront->next;
Q.Qfront->next = q->next;
e = q->data;
if(Q.Qrear == q)
{
Q.Qrear = Q.Qfront;
}
free(q);
return OK;
}
//读出元素
Status vi(QElemType e)
{
printf("%d ", e);
return OK;
}
//从队头到队尾依次对队中的每个元素调用函数visit(). 一旦visit()失败, 则操作失败
Status QueueTraverse(LinkQueue Q, Status(*visit)(QElemType))
{
QElemType e;
printf("遍历队列元素: ");
QueuePtr p = Q.Qfront->next;
while(p)
{
e = p->data;
p = p->next;
if(!(*visit)(e))
{
return ERROR;
}
}
printf("\n");
return OK;
}