C语言算法之队列快速入门教程

队列结构

queue.h

#ifndef ZDPC_ALGORITHM_DEV_QUEUE_H
#define ZDPC_ALGORITHM_DEV_QUEUE_H// 队列
typedef struct queue {int *arr; // 容器int cap; // 容量int size; // 元素个数int front; // 队首,用于出队int tail // 队尾,用于入队
} Queue;#endif //ZDPC_ALGORITHM_DEV_QUEUE_H

新建队列

核心代码:

Queue *newQueue(int cap) {// 申请队列内存Queue *queue = malloc(sizeof(Queue) * cap);// 申请容器内存queue->arr = malloc(sizeof(int) * cap);// 其他属性queue->cap = cap;queue->size = 0;queue->front = 0;queue->tail = 0;// 返回return queue;
}

queue.h

#ifndef ZDPC_ALGORITHM_DEV_QUEUE_H
#define ZDPC_ALGORITHM_DEV_QUEUE_H// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"// 队列
typedef struct queue {int *arr; // 容器int cap; // 容量int size; // 元素个数int front; // 队首,用于出队int tail; // 队尾,用于入队
} Queue;extern Queue *newQueue(int cap); // 创建队列#endif //ZDPC_ALGORITHM_DEV_QUEUE_H

queue.c

#include "queue.h"// 新建队列
Queue *newQueue(int cap) {// 申请队列内存Queue *queue = malloc(sizeof(Queue) * cap);// 申请容器内存queue->arr = malloc(sizeof(int) * cap);// 其他属性queue->cap = cap;queue->size = 0;queue->front = 0;queue->tail = 0;// 返回return queue;
}

释放队列

核心代码:

void freeQueue(Queue *queue) {if (queue == NULL) {return;}free(queue->arr);free(queue);
}

queue.h

#ifndef ZDPC_ALGORITHM_DEV_QUEUE_H
#define ZDPC_ALGORITHM_DEV_QUEUE_H// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"// 队列
typedef struct queue {int *arr; // 容器int cap; // 容量int size; // 元素个数int front; // 队首,用于出队int tail; // 队尾,用于入队
} Queue;extern Queue *newQueue(int cap); // 创建队列
extern void freeQueue(Queue *queue); // 释放队列#endif //ZDPC_ALGORITHM_DEV_QUEUE_H

queue.c

#include "queue.h"// 新建队列
Queue *newQueue(int cap) {// 申请队列内存Queue *queue = malloc(sizeof(Queue) * cap);// 申请容器内存queue->arr = malloc(sizeof(int) * cap);// 其他属性queue->cap = cap;queue->size = 0;queue->front = 0;queue->tail = 0;// 返回return queue;
}// 释放队列
void freeQueue(Queue *queue) {if (queue == NULL) {return;}free(queue->arr);free(queue);
}

判断队列是否已满

核心代码:

int isFullQueue(Queue *queue) {if (queue == NULL) {return 0;}return queue->size >= queue->cap;
}

queue.h

#ifndef ZDPC_ALGORITHM_DEV_QUEUE_H
#define ZDPC_ALGORITHM_DEV_QUEUE_H// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"// 队列
typedef struct queue {int *arr; // 容器int cap; // 容量int size; // 元素个数int front; // 队首,用于出队int tail; // 队尾,用于入队
} Queue;extern Queue *newQueue(int cap); // 创建队列
extern void freeQueue(Queue *queue); // 释放队列
extern int isFullQueue(Queue *queue); // 判断队列是否已满#endif //ZDPC_ALGORITHM_DEV_QUEUE_H

queue.c

#include "queue.h"// 新建队列
Queue *newQueue(int cap) {// 申请队列内存Queue *queue = malloc(sizeof(Queue) * cap);// 申请容器内存queue->arr = malloc(sizeof(int) * cap);// 其他属性queue->cap = cap;queue->size = 0;queue->front = 0;queue->tail = 0;// 返回return queue;
}// 释放队列
void freeQueue(Queue *queue) {if (queue == NULL) {return;}free(queue->arr);free(queue);
}// 判断队列是否已满
int isFullQueue(Queue *queue) {if (queue == NULL) {return 0;}return queue->size >= queue->cap;
}

