循环队列的基本操作

本文介绍了有关循环队列的基本操作。

已经过调试没有很大问题。
如有错误,还请批评指正。

运行代码如下:

一、循环队列的定义:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXQSIZE 100//最大队列长度

typedef int QElemtype;
typedef int Status;

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

二、循环队列的基本操作:

//函数声明
Status InitQueue(SqQueue* Q);//构造一个空队列InitQueue(&Q)

int QueueLength(SqQueue Q);//返回队列元素个数,即队列长度QueueLength(Q)

Status EnQueue(SqQueue* Q, QElemtype e);//插入e为队列尾元素EnQueue(&Q, e)

Status DeQueue(SqQueue* Q, QElemtype* e);//删除队列头元素DeQueue(&Q, &e)

Status QueueEmpty(SqQueue* Q);//判断队列是否为空QueueEmpty(&Q)

Status ClearQueue(SqQueue* Q);//清空队列ClearQueue(&Q)

Status DestroyQueue(SqQueue* Q);//销毁队列DestroyQueue(&Q)

void QueueTraverse(SqQueue* Q, void(*visit)(QElemtype));//遍历函数,输出队列 
                                                        //QueueTraverse(&Q,Print)
void Print(QElemtype e);//visit

1、构造一个空队列:

/*---------------------------------------------------------------------------------------
功能:构造一个空队列
参数:1、队列指针
输出:OK、ERROR
*/
//构造一个空队列InitQueue(&Q)
Status InitQueue(SqQueue* Q)
{
	Q->base = (QElemtype*)malloc(sizeof(QElemtype) * MAXQSIZE);
	if (Q->base)
	{
		Q->front = Q->rear = 0;
	}
	else
	{
		return ERROR;
	}
	return OK;
}

2、返回队列元素个数,即队列长度:

/*---------------------------------------------------------------------------------------
功能:返回队列元素个数,即队列长度
参数:1、队列
输出:队列长度
*/
//返回队列元素个数,即队列长度QueueLength(Q)
int QueueLength(SqQueue Q)
{
	return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
}

3、插入e为队列尾元素:

/*---------------------------------------------------------------------------------------
功能:插入e为队列尾元素
参数:1、队列指针 2、元素
输出:OK、ERROR
*/
//插入e为队列尾元素EnQueue(&Q, e)
Status EnQueue(SqQueue* Q, QElemtype e)
{
	if ((Q->rear + 1) % MAXQSIZE == Q->front)//队列已满
	{
		return ERROR;
	}
	Q->base[Q->rear] = e;
	Q->rear = (Q->rear + 1) % MAXQSIZE;
	return OK;
}

4、删除队列头元素

/*---------------------------------------------------------------------------------------
功能:删除队列头元素
参数:1、队列指针 2、元素
输出:OK、ERROR
*/
//删除队列头元素DeQueue(&Q, &e)
Status DeQueue(SqQueue* Q, QElemtype* e)
{
	if (Q->front == Q->rear)
	{
		return ERROR;
	}
	*e = Q->base[Q->front];
	Q->front = (Q->front + 1) % MAXQSIZE;
	return OK;
}

5、判断队列是否为空:

/*---------------------------------------------------------------------------------------
功能:判断队列是否为空
参数:1、队列指针
输出:TRUE、FALSE
*/
//判断队列是否为空QueueEmpty(&Q)
Status QueueEmpty(SqQueue* Q)
{
	if (Q->rear == Q->front)
	{
		return TRUE;
	}
	return FALSE;
}

6、清空队列:

/*---------------------------------------------------------------------------------------
功能:清空队列
参数:1、队列指针
输出:OK、ERROR
*/
//清空队列ClearQueue(&Q)
Status ClearQueue(SqQueue* Q)
{
	Q->front = Q->rear = 0;
	return OK;
}

7、销毁队列:

/*---------------------------------------------------------------------------------------
功能:销毁队列
参数:1、队列指针
输出:OK、ERROR
*/
//销毁队列DestroyQueue(&Q)
Status DestroyQueue(SqQueue* Q)
{
	if (Q->base)
	{
		free(Q->base);
	}
	Q->base = 0;
	Q->front = Q->rear = 0;
	return OK;
}

8、遍历函数,输出队列:

/*---------------------------------------------------------------------------------------
功能:遍历函数,输出队列
参数:1、队列指针 2、visit函数指针
输出:空
*/
//遍历函数,输出队列QueueTraverse(&Q,Print)
void QueueTraverse(SqQueue* Q, void(*visit)(QElemtype))
{
	int i = Q->front;
	while (i != Q->rear)
	{
		visit(Q->base[i]);
		i = (i + 1) % MAXQSIZE;
	}
	printf("\n");
}
void Print(QElemtype e)
{
	printf("%d ", e);
}

