队列的链式表示和实现
和栈一样,队列也是操作受限的线性表,只允许在队尾插入元素,在队头删除元素。对于链队列结构,为了便于插入元素,设立了队尾指针。这样,插入元素的操作与队列长度无关。
链队列的结构:
具有两个元素(4,7)的链队列:
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */
typedef int QElemType; /* 定义队列元素的类型 */
#include<malloc.h> /* malloc()等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<process.h> /* exit() */
/* 函数结果状态代码 */
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
/* -------------------------- 单链队列--队列的链式存储结构 -----------------------------*/
typedef struct QNode
{
QElemType data;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct
{
QueuePtr front, rear; /* 队头、队尾指针 */
}LinkQueue;
/* ----------------------------------------------------------------------------*/
void visit(QElemType i)
{
printf("%d ", i);
}
/* 链队列的基本操作(9个) */
/* -------------------- 基本操作的函数原型说明 ---------------------*/
Status InitQueue(LinkQueue *Q);
Status DestroyQueue(LinkQueue *Q);
Status ClearQueue(LinkQueue *Q);
Status QueueEmpty(LinkQueue Q);
int QueueLength(LinkQueue Q);
Status GetHead_Q(LinkQueue Q, QElemType *e);
Status EnQueue(LinkQueue *Q, QElemType e);
Status DeQueue(LinkQueue *Q, QElemType *e);
Status QueueTraverse(LinkQueue Q, void(*vi)(QElemType));
/* --------------------------------- 基本操作的实现 ------------------------------------*/
Status InitQueue(LinkQueue *Q)
{ /* 构造一个空队列Q */
(*Q).front = (*Q).rear = (QueuePtr)malloc(sizeof(QNode));
if (!(*Q).front)
exit(OVERFLOW);
(*Q).front->next = NULL;
return OK;
}
Status DestroyQueue(LinkQueue *Q)
{ /* 销毁队列Q(无论空否均可) */
while ((*Q).front)
{
(*Q).rear = (*Q).front->next;
free((*Q).front);
(*Q).front = (*Q).rear;
}
return OK;
}
Status ClearQueue(LinkQueue *Q)
{ /* 将Q清为空队列 */
QueuePtr p, q;
(*Q).rear = (*Q).front;
p = (*Q).front->next;
(*Q).front->next = NULL;
while (p)
{
q = p;
p = p->next;
free(q);
}
return OK;
}
Status QueueEmpty(LinkQueue Q)
{ /* 若Q为空队列,则返回TRUE,否则返回FALSE */
if (Q.front == Q.rear)
return TRUE;
else
return FALSE;
}
int QueueLength(LinkQueue Q)
{ /* 求队列的长度 */
int i = 0;
QueuePtr p;
p = Q.front;
while (Q.rear != p)
{
i++;
p = p->next;
}
return i;
}
Status GetHead_Q(LinkQueue Q, QElemType *e) /* 避免与bo2-6.c重名 */
{ /* 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR */
QueuePtr p;
if (Q.front == Q.rear)
return ERROR;
p = Q.front->next;
*e = p->data;
return OK;
}
Status EnQueue(LinkQueue *Q, QElemType e)
{ /* 插入元素e为Q的新的队尾元素 */
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if (!p) /* 存储分配失败 */
exit(OVERFLOW);
p->data = e;
p->next = NULL;
(*Q).rear->next = p;
(*Q).rear = p;
return OK;
}
Status DeQueue(LinkQueue *Q, QElemType *e)
{ /* 若队列不空,删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR */
QueuePtr p;
if ((*Q).front == (*Q).rear)
return ERROR;
p = (*Q).front->next;
*e = p->data;
(*Q).front->next = p->next;
if ((*Q).rear == p)
(*Q).rear = (*Q).front;
free(p);
return OK;
}
Status QueueTraverse(LinkQueue Q, void(*vi)(QElemType))
{ /* 从队头到队尾依次对队列Q中每个元素调用函数vi()。一旦vi失败,则操作失败 */
QueuePtr p;
p = Q.front->next;
while (p)
{
vi(p->data);
p = p->next;
}
printf("\n");
return OK;
}
/* 检验以上操作的主程序 */
void main()
{
int i;
QElemType d;
LinkQueue q;
i = InitQueue(&q);
if (i)
printf("成功地构造了一个空队列!\n");
printf("是否空队列?%d(1:空 0:否) ", QueueEmpty(q));
printf("队列的长度为%d\n", QueueLength(q));
EnQueue(&q, -5);
EnQueue(&q, 5);
EnQueue(&q, 10);
printf("插入3个元素(-5,5,10)后,队列的长度为%d\n", QueueLength(q));
printf("是否空队列?%d(1:空 0:否) ", QueueEmpty(q));
printf("队列的元素依次为:");
QueueTraverse(q, visit);
i = GetHead_Q(q, &d);
if (i == OK)
printf("队头元素是:%d\n", d);
DeQueue(&q, &d);
printf("删除了队头元素%d\n", d);
i = GetHead_Q(q, &d);
if (i == OK)
printf("新的队头元素是:%d\n", d);
ClearQueue(&q);
printf("清空队列后,q.front=%u q.rear=%u q.front->next=%u\n", q.front, q.rear, q.front->next);
DestroyQueue(&q);
printf("销毁队列后,q.front=%u q.rear=%u\n", q.front, q.rear);
}
运行结果: