队列的链式表示和实现----循环队列

本文深入探讨了队列的链式表示,并重点介绍了循环队列的概念和实现细节,帮助读者理解如何在实际编程中运用这种数据结构。
摘要由CSDN通过智能技术生成

头文件:head.h

#include<string.h>
#include<ctype.h>
#include<malloc.h> /* malloc()等 */
#include<limits.h> /* INT_MAX等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include<io.h> /* eof() */
#include<math.h> /* floor(),ceil(),abs() */
#include<process.h> /* exit() */
/* 函数结果状态代码 */
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
/* #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */


#define MAXQSIZE 10				//最大队列长度

typedef int QElemType;

typedef struct{
	QElemType *base;				//初始化的动态非配存储空间
	int front;						//头指针,若队列不为空,指向队列头元素
	int rear;						//尾指针,若队列不为空,指向队列尾元素的下一个位置
}SqQueue;


Status InitQueue(SqQueue *Q);

Status DestoryQueue(SqQueue *Q);

Status ClearQueue(SqQueue *Q);

Boolean QueueEmtpy(SqQueue Q);

int QueueLength(SqQueue Q);

Status GetHead(SqQueue Q, QElemType *e);

Status EnQueue(SqQueue *Q, QElemType e);

Status DeQueue(SqQueue *Q, QElemType *e);

Status QueueTraverse(SqQueue Q);

算法实现:

#include"head.h"

Status InitQueue(SqQueue *Q)
{
	(*Q).base = (SqQueue *)malloc(MAXQSIZE * sizeof(SqQueue));
	if (!(*Q).base)
	{
		printf("初始化失败!");
		system("pause");
		exit(-1);
	}
	(*Q).front = (*Q).rear = 0;

	return OK;
}

Status DestoryQueue(SqQueue *Q)
{
	if ((*Q).base)
		free((*Q).base);
	(*Q).base = NULL;
	(*Q).front = (*Q).rear = 0;

	return OK;
}

Status ClearQueue(SqQueue *Q)
{
	DestoryQueue(Q);
	InitQueue(Q);

	return OK;
}

Boolean QueueEmtpy(SqQueue Q)
{
	if (Q.rear == Q.front)
		return TRUE;
	else
		return ERROR;
}

int QueueLength(SqQueue Q)
{
	if (QueueEmtpy(Q))
		return 0;
	return (Q.rear + MAXQSIZE - Q.front) % MAXQSIZE;
}

Status GetHead(SqQueue Q, QElemType *e)
{
	if (QueueEmtpy(Q))
		return ERROR;
	
	*e = (Q.base)[Q.front];

	return OK;
}

Status EnQueue(SqQueue *Q, QElemType e)
{
	//插入元素e为Q的队尾元素

	if (((*Q).rear + 1) % MAXQSIZE == (*Q).front)				//队列满
		return ERROR;

	(*Q).base[(*Q).rear] = e;
	(*Q).rear = ((*Q).rear + 1) % MAXQSIZE;

	return OK;
}

Status DeQueue(SqQueue *Q, QElemType *e)
{
	if (QueueEmtpy(*Q))
		return ERROR;
	*e = ((*Q).base)[(*Q).front];
	(*Q).front = ((*Q).front + 1) % MAXQSIZE;

	return OK;
}

Status QueueTraverse(SqQueue Q)
{
	int n = 0;
	int len = QueueLength(Q);
	while (n < len)
	{
		printf("%d  ", Q.base[n]);
		n++;
	}
	printf("\n");

	return OK;
}

测试文件:


#include"head.h"

void main()
{
	Status j;
	int i = 0, l;
	QElemType d;
	SqQueue Q;
	InitQueue(&Q);
	printf("初始化队列后,队列空否?%u(1:空 0:否)\n", QueueEmtpy(Q));
	printf("请输入整型队列元素(不超过%d个),-1为提前结束符: ", MAXQSIZE - 1);
	do
	{
		scanf_s("%d", &d);
		if (d == -1)
			break;
		i++;
		EnQueue(&Q, d);
	} while (i<MAXQSIZE - 1);
	printf("队列长度为: %d\n", QueueLength(Q));
	printf("现在队列空否?%u(1:空 0:否)\n", QueueEmtpy(Q));
	printf("连续%d次由队头删除元素,队尾插入元素:\n", MAXQSIZE);
	for (l = 1; l <= MAXQSIZE; l++)
	{
		DeQueue(&Q, &d);
		printf("删除的元素是%d,请输入待插入的元素: ", d);
		scanf_s("%d", &d);
		EnQueue(&Q, d);
	}
	l = QueueLength(Q);
	printf("现在队列中的元素为: \n");
	QueueTraverse(Q);
	printf("共向队尾插入了%d个元素\n", i + MAXQSIZE);
	if (l - 2>0)
		printf("现在由队头删除%d个元素:\n", l - 2);
	while (QueueLength(Q)>2)
	{
		DeQueue(&Q, &d);
		printf("删除的元素值为%d\n", d);
	}
	j = GetHead(Q, &d);
	if (j)
		printf("现在队头元素为: %d\n", d);
	ClearQueue(&Q);
	printf("清空队列后, 队列空否?%u(1:空 0:否)\n", QueueEmtpy(Q));
	DestoryQueue(&Q);

	system("pause");
}


Running Result:

初始化队列后,队列空否?1(1:空 0:否)
请输入整型队列元素(不超过9个),-1为提前结束符: 1 2 3 4 5 6 7 8
-1
队列长度为: 8
现在队列空否?0(1:空 0:否)
连续10次由队头删除元素,队尾插入元素:
删除的元素是1,请输入待插入的元素:
9
删除的元素是2,请输入待插入的元素: 8
删除的元素是3,请输入待插入的元素: 7
删除的元素是4,请输入待插入的元素: 6
删除的元素是5,请输入待插入的元素: 5
删除的元素是6,请输入待插入的元素: 5
删除的元素是7,请输入待插入的元素: 3
删除的元素是8,请输入待插入的元素: 2
删除的元素是9,请输入待插入的元素: 1
删除的元素是8,请输入待插入的元素: 0
现在队列中的元素为:
7  6  5  5  3  2  1  0
共向队尾插入了18个元素
现在由队头删除6个元素:
删除的元素值为7
删除的元素值为6
删除的元素值为5
删除的元素值为5
删除的元素值为3
删除的元素值为2
现在队头元素为: 1
清空队列后, 队列空否?1(1:空 0:否)
请按任意键继续. . .




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值