当前位置: 首页 > article >正文

用C语言实现队列的顺序结构

用C语言实现队列的初始化、队列的判空操作、入队操作、出队运算、取队头元素运算、顺序打印队列。

#include<stdio.h>
#define QueueSize 100
typedef char ElemType;
typedef struct//队列结构体
{
	ElemType data[QueueSize];//保存队中元素
	int front, rear;//队头和队尾指针
} SqQueue;
//队列的初始化
void InitQueue(SqQueue*qu)
{
	qu->rear = qu->front;//指针初始化
}
//队列的判空操作
int QueueEmpty(SqQueue*qu)//判断队空运算
{
	if (qu->front ==qu->rear)//队空
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
//入队操作
int EnQueue(SqQueue*qu, ElemType x)
{
	if ((qu->rear + 1) % QueueSize == qu->front) //表示队列已经满了
	{
		return 0;
	}
	qu->rear = (qu->rear + 1) % QueueSize; //队尾指针进1
	qu->data[qu->rear] = x;
	return 1;
}
//出队运算
int DeQueue(SqQueue*qu, ElemType*x)
{
	if (QueueEmpty(qu))
	{
		return 0;
	}
	*x = qu->data[qu->front];
	qu->front = (qu->front + 1) % QueueSize;//队头指针进1
	return 1;
}
//取队头元素运算
int GetHead(SqQueue*qu, ElemType*x)
{
	if (qu->rear == qu->front)//队空
	{
		return 0;
	}
	*x = qu->data[(qu->front + 1) % QueueSize];
	{
		return 1;
	}
}
//顺序打印队列
void printQueue(SqQueue* qu)
{
	for (int i = qu->front; i != qu->rear; i = (i + 1) % QueueSize)
	{
		printf("%c", qu->data[i+1]);
	}
	printf("\n");
}
void main()
{
	SqQueue qu;
	ElemType e;
	InitQueue(&qu);
	printf("队%s\n", (QueueEmpty(&qu) == 1 ? "空" : "不空"));
	printf("a进队\n");
	EnQueue(&qu,'a');
	printf("b进队\n");
	EnQueue(&qu,'b');
	printf("c进队\n");
	EnQueue(&qu,'c');
	printf("d进队\n");
	EnQueue(&qu,'d');
	printf("打印队列中的元素:");
	printQueue(&qu);
	printf("队%s\n", (QueueEmpty(&qu) == 1 ? "空" : "不空"));
	GetHead(&qu,&e);
	printf("队头元素:%c\n",e);
	printf("出队次序:");
	printQueue(&qu);
	printf("\n");
}

运行结果: 


http://www.kler.cn/news/162953.html

相关文章:

  • 4.PyTorch——优化器
  • Bert-vits2新版本V2.1英文模型本地训练以及中英文混合推理(mix)
  • 【c语言指针详解】指针的基本概念和用法
  • 面对对象基础案例
  • React中使用react-json-view展示JSON数据
  • 2023年甘肃职业院校技能大赛(中职教师组)网络安全竞赛样题(五)
  • 持续集成交付CICD:CentOS 7 安装 Nexus 3.63
  • Flask template+Vue +项目中include引入其他模版(其他模版也会用到vue)的使用探索
  • 独立服务器的主要应用方向有什么_Maizyun
  • 云原生(Cloud Native)——概念,技术,背景,优缺点,实践例子
  • Vue3如何优雅的跨组件通信
  • C++_对C数据类型的扩展
  • 整数以及浮点数在内存中的存储
  • 等待和通知
  • 联想电脑重装系统Win10步骤和详细教程
  • Ubuntu22.04 交叉编译fdk-aac for Rv1106
  • 【软件安装】VMware安装Centos7虚拟机并且设置静态IP,实现Windows和Centos7网络互相访问
  • Tair(2):Tair安装部署
  • 检测判断IP合法性API接口
  • Ubuntu 修改当前用户的名称
  • 膳食补充剂行业分析:2028年中国市场有望突破3700亿元
  • 有限空间作业中毒窒息事故频发,汉威科技创新方案护航
  • Flink 使用场景
  • K8S集群优化的可执行优化
  • 带大家做一个,易上手的家常辣子鸡
  • HbuilderX使用Uniapp+Vue3安装uview-plus
  • redis-学习笔记(list)
  • Conda常用命令总结
  • Apache Lucene 9.9,有史以来最快的 Lucene 版本
  • Python:核心知识点整理大全7-笔记