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

数据结构之循环队列的顺序结构基本操作-基本结构-初始化-入队-出队-判断队列是否为空-获取队头元素

数据结构之循环队列的顺序结构基本操作-基本结构-初始化-入队-出队-判断队列是否为空-获取队头元素

满队列:(rear+1)%MAXSIZE==front

队尾:(rear+1)%MAXSIZE

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

#define MAXSIZE 100
typedef int ElemType;

typedef struct 
{
	ElemType *data;
	int front;
	int rear;
	
}Queue;

//初始化
Queue* initQueue()
{
	Queue *q = (Queue*)malloc(sizeof(Queue));
	q->data = (ElemType*)malloc(sizeof(ElemType) * MAXSIZE);
	q->front = 0;
	q->rear = 0;
	return q;
}

//判断队列是否为空
int isEmpty(Queue *Q)
{
	if (Q->front == Q->rear)
	{
		printf("空的\n");
		return 1;
	}
	else
	{
		return 0;	
	}
}

//入队
int equeue(Queue *Q, ElemType e)
{
	if ((Q->rear + 1) % MAXSIZE == Q->front)
	{
		printf("满了\n");
		return 0;
	}
	Q->data[Q->rear] = e;
	Q->rear = (Q->rear + 1) % MAXSIZE;
	return 1;
}

//出队
int dequeue(Queue *Q, ElemType *e)
{
	if (Q->front == Q->rear)
	{
		printf("空的\n");
		return 0;
	}
	*e = Q->data[Q->front];
	Q->front = (Q->front + 1) % MAXSIZE;
	return 1;
}

//获取队头元素
int getHead(Queue *Q, ElemType *e)
{
	if (Q->front == Q->rear)
	{
		printf("空的\n");
		return 0;
	}
	*e = Q->data[Q->front];
	return 1;
}

int main()
{
	
	Queue *q = initQueue();

	equeue(q, 10);
	equeue(q, 20);
	equeue(q, 30);
	equeue(q, 40);
	equeue(q, 50);//入队10,20,30,40,50
	
	ElemType e;
	dequeue(q, &e);//出队
	printf("%d\n",e);
	dequeue(q, &e);//出队
	printf("%d\n",e);
	
	getHead(q, &e);//获取队头
	printf("%d\n",e);

	return 0;
}

运行:

http://www.dtcms.com/a/90831.html

相关文章:

  • 当AI重构编程范式:Java 24的进化逻辑与技术深水区的战略突围
  • 【ESP32S3】esp32获取串口数据并通过http上传到前端
  • 造成服务器网络连接不稳定的原因是什么?
  • 【Python】pillow库学习笔记2-ImageFilter类和ImageEnhance类
  • PHP If...Else 语句详解
  • 用户模块——自定义业务异常
  • 【react】实现路由返回拦截的多种方式
  • Three学习入门(四)
  • DeepSeek助力文案,智能音箱如何改变你的生活?
  • 【企业网络安全防护】一体化管控平台:企业办公效率与安全兼得!
  • 计算机专业本科毕业设计一般有哪些选题?
  • 突破反爬困境:SDK架构设计,为什么选择独立服务模式(四)
  • Web前端之JavaScript的DOM操作冷门API
  • 一个简单的用C#实现的分布式雪花ID算法
  • OpenCV图像拼接(6)根据权重图对源图像进行归一化处理函数normalizeUsingWeightMap()
  • 鸿蒙第三方解析(一)
  • 3. 轴指令(omron 机器自动化控制器)——>MC_GearIn
  • ollama迁移已下载的单个模型到服务器
  • mysql部署错误
  • UI-TARS 体验
  • k8s存储介绍(四)hostpath
  • 23种设计模式-观察者(Observer)设计模式
  • django入门教程之自定义中间件【七】
  • leetcode:136. 只出现一次的数字(python3解法)
  • python 格式化利器
  • 一文读懂Sql Server读写分离和分库分表
  • 鼠标在客户区内按下左键和双击右键
  • 光谱范围与颜色感知的关系
  • Spring学习笔记05——Spring Boot的文件结构2(POJO类)
  • 基于 PHP 内置类及函数的免杀 WebShell