三、测试主程序

void menu()
{
	printf("**********************************************\n");
	printf("****** 1.创建循环队列    2.求队列长度   ******\n");
	printf("****** 3.插入队尾元素    4.删除队头元素 ******\n");
	printf("****** 5.判断是否为空    6.清空队列     ******\n");
	printf("****** 7.销毁队列        8.输出队列     ******\n");
	printf("****** 0.退出                           ******\n");
	printf("**********************************************\n");
}
int main()
{
	menu();
	SqQueue Q;
	QElemtype e = 0;
	int input = 1;
	while (input)
	{
		printf("请输入要执行的操作:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			if (InitQueue(&Q))
			{
				printf("创建成功!!!\n");
			}
			else
			{
				printf("创建失败~\n");
			}
			break;
		case 2:
			printf("队列的长度为:%d\n", QueueLength(Q));
			break;
		case 3:
			printf("请输入要插入的值:>");
			scanf("%d", &e);
			if (EnQueue(&Q, e))
			{
				printf("成功插入!!!\n");
			}
			else
			{
				printf("插入失败~\n");
			}
			break;
		case 4:
			if (DeQueue(&Q, &e))
			{
				printf("队列头元素删除成功!!!\n");
			}
			else
			{
				printf("队列为空~\n");
			}
			break;
		case 5:
			if (QueueEmpty(&Q))
			{
				printf("队列为空!!!\n");
			}
			else
			{
				printf("队列不为空!!!\n");
			}
			break;
		case 6:
			if (ClearQueue(&Q))
			{
				printf("清空成功!!!\n");
			}
			else
			{
				printf("清空失败~\n");
			}
			break;
		case 7:
			if (DestroyQueue(&Q))
			{
				printf("销毁成功!!!\n");
			}
			else
			{
				printf("销毁失败~\n");
			}
			break;
		case 8:
			printf("此时队列元素为:");
			QueueTraverse(&Q, Print);
			break;
		case 0:
			printf("退出成功!!!\n");
			break;
		default:
			printf("输入错误~请重新输入!!!\n");
			break;
		}
	}
	system("pause");
	return 0;
}

  • 5
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
循环队列基本操作包括初始化、入队、出队、求队列长度、求队头元素、判空、清空和销毁等。以下是循环队列基本操作的实现方法: 1. 初始化:创建一个空的循环队列,需要指定队列的最大长度。 2. 入队:将元素插入到队列的队尾,如果队列已满则无法插入。 3. 出队:将队列的队头元素删除并返回,如果队列为空则无法删除。 4. 求队列长度:返回队列中元素的个数。 5. 求队头元素:返回队列的队头元素,但不删除。 6. 判空:判断队列是否为空。 7. 清空:清空队列中的所有元素。 8. 销毁:销毁队列,释放内存空间。 以下是一个基于顺序表实现的循环队列代码示例: ``` #define MAXSIZE 100 // 队列的最大长度 typedef struct { int data[MAXSIZE]; // 存储队列元素的数组 int front; // 队头指针 int rear; // 队尾指针 } CircularQueue; // 初始化循环队列 void InitQueue(CircularQueue *q) { q->front = q->rear = 0; } // 判断队列是否为空 int IsEmpty(CircularQueue *q) { return q->front == q->rear; } // 判断队列是否已满 int IsFull(CircularQueue *q) { return (q->rear + 1) % MAXSIZE == q->front; } // 入队 int EnQueue(CircularQueue *q, int x) { if (IsFull(q)) { return 0; // 队列已满,无法插入 } q->data[q->rear] = x; q->rear = (q->rear + 1) % MAXSIZE; return 1; } // 出队 int DeQueue(CircularQueue *q, int *x) { if (IsEmpty(q)) { return 0; // 队列为空,无法删除 } *x = q->data[q->front]; q->front = (q->front + 1) % MAXSIZE; return 1; } // 求队列长度 int QueueLength(CircularQueue *q) { return (q->rear - q->front + MAXSIZE) % MAXSIZE; } // 求队头元素 int GetHead(CircularQueue *q, int *x) { if (IsEmpty(q)) { return 0; // 队列为空,无法获取队头元素 } *x = q->data[q->front]; return 1; } // 清空队列 void ClearQueue(CircularQueue *q) { q->front = q->rear = 0; } // 销毁队列 void DestroyQueue(CircularQueue *q) { free(q); } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值