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

数据结构--循环队列、链队

基础知识

//循环队列数据结构
typedef struct
{
QElemType data[MaxQSize];//数据域
int front,rear; //队头队尾指针
}SqQueue;

//链队结点数据结构
typedef struct QNode
{
int data;//数据域
struct QNode* next;//指针域

}QNode, * QueuePtr;
typedef struct
{
struct QNode* front, * rear;//rear指针指向队尾 用于入队 front指针指向队头 用于出队
}LinkQueue;

队列是一种常见的数据结构,它遵循==先进先出(FIFO)==的原则。以下是队列的基本操作:
1、入队(Enqueue):将元素添加到队列的末尾。
2、出队(Dequeue):从队列的头部移除一个元素,并返回其值。
3、队列长度(Size):获取队列中元素的个数。
4、队列是否为空(IsEmpty):检查队列是否为空。
5、获取队首元素(Front):获取队列头部的元素值,但不移除该元素。

C++ 实现队列的基本操作的示例代码:

#include <iostream>
#include <queue>int main() {std::queue<int> myQueue;// 入队myQueue.push(10);myQueue.push(20);myQueue.push(30);// 队列长度std::cout << "队列长度:" << myQueue.size() << std::endl;// 出队int frontElement = myQueue.front();myQueue.pop();std::cout << "出队元素:" << frontElement << std::endl;// 获取队首元素std::cout << "队首元素:" << myQueue.front() << std::endl;// 队列是否为空std::cout << "队列是否为空:" << (myQueue.empty() ? "是" : "否") << std::endl;return 0;
}

在C语言中,队列的基本操作可以通过结构体和指针来实现。

#include <stdio.h>
#include <stdlib.h>// 定义队列结构体
typedef struct {int *array;  // 存储队列元素的数组int front;   // 队首索引int rear;    // 队尾索引int size;    // 队列的容量
} Queue;// 初始化队列
void initQueue(Queue *queue, int capacity) {queue->array = (int*)malloc(capacity * sizeof(int));queue->front = 0;queue->rear = -1;queue->size = 0;
}// 销毁队列
void destroyQueue(Queue *queue) {free(queue->array);
}// 入队
void enqueue(Queue *queue, int element) {queue->rear = (queue->rear + 1) % queue->size;queue->array[queue->rear] = element;queue->size++;
}// 出队
int dequeue(Queue *queue) {int dequeuedElement = queue->array[queue->front];queue->front = (queue->front + 1) % queue->size;queue->size--;return dequeuedElement;
}// 获取队列长度
int getSize(Queue *queue) {return queue->size;
}// 检查队列是否为空
int isEmpty(Queue *queue) {return queue->size == 0;
}// 获取队首元素
int getFront(Queue *queue) {return queue->array[queue->front];
}int main() {Queue myQueue;initQueue(&myQueue, 5);// 入队enqueue(&myQueue, 10);enqueue(&myQueue, 20);enqueue(&myQueue, 30);// 队列长度printf("队列长度:%d\n", getSize(&myQueue));// 出队int frontElement = dequeue(&myQueue);printf("出队元素:%d\n", frontElement);// 获取队首元素printf("队首元素:%d\n", getFront(&myQueue));// 队列是否为空printf("队列是否为空:%s\n", isEmpty(&myQueue) ? "是" : "否");destroyQueue(&myQueue);return 0;
}

循环队列

