链队 LinkQueue

/**************************************
 *                                    *
 * 文件夹: ▲03 栈和队列\07 LinkQueue *
 *                                    *
 * 文件名: LinkQueue.h                *
 *                                    *
 * 内  容: 链队相关操作列表           *
 *                                    *
 *************************************/

#ifndef LINKQUEUE_H
#define LINKQUEUE_H

#include <stdio.h>
#include <stdlib.h>						//提供malloc、realloc、free、exit原型
#include "../../▲01 绪论/Status.h"		//**▲01 绪论**//

/* 链队类型定义 */
/* 在模拟银行排队、二叉树三叉链表存储的算法中,QElemType_L需重新定义*/
#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);
/*━━━━━━━━━┓
┃(01)初始化链队Q。 ┃
┗━━━━━━━━━*/

void ClearQueue_L(LinkQueue *Q);
/*━━━━━━┓
┃(02)置空Q。 ┃
┗━━━━━━*/

void DestroyQueue_L(LinkQueue *Q);
/*━━━━━━┓
┃(03)销毁Q。 ┃
┗━━━━━━*/

Status QueueEmpty_L(LinkQueue Q);
/*━━━━━━━━━━┓
┃(04)判断Q是否为空。 ┃
┗━━━━━━━━━━*/

int QueueLength_L(LinkQueue Q);
/*━━━━━━━━━━┓
┃(05)返回Q元素个数。 ┃
┗━━━━━━━━━━*/

Status GetHead_L(LinkQueue Q, QElemType_L *e);
/*━━━━━━━━━━━┓
┃(06)用e获取队头元素。 ┃
┗━━━━━━━━━━━*/

Status EnQueue_L(LinkQueue *Q, QElemType_L e);
/*━━━━━━━━┓
┃(07)元素e入队。 ┃
┗━━━━━━━━*/

Status DeQueue_L(LinkQueue *Q, QElemType_L *e);
/*━━━━━━━━┓
┃(08)元素e出队。 ┃
┗━━━━━━━━*/

void QueueTraverse_L(LinkQueue Q, void(Visit)(QElemType_L));
/*━━━━━━━┓
┃(09)访问队列。┃
┗━━━━━━━*/

#endif
/**************************************
 *                                    *
 * 文件夹: ▲03 栈和队列\07 LinkQueue *
 *                                    *
 * 文件名: LinkQueue.c                *
 *                                    *
 *************************************/

#ifndef LINKQUEUE_C
#define LINKQUEUE_C

#include "LinkQueue.h" 					//**▲03 栈和队列**//
	
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
/**************************************
 *                                    *
 * 文件夹: ▲03 栈和队列\07 LinkQueue *
 *                                    *
 * 内  容: 链队相关函数测试           *
 *                                    *
 *************************************/

#include <stdio.h>
#include "LinkQueue.c" 		//**03 栈和队列**//
	
void PrintElem(QElemType_L 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");
	} 
	PressEnter;
	
	printf("▼4\n▲函数 QueueEmpty_L 测试...\n");	//4.函数QueueEmpty_L测试
	{
		QueueEmpty_L(Q) ? printf(" Q 为空!!\n") : printf(" Q 不为空!\n");
		printf("\n");
	} 
	PressEnter;
	
	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");
	} 
	PressEnter;
	
	printf("▼9\n▲函数 QueueTraverse_L 测试...\n");//9.函数QueueTraverse_L测试
	{
		printf(" Q 中的元素为:Q = ");						 
		QueueTraverse_L(Q, PrintElem);
		printf("\n\n");
	} 
	PressEnter;
	
	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");
	} 
	PressEnter;
	
	printf("▼5\n▲函数 QueueLength_L 测试...\n");	//5.函数QueueLength_L测试
	{
		i = QueueLength_L(Q);
		printf(" Q 的长度为 %d \n", i);
		printf("\n");
	} 
	PressEnter;
	
	printf("▼6\n▲函数 GetHead_L 测试...\n");		//6.函数GetHead_L测试
	{
		GetHead_L(Q, &e);
		printf("队头元素的值为 \"%d\" \n", e);
		printf("\n");
	} 
	PressEnter;
	
	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");
	} 
	PressEnter;
	
	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");
	} 
	PressEnter;
		
	return 0;
}

void PrintElem(QElemType_L e)
{
	printf("%d ", e);
}

猜你喜欢

转载自blog.csdn.net/qq_42815188/article/details/87825990