判断队列是否为空

核心代码:

int isEmptyQueue(Queue *queue) {if (queue == NULL) {return 0;}return queue->size <= 0;
}

queue.h

#ifndef ZDPC_ALGORITHM_DEV_QUEUE_H
#define ZDPC_ALGORITHM_DEV_QUEUE_H// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"// 队列
typedef struct queue {int *arr; // 容器int cap; // 容量int size; // 元素个数int front; // 队首,用于出队int tail; // 队尾,用于入队
} Queue;extern Queue *newQueue(int cap); // 创建队列
extern void freeQueue(Queue *queue); // 释放队列
extern int isFullQueue(Queue *queue); // 判断队列是否已满
extern int isEmptyQueue(Queue *queue); // 判断队列是否为空#endif //ZDPC_ALGORITHM_DEV_QUEUE_H

queue.c

#include "queue.h"// 新建队列
Queue *newQueue(int cap) {// 申请队列内存Queue *queue = malloc(sizeof(Queue) * cap);// 申请容器内存queue->arr = malloc(sizeof(int) * cap);// 其他属性queue->cap = cap;queue->size = 0;queue->front = 0;queue->tail = 0;// 返回return queue;
}// 释放队列
void freeQueue(Queue *queue) {if (queue == NULL) {return;}free(queue->arr);free(queue);
}// 判断队列是否已满
int isFullQueue(Queue *queue) {if (queue == NULL) {return 0;}return queue->size >= queue->cap;
}// 判断队列是否为空
int isEmptyQueue(Queue *queue) {if (queue == NULL) {return 0;}return queue->size <= 0;
}

入队

核心代码:

