数据结构和算法(1) ---- Queue 的原理和实现

Queue 的定义和结构

队列(Queue) 是只允许在一端进行插入,在另一端进行删除的线性表
队列是一种先进先出(First In First Out)的线性表,简称 FIFO(First IN First OUT), 允许插入的一端称为队尾, 允许删除的一端称为队列头

队列的基本结构如下图所示:
queue struct

Queue 的抽象数据类型

队列也有线性表的各种操作,不同的是插入元素只能在队列尾,删除元素只能在对列头进行:
队列的抽象结构如下所示:

ADT Queue(队列)
Data:同线性表, 元素具有相同的类型,相邻的元素具有前驱和后继关系
Operation:InitQueue(Q*)DestroyQueue(Q*)isEmpty(Q*)isFull(Q*)dequeue(Q*, *e)enqueue(Q*, e)queueSize(Q)
endADT

队列有多种实现方式,比如 静态数组,动态数组,单链表,双链表等

静态数组实现Queue

静态数组实现队列的基本原理:

  • 建立一个 MAX_SIZE 的数组, 用于存放 Queue 中的元素
  • 建立int类型 queue->rear 代表队列尾, 每次 enqueue 一个元素时,queu->rear 指向最新的元素位置
    staticArrayEnqueue
  • 建立 queue->front 代表队列头, 每次 dequeue 一个元素,从 queue->front 位置处取出数据,并且最后其指向下一个元素位置
    StaticArrayDequeue
  • queue->rearqueue->front 相等时,queue->frontqueue->rear都重新设置为 0,此时队列为空,表示重新开始存储数据
    StaticArrayEqual
    参考代码如下:
#define MAX_SIZE 100
typedef struct {int data[MAX_SIZE];int front;// queue 尾端的索引int rear;
}Queue;void Queueinit(Queue* queue) {queue->front = -1;queue->rear = -1;
}int isEmpty(Queue* queue) {return (queue->front == -1 && queue->rear == -1);
};int isFull(Queue* queue) {// queue->rear == MAX_SIZE - 1  queue->front = 0//return (queue->rear + 1)%MAX_SIZE == queue->front;if((queue->rear + 1 - queue->front) == MAX_SIZE) {return 1;}return 0;
};void enqueue(Queue* queue,int item) {if(isFull(queue)) {fprintf(stderr,"queue is full. \n");return;}//printf("queue front is %d rear is %d \n",queue->front,queue->rear);if(isEmpty(queue)) {queue->rear = 0;queue->front = 0;} else {queue->rear = (queue->rear+1)%MAX_SIZE;}queue->data[queue->rear] = item;
}int dequeue(Queue* queue) {if(isEmpty(queue)) {fprintf(stderr,"queue is empty. \n");return -1;}int item = queue->data[queue->front];// with no elementif(queue->front == queue->rear) {printf("queue has no element backup to empty\n");queue->front = -1;queue->rear = -1;} else {queue->front = (queue->front +1)%MAX_SIZE;}return item;
}int peek(Queue* queue) {if(isEmpty(queue)) {fprintf(stderr,"queue is empty. \n");return -1;}return queue->data[queue->front];
}int testbasicQueueStaticArray(int agrc, char *argv[]) {{Queue testqueue = {.front = -1,.rear = -1,};Queueinit(&testqueue);for(int i = 0; i < 2000; i++) {enqueue(&testqueue,200+i);dequeue(&testqueue);}enqueue(&testqueue,1001);enqueue(&testqueue,1002);enqueue(&testqueue,1003);printf("dequeue item:%d \n", dequeue(&testqueue));printf("dequeue item:%d \n", dequeue(&testqueue));printf("dequeue item:%d \n", dequeue(&testqueue));printf("dequeue item:%d \n", dequeue(&testqueue));printf("peek queue element: %d queue size:%d\n", peek(&testqueue),QueueSize(&testqueue));}}
单链表实现Queue

单链表实现Queue的基本原理:

  • 建立一个单链表,包含指向队列头的指针queue->front 和指向队列尾的指针queue->rear

  • enqueue时,首先为新元素分配空间,然后插入到单链表的尾部,用queue->rear指向它
    LinkedListEnqueue

  • dequeue时,首先返回queue->front指向的节点内容,然后free掉queue->front节点,queue->front指向顺序的后一个节点
    LinkedListDequeue
    参考代码如下:

