#ifndef LINKQUEUE_H
#define LINKQUEUE_H
#include <stdio.h>
#include <stdlib.h>
#include "../../▲01 绪论/Status.h"
#if !defined BANKQUEUING_H && \
!defined TRI_BINARYTREE_H
typedef int QElemType_L;
#endif
typedef struct QNode
{
QElemType_L data;
struct QNode *next;
}QNode;
typedef QNode* QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
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));
#endif
#ifndef LINKQUEUE_C
#define LINKQUEUE_C
#include "LinkQueue.h"
Status InitQueue_L(LinkQueue *Q)
{
(*Q).front = (*Q).rear = (QueuePtr)malloc(sizeof(QNode));
if(!(*Q).front)
exit(OVERFLOW);
(*Q).front->next = NULL;
return OK;
}
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)
{
if(Q.front==Q.rear)
return TRUE;
else
return 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)
{
QueuePtr p;
if(Q.front==Q.rear)
return ERROR;
p = Q.front->next;
*e = p->data;
return OK;
}
Status EnQueue_L(LinkQueue *Q, QElemType_L e)
{
QueuePtr p;
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_L(LinkQueue *Q, QElemType_L *e)
{
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;
}
void QueueTraverse_L(LinkQueue Q, void (Visit)(QElemType_L))
{
QueuePtr p;
p = Q.front->next;
while(p)
{
Visit(p->data);
p = p->next;
}
}
#endif
#include <stdio.h>
#include "LinkQueue.c"
void PrintElem(QElemType_L e);
int main(int argc, char **argv)
{
LinkQueue Q;
int i;
QElemType_L e;
printf("▼1\n▲函数 InitQueue_L 测试...\n");
{
printf("初始化链队 Q ...\n");
InitQueue_L(&Q);
printf("\n");
}
PressEnter;
printf("▼4\n▲函数 QueueEmpty_L 测试...\n");
{
QueueEmpty_L(Q) ? printf(" Q 为空!!\n") : printf(" Q 不为空!\n");
printf("\n");
}
PressEnter;
printf("▼7\n▲函数 EnQueue_L 测试...\n");
{
for(i=1; i<=6; i++)
{
printf("元素 \"%2d\" 入队,", 2*i);
EnQueue_L(&Q, 2*i);
printf("(累计第 %d 个元素)...\n", QueueLength_L(Q));
}
printf("\n");
}
PressEnter;
printf("▼9\n▲函数 QueueTraverse_L 测试...\n");
{
printf(" Q 中的元素为:Q = ");
QueueTraverse_L(Q, PrintElem);
printf("\n\n");
}
PressEnter;
printf("▼8\n▲函数 DeQueue_L 测试...\n");
{
DeQueue_L(&Q, &e);
printf("队头元素 \"%d\" 出队...\n", e);
printf(" Q 中的元素为:Q = ");
QueueTraverse_L(Q, PrintElem);
printf("\n\n");
}
PressEnter;
printf("▼5\n▲函数 QueueLength_L 测试...\n");
{
i = QueueLength_L(Q);
printf(" Q 的长度为 %d \n", i);
printf("\n");
}
PressEnter;
printf("▼6\n▲函数 GetHead_L 测试...\n");
{
GetHead_L(Q, &e);
printf("队头元素的值为 \"%d\" \n", e);
printf("\n");
}
PressEnter;
printf("▼3\n▲函数 ClearQueue_L 测试...\n");
{
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");
}
PressEnter;
printf("▼2\n▲函数 DestroyQueue_L 测试...\n");
{
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");
}
PressEnter;
return 0;
}
void PrintElem(QElemType_L e)
{
printf("%d ", e);
}