一、关键点
1. 队头队尾
队头是front,队尾是rear;
队头出数据,队尾进数据;
队头指针front 不存数据;
当front == rear时 队列为空;
清空或取出数据至队列为空时记得将rear指针移到front上;
QueuePtr p = Q->front->next;
*e = p->data;
Q->front->next = p->next;
if(p==Q->rear)
{
Q->rear = Q->front;
}
2.取出数据要free()结点
QueuePtr p = Q->front->next;
*e = p->data;
Q->front->next = p->next;
if(p==Q->rear)
{
Q->rear = Q->front;
}
free(p);
二、代码
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OK 1
typedef int QElemType_L;
typedef int Status;
typedef struct QNode
{
QElemType_L data;
QNode *next;
} QNode,*QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
} LinkQueue;
void PrintElem(QElemType_L e);
Status InitQueue_L(LinkQueue *Q);
void ClearQueue_L(LinkQueue *Q);
void DestroyQueue_L(LinkQueue *Q);
Status QueueEmpty_L(LinkQueue Q);
int QueueLength_L(LinkQueue Q);
Status GetHead_L(LinkQueue Q, QElemType_L *e);
Status EnQueue_L(LinkQueue *Q, QElemType_L e);
Status DeQueue_L(LinkQueue *Q, QElemType_L *e);
void QueueTraverse_L(LinkQueue Q, void(Visit)(QElemType_L));
Status InitQueue_L(LinkQueue *Q)
{
QueuePtr base = (QueuePtr)malloc(sizeof(QNode));
if(!base)
{
exit(OVERFLOW);
}
Q->front = base;
Q->rear = Q->front;
Q->rear->next = NULL;
}
void ClearQueue_L(LinkQueue *Q)
{
Q->rear = Q->front->next;
while(Q->rear)
{
Q->front->next = Q->rear->next;
free(Q->rear);
Q->rear = Q->front->next;
}
Q->rear = Q->front;
}
void DestroyQueue_L(LinkQueue *Q)
{
while(Q->front)
{
Q->rear = Q->front->next;
free(Q->front);
Q->front = Q->rear;
}
}
Status QueueEmpty_L(LinkQueue Q)
{
return Q.front==Q.rear?TRUE:FALSE;
}
int QueueLength_L(LinkQueue Q)
{
int count = 0;
QueuePtr p = Q.front;
while(p!=Q.rear)
{
count++;
p=p->next;
}
return count;
}
Status GetHead_L(LinkQueue Q, QElemType_L *e)
{
if(Q.front==Q.rear)
{
return ERROR;
}
*e = Q.front->next->data;
return OK;
}
Status EnQueue_L(LinkQueue *Q, QElemType_L e)
{
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if(!p)
{
exit(OVERFLOW);
}
p->data = e;
p->next = Q->rear->next;
Q->rear->next = p;
Q->rear = p;
return OK;
}
Status DeQueue_L(LinkQueue *Q, QElemType_L *e)
{
if(Q->front==Q->rear)
{
return ERROR;
}
QueuePtr p = Q->front->next;
*e = p->data;
Q->front->next = p->next;
if(p==Q->rear)
{
Q->rear = Q->front;
}
free(p);
return OK;
}
void QueueTraverse_L(LinkQueue Q, void(Visit)(QElemType_L))
{
if(Q.front==Q.rear)
{
return;
}
QueuePtr p = Q.front;
while(p!=Q.rear)
{
Visit(p->next->data);
p = p->next;
}
}
void PrintElem(QElemType_L e)
{
printf("%d ", e);
}
int main(int argc, char **argv)
{
LinkQueue Q;
int i;
QElemType_L e;
printf("▼1\n▲函数 InitQueue_L 测试...\n"); //1.函数InitQueue_L测试
{
printf("初始化链队 Q ...\n");
InitQueue_L(&Q);
printf("\n");
}
printf("▼4\n▲函数 QueueEmpty_L 测试...\n"); //4.函数QueueEmpty_L测试
{
QueueEmpty_L(Q) ? printf(" Q 为空!!\n") : printf(" Q 不为空!\n");
printf("\n");
}
printf("▼7\n▲函数 EnQueue_L 测试...\n"); //7.函数EnQueue_L测试
{
for(i=1; i<=6; i++)
{
printf("元素 \"%2d\" 入队,", 2*i);
EnQueue_L(&Q, 2*i);
printf("(累计第 %d 个元素)...\n", QueueLength_L(Q));
}
printf("\n");
}
printf("▼9\n▲函数 QueueTraverse_L 测试...\n");//9.函数QueueTraverse_L测试
{
printf(" Q 中的元素为:Q = ");
QueueTraverse_L(Q, PrintElem);
printf("\n\n");
}
printf("▼8\n▲函数 DeQueue_L 测试...\n"); //8.函数DeQueue_L测试
{
DeQueue_L(&Q, &e);
printf("队头元素 \"%d\" 出队...\n", e);
printf(" Q 中的元素为:Q = ");
QueueTraverse_L(Q, PrintElem);
printf("\n\n");
}
printf("▼5\n▲函数 QueueLength_L 测试...\n"); //5.函数QueueLength_L测试
{
i = QueueLength_L(Q);
printf(" Q 的长度为 %d \n", i);
printf("\n");
}
printf("▼6\n▲函数 GetHead_L 测试...\n"); //6.函数GetHead_L测试
{
GetHead_L(Q, &e);
printf("队头元素的值为 \"%d\" \n", e);
printf("\n");
}
printf("▼3\n▲函数 ClearQueue_L 测试...\n"); //3.函数ClearQueue_L测试
{
printf("清空 Q 前:");
QueueEmpty_L(Q) ? printf(" Q 为空!!\n") : printf(" Q 不为空!\n");
ClearQueue_L(&Q);
printf("清空 Q 后:");
QueueEmpty_L(Q) ? printf(" Q 为空!!\n") : printf(" Q 不为空!\n");
printf("\n");
}
printf("▼2\n▲函数 DestroyQueue_L 测试...\n"); //2.函数DestroyQueue_L测试
{
printf("销毁 Q 前:");
Q.front!=NULL && Q.rear!=NULL ? printf(" Q 存在!\n") : printf(" Q 不存在!!\n");
DestroyQueue_L(&Q);
printf("销毁 Q 后:");
Q.front!=NULL && Q.rear!=NULL ? printf(" Q 存在!\n") : printf(" Q 不存在!!\n");
printf("\n");
}
return 0;
}