struct node {int data;struct node *next;
};typedef struct {struct node *front;struct node *rear;
}Queue;static int empty(Queue* queue){return (queue->front == NULL);
}static void initQueue(Queue* queue) {queue->front = queue->rear = NULL;
}static void push(Queue* queue, int value) {struct node *pnode;pnode = (struct node*)malloc(sizeof(struct node));if(pnode == NULL) {printf("malloc node failed!.\n");exit(1);}pnode->data = value;pnode->next = NULL;if(empty(queue)) {queue->front = pnode;queue->rear = pnode;} else {queue->rear->next= pnode;queue->rear = pnode;}
}static int pop(Queue* queue) {if (empty(queue)){printf("Queue Underflow. Unable to remove.\n");exit(1);}int item;struct node *p = queue->front;item = queue->front->data;queue->front = queue->front->next;if (queue->front == NULL) /* Queue contained only one node */queue->rear = NULL;free(p);return item;
}static int peek(Queue* queue) {if (empty(queue)){printf("Queue Underflow. Unable to remove.\n");exit(1);}return queue->front->data;
}static int queueSize(Queue* queue){struct node *p = queue->front;int count = 0;if(empty(queue)){return 0;}do {p = p->next;count++;}while(p != NULL);return count;
}int testbasicQueueImplsingleLinkedList(int agrc, char *argv[]) {{Queue testqueue;int qsize = 0;initQueue(&testqueue);push(&testqueue, 10);printf("queue size: %d. \n", queueSize(&testqueue));push(&testqueue, 101);push(&testqueue, 102);push(&testqueue, 103);push(&testqueue, 104);printf("queue size: %d. \n", queueSize(&testqueue));printf("pop value: %d queue size: %d. \n", pop(&testqueue), qsize);qsize = queueSize(&testqueue);printf("pop value: %d queue size: %d. \n", pop(&testqueue), qsize);qsize = queueSize(&testqueue);printf("pop value: %d queue size: %d. \n", pop(&testqueue), qsize);qsize = queueSize(&testqueue);printf("pop value: %d queue size: %d. \n", pop(&testqueue), qsize);printf("queue size: %d. \n", queueSize(&testqueue));printf("peek value: %d  \n", peek(&testqueue));printf("queue size: %d. \n", queueSize(&testqueue));}return 1;
}

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

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

相关文章

FreeCAD中智能指针分析

实现原理 FreeCAD中有两套智能指针&#xff0c;一个是OCC的智能指针handle&#xff0c;另一个是自己定义的智能指针Reference&#xff0c;两种智能指针都是通过引用计数方式管理指针。 1.1 OCC智能指针handle OCC在基础类包中定义了一个模板类handle&#xff0c;该类包含一个私…

大学物理(下)笔记

摘录来自笔记网站的笔记。笔记网站详见https://onford.github.io/Notes/。 大学物理&#xff08;下&#xff09;笔记 部分常用物理常量的计算值 C h a p t e r 9 Chapter9 Chapter9 恒定磁场 毕奥-萨伐尔定律 磁场和电场在很多性质上是有共性的&#xff0c;很多时候可以拿它…

【pytorch05】索引与切片

索引 a[0,0]第0张图片的第0个通道 a[0,0,2,4]第0张图片&#xff0c;第0个通道&#xff0c;第2行&#xff0c;第4列的像素点&#xff0c;dimension为0的标量 选择前/后N张图片 a[:2,:1,:,:].shape前两张图片&#xff0c;第1个通道上的所有图片的数据 a[:2,1:,:,:].shape前两张…

ADD属性驱动架构设计(一)

目录 一、架构设计过程 1.1、架构设计过程 1.1.1、设计目的 1.1.2、质量属性&#xff08;非功能需求&#xff09; 1.1.3、核心功能&#xff08;功能需求&#xff09; 1.1.4、架构关注 1.1.5、约束条件 1.2、基于设计过程 二、什么是ADD? 三、为什么选择ADD? 四、作…

本地离线模型搭建指南-中文大语言模型底座选择依据

搭建一个本地中文大语言模型&#xff08;LLM&#xff09;涉及多个关键步骤&#xff0c;从选择模型底座&#xff0c;到运行机器和框架&#xff0c;再到具体的架构实现和训练方式。以下是一个详细的指南&#xff0c;帮助你从零开始构建和运行一个中文大语言模型。 本地离线模型搭…

鸿蒙开发系统基础能力:【@ohos.hiAppEvent (应用打点)】

应用打点 本模块提供了应用事件打点能力&#xff0c;包括对打点数据的落盘&#xff0c;以及对打点功能的管理配置。 说明&#xff1a; 本模块首批接口从API version 7开始支持。后续版本的新增接口&#xff0c;采用上角标单独标记接口的起始版本。 导入模块 import hiAppEve…

今日分享:中国石油年金系统交互、视觉设计和vue开发

金融系统交互及UI设计时&#xff0c;需注意简洁明了、色彩合理、字体统一、交互易用、安全感和用户控制。确保用户快速理解、安全操作并提升体验。

数据结构~~时间、空间复杂度

目录 一、什么是数据结构 什么是算法 算法的复杂度 二、时间复杂度 三、空间复杂度 四、总结 一、什么是数据结构 数据结构(Data Structure)是计算机存储、组织数据的方式&#xff0c;指相互之间存在一种或多种特定关系的 数据元素的集合。 数据结构关注的是数据的逻辑结…