#define MaxSize 100
typedef struct { int data[MaxSize]; int front, rear; }SqQueue;//***********************************   基本操作函数  *******************************************int InitQueue(SqQueue &Q)
{Q.front = Q.rear = 0;return 1;}
bool QueueEmpty(SqQueue& Q) {if (Q.front != Q.rear)	return true;else   return false;
}//入队:尾部加1;	出队:头部加1
bool EnQueue(SqQueue& Q, int& e) {if (Q.front ==Q.rear)	return false;e = Q.data[Q.rear];Q.rear = (Q.rear + 1) % MaxSize; //指针加1 取模return true;
}bool DeQueue(SqQueue& Q, int &e) {if (Q.front == Q.rear)	return false;e = Q.data[Q.front];Q.front = (Q.front + 1) % MaxSize; //指针加1 取模return true;
}bool GetHead(SqQueue& Q, int &e)
{if (Q.front == Q.rear)	return false;//队空 					e = Q.data[Q.front];return true;
}//********************************功能实现函数**************************************//void EnterToQueue(SqQueue& Q)
{int n; int e; int flag;printf("请输入入队元素个数(>=1):\n");scanf("%d", &n);for (int i = 0; i < n; i++){printf("请输入第%d个元素的值:", i + 1);scanf("%d", &e);flag = EnQueue(Q, e);if (flag)printf("%d已入队\n", e);else { printf("队已满!!!\n"); break; }}
}void DeleteFromQueue(SqQueue& Q)
{int n; int e; int flag;printf("请输入出队元素个数(>=1):\n");scanf("%d", &n);for (int i = 0; i < n; i++){flag = DeQueue(Q, e);if (flag)printf("%d已出队\n", e);else { printf("队已空!!!\n"); break; }}
}void GetHeadOfQueue(SqQueue Q)
{int e; bool flag;flag = GetHead(Q, e);if (flag)printf("队头元素为:%d\n", e);else printf("队已空!!!\n");
}void menu() {printf("********1.入队          2.出队*********\n");printf("********3.取队头元素    4.退出*********\n");
}int main() {SqQueue Q;int choice = 0;;InitQueue(Q);while (1){menu();printf("请输入菜单序号:\n");scanf("%d", &choice);if (choice == 4) break;switch (choice){case 1:EnterToQueue(Q); break;case 2:DeleteFromQueue(Q); break;case 3:GetHeadOfQueue(Q); break;default:printf("输入错误!!!\n");}system("pause");system("cls");}return 0;
}

链队

