队列的顺序表示和实现

队列的顺序表示和实现:

编写一个程序实现顺序队列的各种基本运算(采用循环队列),并在此基础上设计一个主程序,完成如下功能:

  1. 初始化队列
  2. 入队
  3. 出队
  4. 判断队列是否为空
  5. 清空队列
  6. 销毁队列
  7. 获取队列长度
  8. 获取队头元素
  9. 遍历队列
代码(C语言):
/*顺序循环队列及操作的算法实现*/
/*front为队头元素的当前位置,rear为队尾元素的下一个位置*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define INITSIZE 10 //队列的初始容量
#define INCREMENT 5 //队列的动态增长长度

/*队列的数据结构*/
typedef int ElemType;
typedef struct
{
	ElemType* data; //初始化的动态存储空间
	int front;      //头指针
	int rear;       //尾指针
	int QueueSize;  //队列的当前容量
} SqQueue;

/*函数声明列表*/
int InitQueue(SqQueue* Q);            //初始化队列
void DestoryQueue(SqQueue* Q);        //销毁队列
void ClearQueue(SqQueue* Q);          //清空队列
int QueueEmpty(SqQueue Q);            //判断队列是否为空
int GetHead(SqQueue Q, ElemType* e);  //获取队头元素
int QueueLength(SqQueue Q);           //获取队列长度
void QueueTraverse(SqQueue Q);        //遍历并输出队列
int EnQueue(SqQueue* Q, ElemType e);  //入队
int DeQueue(SqQueue* Q, ElemType* e); //出队

/*主函数*/
int main(void)
{
	int i = 0, k = 0, n = 0;
	ElemType d = 0;
	srand((unsigned)time(NULL));
	SqQueue Q;
	InitQueue(&Q);    //初始化队列
	printf("输入队列初始元素个数:");
	scanf("%d", &n);
	printf("队列初始化成功!\n执行%d次入队操作:\n", n);
	for (i = 0; i < n; i++)
	{
		d = rand() % 90 + 10;
		printf("第%-2d次:元素%d入队\n", i + 1, d);
		EnQueue(&Q, d); //入队
	}
	printf("\n打印当前队列:");
	QueueTraverse(Q);   //遍历并输出队列
	k = QueueLength(Q); //获取队列长度
	printf("\n当前队列长度为:%d\n\n", QueueLength(Q));
	printf("执行%d次出队操作:\n", k / 2);
	for (i = 0; i < k / 2; i++)
	{
		DeQueue(&Q, &d); //出队
		printf("第%-2d次:元素%d出队", i + 1, d);
		printf("\t打印当前队列:");
		QueueTraverse(Q);
	}
	n = GetHead(Q, &d); //获取队头元素,保存到d中
	if (n) printf("\n队头元素的值:%d\n", d);
	printf("执行清空队列操作\n");
	ClearQueue(&Q);     //清空队列
	printf("清空队列后,队列是否为空:n=%d(1,为空;0,为不空)\n", QueueEmpty(Q));
	system("pause");
	return 0;
}

/*函数实现部分*/
/*【功能1 初始化队列】*/
int InitQueue(SqQueue* Q)
{
	/*构造一个空队列*/
	Q->data = (ElemType*)malloc(INITSIZE * sizeof(ElemType));
	if (!Q->data) return 0; //存储分配失败则返回0
	Q->front = Q->rear = 0;
	Q->QueueSize = INITSIZE;
	return 1; //初始化成功则返回1
}

/*【功能2 销毁队列】*/
void DestoryQueue(SqQueue* Q)
{
	if (Q->data) free(Q->data);
	Q->data = NULL;
	Q->QueueSize = Q->front = Q->rear = 0;
}

/*【功能3 清空队列】*/
void ClearQueue(SqQueue* Q)
{
	Q->front = Q->rear = 0;
}

/*【功能4 判断队列是否为空】*/
int QueueEmpty(SqQueue Q)
{
	if (Q.front == Q.rear) return 1; //返回1代表为空队列,返回0代表不为空队列
	else return 0;
}

/*【功能5 获取队头元素】*/
int GetHead(SqQueue Q, ElemType* e)
{
	if (Q.front == Q.rear) return 0;
	*e = Q.data[Q.front];/*将队头元素的值赋给e*/
	return 1;
}

/*【功能6 获取队列长度】*/
int QueueLength(SqQueue Q)
{
	return (Q.rear - Q.front + Q.QueueSize) % Q.QueueSize;
}

/*【功能7 遍历并输出队列】*/
void QueueTraverse(SqQueue Q)
{
	int i = Q.front; /*i最初指向队头元素*/
	while (i != Q.rear)
	{
		printf("%d ", Q.data[i]);
		i = (i + 1) % Q.QueueSize;
	}
	printf("\n");
}

/*【功能8 入队】*/
int EnQueue(SqQueue* Q, ElemType e)
{
	if ((Q->rear + 1) % Q->QueueSize == Q->front)//循环队列满,牺牲一个空间,rear指向队尾元素的下一个位置
	{
		Q->data = (ElemType*)realloc(Q->data, sizeof(ElemType) * (Q->QueueSize + INCREMENT));
		Q->QueueSize += INCREMENT;
	}
	if (!Q->data) return 0;
	Q->data[Q->rear] = e;                  //将e插在队尾
	Q->rear = (Q->rear + 1) % Q->QueueSize;//队尾指针加1
	return 1;
}

/*【功能9 出队】*/
int DeQueue(SqQueue* Q, ElemType* e)
{
	if (Q->front == Q->rear) return 0; //队列为空则返回0,代表出队失败
	*e = Q->data[Q->front];            //将队头元素的值赋给e
	Q->front = (Q->front + 1) % Q->QueueSize;
	return 1;
}
执行结果:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aabyte

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值