void enQueue(Queue *queue, int value) {if (isFullQueue(queue)) {printf("queue is full\n");return;}if (queue->tail >= queue->cap) {queue->tail = 0;// 循环队列}queue->arr[queue->tail] = value;queue->tail++;queue->size++;
}

queue.h

#ifndef ZDPC_ALGORITHM_DEV_QUEUE_H
#define ZDPC_ALGORITHM_DEV_QUEUE_H// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"// 队列
typedef struct queue {int *arr; // 容器int cap; // 容量int size; // 元素个数int front; // 队首,用于出队int tail; // 队尾,用于入队
} Queue;extern Queue *newQueue(int cap); // 创建队列
extern void freeQueue(Queue *queue); // 释放队列
extern int isFullQueue(Queue *queue); // 判断队列是否已满
extern int isEmptyQueue(Queue *queue); // 判断队列是否为空
extern void enQueue(Queue *queue, int value); // 入队
extern void deQueue(Queue *queue); // 出队#endif //ZDPC_ALGORITHM_DEV_QUEUE_H

queue.c

#include "queue.h"// 新建队列
Queue *newQueue(int cap) {// 申请队列内存Queue *queue = malloc(sizeof(Queue) * cap);// 申请容器内存queue->arr = malloc(sizeof(int) * cap);// 其他属性queue->cap = cap;queue->size = 0;queue->front = 0;queue->tail = 0;// 返回return queue;
}// 释放队列
void freeQueue(Queue *queue) {if (queue == NULL) {return;}free(queue->arr);free(queue);
}// 判断队列是否已满
int isFullQueue(Queue *queue) {if (queue == NULL) {return 0;}return queue->size >= queue->cap;
}// 判断队列是否为空
int isEmptyQueue(Queue *queue) {if (queue == NULL) {return 0;}return queue->size <= 0;
}// 入队
void enQueue(Queue *queue, int value) {if (isFullQueue(queue)) {printf("queue is full\n");return;}if (queue->tail >= queue->cap) {queue->tail = 0;// 循环队列}queue->arr[queue->tail] = value;queue->tail++;queue->size++;
}

出队

核心代码:

int deQueue(Queue *queue) {if (isEmptyQueue(queue)) {printf("queue is empty\n");return -1;}if (queue->front >= queue->cap) {queue->front = 0;// 循环队列}// 取值int value = queue->arr[queue->front];queue->front++;queue->size--;// 返回return value;
}

queue.h

#ifndef ZDPC_ALGORITHM_DEV_QUEUE_H
#define ZDPC_ALGORITHM_DEV_QUEUE_H// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"// 队列
typedef struct queue {int *arr; // 容器int cap; // 容量int size; // 元素个数int front; // 队首,用于出队int tail; // 队尾,用于入队
} Queue;extern Queue *newQueue(int cap); // 创建队列
extern void freeQueue(Queue *queue); // 释放队列
extern int isFullQueue(Queue *queue); // 判断队列是否已满
extern int isEmptyQueue(Queue *queue); // 判断队列是否为空
extern void enQueue(Queue *queue, int value); // 入队
extern int deQueue(Queue *queue); // 出队#endif //ZDPC_ALGORITHM_DEV_QUEUE_H

queue.c

#include "queue.h"// 新建队列
Queue *newQueue(int cap) {// 申请队列内存Queue *queue = malloc(sizeof(Queue) * cap);// 申请容器内存queue->arr = malloc(sizeof(int) * cap);// 其他属性queue->cap = cap;queue->size = 0;queue->front = 0;queue->tail = 0;// 返回return queue;
}// 释放队列
void freeQueue(Queue *queue) {if (queue == NULL) {return;}free(queue->arr);free(queue);
}// 判断队列是否已满
int isFullQueue(Queue *queue) {if (queue == NULL) {return 0;}return queue->size >= queue->cap;
}// 判断队列是否为空
int isEmptyQueue(Queue *queue) {if (queue == NULL) {return 0;}return queue->size <= 0;
}// 入队
void enQueue(Queue *queue, int value) {if (isFullQueue(queue)) {printf("queue is full\n");return;}if (queue->tail >= queue->cap) {queue->tail = 0;// 循环队列}queue->arr[queue->tail] = value;queue->tail++;queue->size++;
}// 出队
int deQueue(Queue *queue) {if (isEmptyQueue(queue)) {printf("queue is empty\n");return -1;}if (queue->front >= queue->cap) {queue->front = 0;// 循环队列}// 取值int value = queue->arr[queue->front];queue->front++;queue->size--;// 返回return value;
}

队列的基本使用

main.c

#include <stdio.h>
#include "queue.h"int main(void) {// 创建队列Queue *queue = newQueue(2);// 入队enQueue(queue, 11);enQueue(queue, 22);enQueue(queue, 33); // 队列满了// 出队printf("%d\n", deQueue(queue));printf("%d\n", deQueue(queue));printf("%d\n", deQueue(queue)); // 队列空了return 0;
}

输出:

queue is full
11
22
queue is empty
-1

队列交替入队和出队

main.c

#include <stdio.h>
#include "queue.h"int main(void) {// 创建队列Queue *queue = newQueue(2);// 入队enQueue(queue, 11);enQueue(queue, 22);printf("%d\n", deQueue(queue));enQueue(queue, 33); // 循环队列printf("%d\n", deQueue(queue));printf("%d\n", deQueue(queue));// 入队enQueue(queue, 11);enQueue(queue, 22);printf("%d\n", deQueue(queue));enQueue(queue, 33); // 循环队列printf("%d\n", deQueue(queue));printf("%d\n", deQueue(queue));return 0;
}

输出:

11
22
33
11
22
33

遍历队列

main.c

#include <stdio.h>
#include "queue.h"int main(void) {// 创建队列Queue *queue = newQueue(2);// 入队enQueue(queue, 11);enQueue(queue, 22);printf("cap=%d size=%d\n", queue->cap, queue->size);// 遍历队列while (!isEmptyQueue(queue)) {printf("%d\n", deQueue(queue));}printf("cap=%d size=%d\n", queue->cap, queue->size);return 0;
}

输出:

cap=2 size=2
11
22
cap=2 size=0

重要:别忘了释放内存!!!

main.c

#include <stdio.h>
#include "queue.h"int main(void) {// 创建队列Queue *queue = newQueue(2);// 入队enQueue(queue, 11);enQueue(queue, 22);printf("cap=%d size=%d\n", queue->cap, queue->size);// 遍历队列while (!isEmptyQueue(queue)) {printf("%d\n", deQueue(queue));}printf("cap=%d size=%d\n", queue->cap, queue->size);// 别忘了释放内存freeQueue(queue);return 0;
}

输出:

cap=2 size=2
11
22
cap=2 size=0

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/9881.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux虚拟机运行“yum install gcc-c++”报错“Loading mirror speeds from cached hostfile”

目录 一、在Linux上安装Redis时&#xff0c;在终端执行命令“yum install gcc-c”时&#xff0c;报错&#xff1a; 二、然后发现linux的网络不通&#xff0c;什么网站都访问不了 三、连上网后&#xff0c;再变换yum源 四、重新运行yum install gcc 一、在Linux上安装Redis时…

Java中55种锁,高级面试题,最新面试题

Java中乐观锁在实际应用中如何解决并发问题&#xff1f; 乐观锁通过假设并发冲突发生概率较低来解决并发问题&#xff0c;主要通过数据版本控制实现。在更新数据前&#xff0c;会检查数据版本是否发生变化&#xff0c;只有在数据版本未变时才允许更新&#xff0c;这样可以避免…

SIUI便携式B超机维修Apogee 1000兽医超声波检查仪宠物医疗彩色多普勒深圳捷达工控维修

带微凸探头的 Siui Apogee 1000 Lite Siui 的最新版本提供了 Apogee 2300 的经济高效、高度便携的替代方案。 非常适合&#xff1a; 小动物兽医实践侧重于主要腹部、甲状腺和小部位的高分辨率成像。 流动兽医。 兽医进行心脏筛查&#xff0c;而不是进行全面超声心动图检查&am…

nginx_01

1.安装 yum install epel-release -y # 安装yum的扩展包 yum install nginx -y systemctl start nginx.service #启动nginx systemctl enable nginx.service # netstat -lntup # 查看端口占用情况 # 可以看到nginx默认占用了80端口 2.nginx配置 # 注意配置文件的语法格式…

haproxy配置安装,实现web服务器负载均衡

一、源码编译安装haproxy 2.x&#xff0c;配置服务启动脚本 1. 下载Haproxy源码包&#xff1a; wget -c https://repo.huaweicloud.com/haproxy/2.4/src/haproxy-2.4.8.tar.gz 2. 下载源码编译工具&#xff1a; yum install -y gcc gcc-c make 3. 下载Haproxy依赖包lua并编…

万物生长大会 | 创邻科技再登杭州准独角兽榜单

近日&#xff0c;由民建中央、中国科协指导&#xff0c;民建浙江省委会、中国投资发展促进会联合办的第八届万物生长大会在杭州举办。 在这场创新创业领域一年一度的盛会上&#xff0c;杭州市创业投资协会联合微链共同发布《2024杭州独角兽&准独角兽企业榜单》。榜单显示&…

什么是股指期货风险度?

期货风险度就像是你账户的“健康指标”&#xff0c;它告诉我们你用了多少资金来持有期货合约&#xff0c;以及你账户里还剩下多少“备用金”。风险度越高&#xff0c;意味着你的“备用金”越少&#xff0c;如果市场突然变化&#xff0c;你可能需要迅速补充资金。 股指期货风险…

Flink Stream API实践

目录 Flink程序的基本构成 获得执行环境&#xff08;environment&#xff09; 加载/初始化数据&#xff08;source&#xff09; 基于文件 基于socket 基于集合 自定义 转换操作&#xff08;transformation&#xff09; 基本转换 物理分区 任务链和资源组 名称和描述…

小红书搞钱美学课-6.0升级版,账号搭建/爆款创作/工具实战/账号变现篇

让我们用视觉撬动流量 课程体系 334253课程权益(5周服务期) 3节账号运营基础课3节自媒体笔记创作课。4节封面设计实操课2次实操加餐分享5次作业指导(一对一)3次答疑直播 课程大纲 一、账号搭建篇 变现模板、精准定位 二、爆款创作篇爆款选题、首图、文案与脚本、快速涨粉…

redis入门学习

一、基础架构 一、应用场景 1、缓存&#xff1a;将后端数据库的热数据缓存到redis中&#xff0c;然后直接从内存中读取数据&#xff0c;提高响应速度。 2、消息队列 3、排行榜&#xff0c;一般用于游戏行业 二、基础结构 1、数据结构 sring&#xff08;字符串&#xff09…

【计算机毕业设计】springboot工资管理系统

人类现已迈入二十一世纪&#xff0c;科学技术日新月异&#xff0c;经济、资讯等各方面都有了非常大的进步&#xff0c;尤其是资讯与 网络技术的飞速发展&#xff0c;对政治、经济、军事、文化等各方面都有了极大的影响。 利用电脑网络的这些便利&#xff0c;发展一套工资管理系…

权力集中,效率提升,中心化模式的优势与挑战

​&#x1f308; 个人主页&#xff1a;danci_ &#x1f525; 系列专栏&#xff1a;《设计模式》 &#x1f4aa;&#x1f3fb; 制定明确可量化的目标&#xff0c;坚持默默的做事。 &#x1f680; 转载自热榜文章&#x1f525;&#xff1a;探索设计模式的魅力&#xff1a;权力集中…

IO 5.10

在一个进程中&#xff0c;创建一个子线程。 主线程负责&#xff1a;向文件中写入数据 子线程负责&#xff1a;从文件中读取数据 要求使用线程的同步逻辑&#xff0c;保证一定在主线程向文件中写入数据成功之后&#xff0c;子线程才开始运行&#xff0c;去读取文件中的数据#incl…

第Ⅷ章-Ⅱ 组合式API使用

第Ⅷ章-Ⅱ 组合式API使用 provide与inject的使用vue 生命周期的用法编程式路由的使用vuex的使用获取DOM的使用setup语法糖setup语法糖的基本结构响应数据的使用其它语法的使用引入组件的使用 父组件传值的使用defineProps 父传子defineEmits 子传父 provide与inject的使用 pro…

学习java的继承

1.什么是继承 java中提供了一个关键字&#xff0c;extends&#xff0c;可以让一个类与另一个类建立起父子关系。 例如 public class B extends A { --- } 在这里&#xff0c;我们称A类为父类&#xff08;也被称为基类或者超类&#xff09;B类称为子类&#xff08;或者是派生…

debian10 (armbian) 配置CUPS 服务

更新apt apt-update安装相关软件 apt-get install ghostscript apt-get install dc apt-get install foomatic-db-engine apt-get install cups3.修改配置文件 nano /etc/cups/cupsd.conf Listen localhost:631改为 Listen 0.0.0.0:631 以下四段配置加入Allow All # Only li…

【智能优化算法】矮猫鼬优化算法(Dwarf Mongoose Optimization Algorithm,DMHO)

矮猫鼬优化算法(Dwarf Mongoose Optimization Algorithm,DMHO)是期刊“COMPUTER METHODS IN APPLIED MECHANICS AND ENGINEERING”&#xff08;IF 7.3&#xff09;的2022年智能优化算法 01.引言 矮猫鼬优化算法(Dwarf Mongoose Optimization Algorithm,DMHO)模仿矮猫鼬的觅食行…

天府锋巢直播产业基地构建成都电商直播高地

天府锋巢直播产业基地自成立以来&#xff0c;一直秉承着创新、协同、共赢的发展理念&#xff0c;吸引了众多直播企业纷纷入驻。随着直播产业的迅猛发展&#xff0c;改成都直播基地内的配套服务也显得尤为重要。本文将深入探讨入驻天府锋巢直播产业基地后&#xff0c;配套的直播…

错误处理机制——vba(vb.net)

程序出现错误时可采用如下错误处理机制&#xff1a;出错时跳到标签处&#xff0c;判断错误类型修正错误&#xff0c;重新返回正确标签处&#xff0c;继续运行程序。 代码如下&#xff1a; Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click…

数据分析的行为要求

数据分析的行为要求通常涉及多个方面&#xff0c;以确保分析的准确性、有效性和合规性。以下是一些关键的行为要求&#xff1a; 诚信、严谨和积极的职业态度&#xff1a; 保持诚实和透明的态度&#xff0c;确保数据分析结果的真实性和可信度。严谨对待数据&#xff0c;遵循科学…