#define MaxSize 100//链队结点数据结构
typedef struct QNode
{int data;//数据域struct QNode* next;//指针域}QNode, * QueuePtr;
typedef struct
{struct QNode* front, * rear;//rear指针指向队尾 用于入队 front指针指向队头 用于出队
}LinkQueue;//***********************************   基本操作函数  *******************************************int InitQueue(LinkQueue &Q)
{Q.front = Q.rear = new QNode;Q.front->next = NULL;return 1;}int EnQueue(LinkQueue& Q, int& e) {QNode* p;p = new QNode;//生成新节点p->data = e;    //赋值p->next = NULL;Q.rear->next = p;//加入队尾Q.rear = p;      //尾指针后移return 1;
}bool DeQueue(LinkQueue &Q, int &e) {QueuePtr p;if (Q.front == Q.rear)return false;//队空e = Q.front->next->data;           //e返回值 之前写的Q.front->data 炸了,头结点没数据的,一定要注意头结点p = Q.front->next;                //保留,一会儿释放空间Q.front->next = p->next;          //出队,注意Q.front->next 不是Q.front 还有头结点if (Q.rear == p)Q.rear = Q.front;    //最后一个元素出队,rear指向头结点free(p);return true;
}//取队顶函数 用e返回
bool GetHead(LinkQueue &Q, int &e)
{if (Q.front == Q.rear)	return false;//队空 					e = Q.front->next->data;return true;
}//********************************功能实现函数**************************************//void EnterToQueue(LinkQueue& Q)
{int n; int e; int flag;printf("请输入入队元素个数(>=1):\n");scanf("%d", &n);for (int i = 0; i < n; i++){printf("请输入第%d个元素的值:", i + 1);scanf("%d", &e);flag = EnQueue(Q, e);if (flag)printf("%d已入队\n", e);}
}void DeleteFromQueue(LinkQueue& Q)
{int n; int e; int flag;printf("请输入出队元素个数(>=1):\n");scanf("%d", &n);for (int i = 0; i < n; i++){flag = DeQueue(Q, e);if (flag)printf("%d已出队\n", e);else { printf("队已空!!!\n"); break; }}
}void GetHeadOfQueue(LinkQueue Q)
{int e; bool flag;flag = GetHead(Q, e);if (flag)printf("队头元素为:%d\n", e);else printf("队已空!!!\n");
}void menu() {printf("********1.入队          2.出队*********\n");printf("********3.取队头元素    4.退出*********\n");
}int main() {LinkQueue Q;int choice = 0;;InitQueue(Q);while (1){menu();printf("请输入菜单序号:\n");scanf("%d", &choice);if (choice == 4) break;switch (choice){case 1:EnterToQueue(Q); break;case 2:DeleteFromQueue(Q); break;case 3:GetHeadOfQueue(Q); break;default:printf("输入错误!!!\n");}system("pause");system("cls");}return 0;
}

相关文章:

数据结构--循环队列、链队

基础知识 //循环队列数据结构 typedef struct { QElemType data[MaxQSize];//数据域 int front,rear; //队头队尾指针 }SqQueue; //链队结点数据结构 typedef struct QNode { int data;//数据域 struct QNode* next;//指针域 }QNode, * QueuePtr; typedef struct { struct Q…...

hbuilderx主题色分享-github风格

效果 步骤 hbuilderx总共有三种主题&#xff0c;绿柔主题Default,酷黑主题Monokai,雅黑主题Atom One Dark,修改主题色是基于三种主题之一的&#xff0c;不能直接创建一个新主题&#xff0c;比如下方配置是基于Atom One Dark(对象名为[Atom One Dark])&#xff0c;则当前hbuild…...

【C++】类与对象(1)

文章目录 前言一、什么是类1.类的定义2.类的访问限定符3.类的作用域 二、类的实例化三、类对象的存储方式四、this指针总结 前言 C语言是面向过程的&#xff0c;关注的是过程&#xff0c;分析出求解问题的步骤&#xff0c;通过函数调用逐步解决问题。C是基于面向对象的&#x…...

Java课题笔记~ MyBatis核心配置

一、核心配置文件概览 MyBatis配置文件中有MyBatis框架的核心配置&#xff0c;负责对MyBatis进行全局管理。它包含许多控制MyBatis功能的重要元素。 <configuration><!--设置配置文件--><properties><property name"" value""/>…...

从0开始自学网络安全(黑客)

前言 黑客技能是一项非常复杂和专业的技能&#xff0c;需要广泛的计算机知识和网络安全知识。你可以参考下面一些学习步骤&#xff0c;系统自学网络安全。 在学习之前&#xff0c;要给自己定一个目标或者思考一下要达到一个什么样的水平&#xff0c;是学完找工作&#xff08;…...

kotlin 编写一个简单的天气预报app(四)增加界面显示

编写界面来显示返回的数据 用户友好性&#xff1a;通过界面设计和用户体验优化&#xff0c;可以使天气信息更易读、易理解和易操作。有效的界面设计可以提高用户满意度并提供更好的交互体验。 增加城市名字的TextView <TextViewandroid:id"id/textViewCityName"…...

英语不好能学好Python吗?Python常用英文单词汇总

前言 嗨喽&#xff0c;大家好呀~这里是爱看美女的茜茜呐 有些小可爱对英语好不好对学习python有没有什么影响有着很深的疑惑。 其实python学习&#xff0c;主要靠多敲多练&#xff0c;主打一个熟能生巧 那今天我就给大家带来Python常用英文单词汇总&#xff0c; 新手期小可…...

Counting Stars 2023“钉耙编程”中国大学生算法设计超级联赛(5)hdu7335

Problem - 7335 题目大意&#xff1a;如果一个点连接着k个点&#xff0c;就称这k1个点构成k星图&#xff0c;现给出一个大小为n的图&#xff0c;问2星图的数量^3星图的数量^...^n星图的数量是多少 3<n<1e6;1<m<1e6 思路&#xff1a;因为边数总共不超过1e6&#…...

浅谈document.write()输出样式

浅谈document.write()输出样式 js中的最基本的命令之一&#xff1a;document.write&#xff08;&#xff09;&#xff0c;用于简单的打印内容到页面上&#xff0c;可以逐字打印你需要的内容——document.write("content"),这里content就是需要输出的内容&#xff1b;…...

AIGC(Artificial Intelligence and Graph Computing)职业发展路径和前景如何?

目录 一、AIGC 基本概念二、AIGC 市场规模三、AIGC 未来发展前景四、AIGC 职业发展路径五、AIGC 技能要求六、AIGC 相关公司 AIGC&#xff08;Artificial Intelligence and Graph Computing&#xff09;是人工智能和图计算的结合&#xff0c;它是一种用于处理大规模复杂数据的计…...

MySql006——基本的SELECT查询语句

在《MySql003——结构化查询语言SQL基础知识》中&#xff0c;我们学习了有关SQL的基础知识&#xff0c;也知道SQL中查询语句SELECT使用最为频繁 接下来我们将学习一些基本的SELECT查询语句 一、SELECT语句的通用语法 在MySQL数据库中&#xff0c;使用SELECT语句可以查询数据…...

【啥都生】分类项目中的模型搭建代码解析

def build_model(cfg):if isinstance(cfg, list):modules [eval(cfg_.pop("type"))(**cfg_) for cfg_ in cfg]return Sequential(*modules)else:return eval(cfg.pop("type"))(**cfg)b站up啥都生维护的分类项目 这段代码的功能是完成模型搭建&#xff0c;…...

Ubuntu出现了内部错误

使用的Ubuntu版本是18.04&#xff0c;使用的时候弹出对话框说出现了内部错误&#xff0c;好奇是哪里出现了错误&#xff0c;查找了一下解决的办法&#xff0c;记录一下。 参考解决方案&#xff1a;ubantu出现了内部错误 一旦程序崩溃过一次&#xff0c;就会生成一个.crash文件…...

Stable Diffusion AI绘画初学者指南【概述、云端环境搭建】

概述、云端环境搭建 Stable Diffusion 是什么、能干啥&#xff1f; 是一种基于深度学习的图像处理技术&#xff0c;可以生成高质量的图像。它可以在不需要真实图像的情况下&#xff0c;通过文字描述来生成逼真的图像。 可以对图像进行修复、超分辨率转换&#xff0c;将低分辨…...

小程序动态隐藏分享按钮

// 禁用分享 wx.hideShareMenu({menus: [shareAppMessage, shareTimeline] })// 显示分享 wx.showShareMenu({withShareTicket: true,menus: [shareAppMessage, shareTimeline] })//私密消息 wx.updateShareMenu({isPrivateMessage: true, })...

语音合成是什么?如何进行语音合成TTS数据采集?

我们在上一篇讲到语音数据采集分为常见的两种语音数据采集类型&#xff0c;一个是语音识别数据&#xff08;ASR&#xff09;&#xff0c;另一个是语音合成&#xff08;TTS&#xff09;。这一期中&#xff0c;我们将介绍语音合成技术是什么&#xff0c;如何采集语音合成数据和制…...

实用干货!一文读懂Salesforce中6种数据关系类型!

Salesforce中对象之间的数据关系可能是一个棘手的话题。对于创建自定义对象的业务场景&#xff0c;需要决定使用哪些关系类型来扩展Salesforce数据模型。 01 查找关系 查找关系&#xff08;Lookup Relationships&#xff09;是一种松散耦合&#xff08;loosely coupled&…...

Spring引入外部数据源

spring-dataSource.xml 数据源配置文件 <?xml version"1.0" encoding"UTF-8"?> <beans xmlns"http://www.springframework.org/schema/beans"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xmlns:context"h…...

word里的页码问题

封面不需要页码怎么办 一份文档写完&#xff0c;如果需要页码&#xff0c;第一页是封面&#xff0c;封面不需要页码怎么办&#xff1f; 解决&#xff1a;打开页眉页脚&#xff0c;然后把首页不同勾选上&#xff0c;这一页就没有页码了。 目录页与正文页码格式不同怎么办 目录…...

​LeetCode解法汇总142. 环形链表 II

目录链接&#xff1a; 力扣编程题-解法汇总_分享记录-CSDN博客 GitHub同步刷题项目&#xff1a; https://github.com/September26/java-algorithms 原题链接&#xff1a; 力扣 描述&#xff1a; 给定一个链表的头节点 head &#xff0c;返回链表开始入环的第一个节点。 如…...

Leetcode 3576. Transform Array to All Equal Elements

Leetcode 3576. Transform Array to All Equal Elements 1. 解题思路2. 代码实现 题目链接&#xff1a;3576. Transform Array to All Equal Elements 1. 解题思路 这一题思路上就是分别考察一下是否能将其转化为全1或者全-1数组即可。 至于每一种情况是否可以达到&#xf…...

GitHub 趋势日报 (2025年06月08日)

&#x1f4ca; 由 TrendForge 系统生成 | &#x1f310; https://trendforge.devlive.org/ &#x1f310; 本日报中的项目描述已自动翻译为中文 &#x1f4c8; 今日获星趋势图 今日获星趋势图 884 cognee 566 dify 414 HumanSystemOptimization 414 omni-tools 321 note-gen …...

Spring AI 入门:Java 开发者的生成式 AI 实践之路

一、Spring AI 简介 在人工智能技术快速迭代的今天&#xff0c;Spring AI 作为 Spring 生态系统的新生力量&#xff0c;正在成为 Java 开发者拥抱生成式 AI 的最佳选择。该框架通过模块化设计实现了与主流 AI 服务&#xff08;如 OpenAI、Anthropic&#xff09;的无缝对接&…...

Mac下Android Studio扫描根目录卡死问题记录

环境信息 操作系统: macOS 15.5 (Apple M2芯片)Android Studio版本: Meerkat Feature Drop | 2024.3.2 Patch 1 (Build #AI-243.26053.27.2432.13536105, 2025年5月22日构建) 问题现象 在项目开发过程中&#xff0c;提示一个依赖外部头文件的cpp源文件需要同步&#xff0c;点…...

VM虚拟机网络配置(ubuntu24桥接模式):配置静态IP

编辑-虚拟网络编辑器-更改设置 选择桥接模式&#xff0c;然后找到相应的网卡&#xff08;可以查看自己本机的网络连接&#xff09; windows连接的网络点击查看属性 编辑虚拟机设置更改网络配置&#xff0c;选择刚才配置的桥接模式 静态ip设置&#xff1a; 我用的ubuntu24桌…...

scikit-learn机器学习

# 同时添加如下代码, 这样每次环境(kernel)启动的时候只要运行下方代码即可: # Also add the following code, # so that every time the environment (kernel) starts, # just run the following code: import sys sys.path.append(/home/aistudio/external-libraries)机…...

jmeter聚合报告中参数详解

sample、average、min、max、90%line、95%line,99%line、Error错误率、吞吐量Thoughput、KB/sec每秒传输的数据量 sample&#xff08;样本数&#xff09; 表示测试中发送的请求数量&#xff0c;即测试执行了多少次请求。 单位&#xff0c;以个或者次数表示。 示例&#xff1a;…...

TSN交换机正在重构工业网络,PROFINET和EtherCAT会被取代吗?

在工业自动化持续演进的今天&#xff0c;通信网络的角色正变得愈发关键。 2025年6月6日&#xff0c;为期三天的华南国际工业博览会在深圳国际会展中心&#xff08;宝安&#xff09;圆满落幕。作为国内工业通信领域的技术型企业&#xff0c;光路科技&#xff08;Fiberroad&…...

tauri项目,如何在rust端读取电脑环境变量

如果想在前端通过调用来获取环境变量的值&#xff0c;可以通过标准的依赖&#xff1a; std::env::var(name).ok() 想在前端通过调用来获取&#xff0c;可以写一个command函数&#xff1a; #[tauri::command] pub fn get_env_var(name: String) -> Result<String, Stri…...

uniapp 实现腾讯云IM群文件上传下载功能

UniApp 集成腾讯云IM实现群文件上传下载功能全攻略 一、功能背景与技术选型 在团队协作场景中&#xff0c;群文件共享是核心需求之一。本文将介绍如何基于腾讯云IMCOS&#xff0c;在uniapp中实现&#xff1a; 群内文件上传/下载文件元数据管理下载进度追踪跨平台文件预览 二…...