快速搭建Jenkins自动化集成cicd工具

一、简介 jenkins是一款优秀的自动化持续集成运维工具&#xff0c;可以极大的简化运维部署的步骤。 传统的项目部署需要手动更换最新的项目代码&#xff0c;然后打包并运行到服务器上。 使用Jenkins可以自动化实现&#xff0c;当代码编写完成并提交到git后&#xff0c;Jenki…

通过 cloudflare 白嫖个人 docker 镜像加速服务

不知为何&#xff0c;现在大多数的 docker hub 镜像加速站都停止服务&#xff0c;而官方站点又因某些原因访问不到或延迟很高。所以&#xff0c;今天来记录一种通过 CloudFlare 搭建一个自己的镜像加速服务。 0、必看&#xff01;&#xff01;&#xff01; 注意&#xff1a; 此…

pywebview打包本地的html

51.安装 pip install pywebview 2.新建start.py import webview import timeclass API:def say_hello(self, name):time.sleep(2) # 模拟一个耗时操作return fHello, {name}!def main():api API()webview.create_window(pywebview Example, index.html, js_apiapi)webview.…

抛弃Mybatis,拥抱新的ORM 框架!【送源码】

背景 转java后的几年时间里面一直在寻找一个类似.net的orm&#xff0c;不需要很特别的功能&#xff0c;仅希望90%的场景都可以通过强类型语法来编写符合直觉的sql&#xff0c;来操作数据库编写业务。 但是一直没有找到&#xff0c;Mybatis-Plus的单表让我在最初的时间段内看到…

利用golang_Consul代码实现Prometheus监控目标的注册以及动态发现与配置

文章目录 前言一、prometheus发现方式二、监控指标注册架构图三、部分代码展示1.核心思想2.代码目录3、程序入口函数剖析4、settings配置文件5、初始化配置文件及consul6、全局变量7、配置config8、公共方法目录common9、工具目录tools10、service层展示11、命令行参数12、Make…

Android笔记-安装Termux开启ssh,安装vim、android-tool、python等

背景 家里面有个老手机&#xff0c;想将此手机做成一个家庭服务器。控制各种家电。目前准备先控制电视机 流程 用电脑usb连接手机&#xff0c;下载好Termux&#xff0c;在安卓手机上安装好。这里就不记录笔记了&#xff0c;各个手机不一样。 安装好进入手机后&#xff1a; …

mfc140.dll是什么文件?mfc140.dll文件下载安装办法

一、mfc140.dll文件功能与作用 mfc140.dll作为Microsoft Foundation Classes库的一部分&#xff0c;提供了一系列的基础功能&#xff0c;这些功能对于开发Windows桌面应用程序至关重要。 2.1 应用程序框架 mfc140.dll封装了应用程序的生命周期管理&#xff0c;包括初始化、运…

赶论文不用愁:如何利用ChatGPT在3小时内完成论文

在这份指南里&#xff0c;我将详细介绍如何运用ChatGPT 4.0的高级功能来辅助学术研究与文章写作。从挖掘研究课题的初步想法开始&#xff0c;到撰写一篇内容深刻、结构完整的学术论文&#xff0c;我将逐步演示如何在研究的各个阶段中充分利用ChatGPT。值得一提的是&#xff0c;…

编程精粹—— Microsoft 编写优质无错 C 程序秘诀 07:编码中的假象

这是一本老书&#xff0c;作者 Steve Maguire 在微软工作期间写了这本书&#xff0c;英文版于 1993 年发布。2013 年推出了 20 周年纪念第二版。我们看到的标题是中译版名字&#xff0c;英文版的名字是《Writing Clean Code ─── Microsoft’s Techniques for Developing》&a…

二进制炸弹的fp是什么?

&#x1f3c6;本文收录于「Bug调优」专栏&#xff0c;主要记录项目实战过程中的Bug之前因后果及提供真实有效的解决方案&#xff0c;希望能够助你一臂之力&#xff0c;帮你早日登顶实现财富自由&#x1f680;&#xff1b;同时&#xff0c;欢迎大家关注&&收藏&&…

【算法】优先级队列-基础与应用

优先级队列&#xff08;Priority Queue&#xff09;是一种特殊的队列类型&#xff0c;它允许在其元素中分配优先级。与传统的先进先出&#xff08;FIFO&#xff09;队列不同&#xff0c;优先级队列中元素的出队顺序取决于它们的优先级。优先级较高的元素会被优先处理&#xff0…

【Qt笔记①】帮助文档、窗口、按钮、信号和槽、lambda表达式

学习第一天&#xff1a;2024-3-9 文章目录 Qt creator 快捷键帮助文档默认生成的main.cpp逐行解释核心类帮助文档的查阅方法-①代码创建按钮第一个第二个对窗口的其他设置 对象树窗口坐标系信号和槽&#xff08;优点&#xff1a;松散耦合&#xff09;帮助文档的查阅方法-②找信…