数据结构:循环顺序队列的基本操作

简介:使用C语言实现循环顺序队列的基本操作,并且测试了程序的正确性。

头文件SqQueue.h:包括数据类型定义、函数声明

#ifndef SQ_QUEUE_H
#define SQ_QUEUE_H

#include "stdio.h"

#define MAXSIZE 10

typedef int ElemType;

typedef enum{
	FALSE,
	TRUE
}BOOL;
	
typedef struct{
	ElemType data[MAXSIZE]; //用静态数组存放队列元素
	int front,rear; //队列头指针和对位指针
}SqQueue;

void InitQueue(SqQueue *Q);
BOOL QueueEmpty(SqQueue Q);
BOOL EnQueue(SqQueue *Q, ElemType e);
BOOL DeQueue(SqQueue *Q, ElemType *e);
BOOL GetHead(SqQueue Q, ElemType *e);
int QueueLength(SqQueue Q);

#endif

源文件SqQueue.c:包括循环顺序队列基本操作的C语言实现

#include "SqQueue.h"

/********************************************************************************
 @brief  初始化队列
 @param  Q:队列地址 
 @retval void
 @note   初始化后队头及队尾指针指向0
*********************************************************************************/
void InitQueue(SqQueue *Q)
{
	Q->front = 0;

	Q->rear = 0;
}

/********************************************************************************
 @brief  判断队空
 @param  Q:队列 
 @retval TRUE :队空
         FALSE:队非空
 @note   void
*********************************************************************************/
BOOL QueueEmpty(SqQueue Q)
{
	return (BOOL)(Q.front == Q.rear);
}

/********************************************************************************
 @brief  入队操作
 @param  Q:队列的地址
         e:入队元素
 @retval TRUE :操作成功
         FALSE:操作失败
 @note   void
*********************************************************************************/
BOOL EnQueue(SqQueue *Q, ElemType e)
{
	if ((Q->rear+1)%MAXSIZE == Q->front){ //队满则报错
		return FALSE;
	}

	Q->data[Q->rear] = e; //将e插入队尾

	Q->rear = (Q->rear+1)%MAXSIZE; //队尾指针后移

	return TRUE;
}

/********************************************************************************
 @brief  出队操作
 @param  Q:队列的地址
         e:出队元素的数值
 @retval TRUE :操作成功
         FALSE:操作失败
 @note   void
*********************************************************************************/
BOOL DeQueue(SqQueue *Q, ElemType *e)
{
	if (Q->rear ==Q->front){ //队空则报错
		return FALSE;
	}

	*e = Q->data[Q->front]; //出队

	Q->front = (Q->front+1)%MAXSIZE; //队头后移

	return TRUE;
}

/********************************************************************************
 @brief  读取队头元素
 @param  Q:队列
         e:队头元素
 @retval TRUE :操作成功
         FALSE:操作失败
 @note   void
*********************************************************************************/
BOOL GetHead(SqQueue Q, ElemType *e)
{
	if (Q.rear == Q.front){ //队空则报错
		return FALSE;
	}

	*e = Q.data[Q.front]; //获取队头元素

	return TRUE;
}

/********************************************************************************
 @brief  计算队长
 @param  Q:队列
 @retval 队列长度
 @note   void
*********************************************************************************/
int QueueLength(SqQueue Q)
{
	return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
}

主函数main.c:测试基本操作程序的正确性

/***************************************头文件***********************************/
#include "stdio.h"
#include "stdlib.h"
#include "SqQueue.h"
/**********************************宏定义及类型定义******************************/

/************************************定义全局变量********************************/

/********************************************************************************
 @brief  打印栈中所有元素
 @param  S:顺序栈 
 @retval TRUE :操作成功
         FALSE:操作失败
 @note   void
*********************************************************************************/

/*************************************函数声明***********************************/
#define ENTRY_QUEUE_SIZE 7
#define DELETE_QUEUE_SIZE 7
/********************************************************************************
 @brief  打印队中所有元素
 @param  void
 @retval void
 @note   void
*********************************************************************************/
BOOL PrintQueueAllElem(SqQueue Q)
{
	int i;
	int index;

	if (Q.rear == Q.front){ //队空则报错
		return FALSE;
	}

	index = Q.front;

	do
	{
		if ((index == Q.rear) && ((i+1)%MAXSIZE == Q.front)){
			printf("位序%d为未使用的位序\n", index);
		}
		else{
			printf("位序%d的元素值为%d\n", index, Q.data[index]);
		}

		index = (index+1)%MAXSIZE; //指针后移
	}
	while (index != Q.rear); //后移指针指向队尾指针时,所有元素访问完毕

	return TRUE;
}

/********************************************************************************
 @brief  测试队列基本操作
 @param  void
 @retval void
 @note   void
*********************************************************************************/
void TestSqQueue(void)
{
	int i;
	SqQueue Q;
	ElemType e;

	InitQueue(&Q); //初始化队列

	printf("\n判断队列是否为空...\n"); //测试队列为空操作
	if (QueueEmpty(Q)){
		printf("判断结果:队列为空\n");
	}
	else{
		printf("判断结果:队列非空\n");
	}

	printf("\n正在进行入队%d个元素操作...\n", ENTRY_QUEUE_SIZE); //测试入队操作
	for (i = 0; i < ENTRY_QUEUE_SIZE; i++)
	{
		if (EnQueue(&Q, i+1)){
			if (Q.rear == 0){
				printf("入队成功,位序%d入队元素为%d\n", MAXSIZE-1, i+1);
			}
			else{
				printf("入队成功,位序%d入队元素为%d\n", Q.rear-1, i+1);
			}	
		}
		else{
			printf("入队错误\n");
		}
	}
	
	printf("\n打印队列中的所有元素...\n");
	PrintQueueAllElem(Q);

	printf("\n正在进行出队操作...\n"); //测试出队操作
	for (i = 0; i < DELETE_QUEUE_SIZE; i++)
	{
		if (DeQueue(&Q, &e)){
			if (Q.front == 0){
				printf("出队成功,位序%d出队元素为%d\n", MAXSIZE-1, e);
			}
			else{
				printf("出队成功,位序%d出队元素为%d\n", Q.front-1, e);
			}
		}
		else{
			printf("出队错误\n");
		}
	}

	printf("\n打印队列中的所有元素...\n");
	if (PrintQueueAllElem(Q) == FALSE){
		printf("队空\n");
	}

	printf("\n正在进行入队%d个元素操作...\n", ENTRY_QUEUE_SIZE); //测试循环入队操作
	for (i = 0; i < ENTRY_QUEUE_SIZE; i++)
	{
		if (EnQueue(&Q, i+1)){
			if (Q.rear == 0){
				printf("入队成功,位序%d入队元素为%d\n", MAXSIZE-1, i+1);
			}
			else{
				printf("入队成功,位序%d入队元素为%d\n", Q.rear-1, i+1);
			}	
		}
		else{
			printf("入队错误\n");
		}
	}

	printf("\n打印队列中的所有元素...\n");
	PrintQueueAllElem(Q);
}

/********************************************************************************
 @brief  主函数
 @param  void 
 @retval void
 @note   void
*********************************************************************************/
int main(void)
{
	TestSqQueue();

	system("pause");

	return 0;
}

程序运行结果 :
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值