数据结构-----队列

目录

前言

队列

定义 

队列的定义和操作方法

 队列节点的定义

 操作方式

 顺序表实现队列(C/C++代码)

链表实现队列(C/C++代码)

Python语言实现队列


前言

        排队是我们日常生活中必不可少的一件事,去饭堂打饭的时候排队,上公交车的时候排队等等,那排队的原则就是先到先得,排在前面的人先打饭,同样的在数据结构当中,有一种数据结构类型叫队列,其性质跟排队是一模一样的,下面我们就一起来看看吧!

队列

定义 

        队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。

        队列的数据元素又称为队列元素。在队列中插入一个队列元素称为入队,从队列中删除一个队列元素称为出队。因为队列只允许在一端插入,在另一端删除,所以只有最早进入队列的元素才能最先从队列中删除,故队列又称为先进先出(FIFO—first in first out)线性表

 

队列可以通过顺序表实现和链表实现,下面就一起来看看吧!  

队列的定义和操作方法

 队列节点的定义

顺序表:

//队列定义
typedef struct queue {ElemType data[Maxsize];int front;//指向队头int rear;//指向队尾
}Queue;

链式:

//定义队列
typedef struct queue {int count;	//计数Node* front;//指向队头指针Node* rear;//指向队尾指针
}Queue;

 操作方式

void Queue_init(Queue* queue);//初始化bool isEmpty(Queue* queue);//判空bool isFull(Queue* queue);//判满void enQueue(Queue* queue, ElemType data);//入队Node* deQueue(Queue* queue);//出队int get_length(Queue* queue);//获取长度void travel_Queue(Queue* queue);//遍历(队头到队尾)void clear_Queue(Queue* queue);//清空销毁

 顺序表实现队列(C/C++代码)

#include <stdio.h>
#include<string.h>
#define Maxsize 20 //最大容量
//顺序表队列//节点数据
typedef struct data {int num;char name[10];
}ElemType;
//队列定义
typedef struct queue {ElemType data[Maxsize];int front;//指向队头int rear;//指向队尾
}Queue;//初始化
void queue_init(Queue* queue) {queue->front = 0;queue->rear = 0;
}//判断是否满队列
bool isFull(Queue* queue) {if (queue->rear == Maxsize) {printf("The queue is full\n");return true;}return false;
}//判断是否空队列
bool isEmpty(Queue* queue) {if (queue->rear == 0) {printf("The queue is etmpy\n");return true;}return false;
}//入队操作
void enQueue(Queue* queue, ElemType data) {if (!isFull(queue)) {//赋值queue->data[queue->rear].num = data.num;strcpy(queue->data[queue->rear].name, data.name);queue->rear++;	//队尾+1,往后移动一位}elseprintf("error\n");
}//出队操作
ElemType deQueue(Queue* queue) {ElemType de_data = { 0 };if (!isFull(queue)) {de_data = queue->data[queue->front];queue->front++;	//队头+1往后移动一位return de_data;}elseprintf("error\n");
}//遍历队列(从队头开始到队尾)
void travel_Queue(Queue* queue) {for (int i = queue->front; i < queue->rear; i++) {printf("%d %s\n", queue->data[i].num, queue->data[i].name);}printf("printf over!\n");
}//获取队列长度
int get_Queuelength(Queue* queue) {return queue->rear-queue->front;
}//清空队列
void clear_Queue(Queue* queue) {queue_init(queue);//直接恢复出厂设置
}int main(void)
{Queue queue;queue_init(&queue);ElemType data[4] = { {15,"fuck"},{16,"wdf"},{17,"wtmc"},{18,"cnmb"} };for (int i = 0; i < 4;i++) {enQueue(&queue, data[i]);}deQueue(&queue);travel_Queue(&queue);
}//16 wdf
//17 wtmc
//18 cnmb
//printf over!

链表实现队列(C/C++代码)

#include<stdio.h>
#include<string.h>
#include <stdbool.h>
#include<stdlib.h>
#include<assert.h>
#define Maxsize 20typedef struct datatype {int num;char name[10];
}ElemType;
//定义节点
typedef struct node {ElemType data;struct node* next;
}Node;
//定义队列
typedef struct queue {int count;	//计数Node* front;//指向队头指针Node* rear;//指向队尾指针
}Queue;void Queue_init(Queue* queue);//初始化
bool isEmpty(Queue* queue);//判空
bool isFull(Queue* queue);//判满
void enQueue(Queue* queue, ElemType data);//入队
Node* deQueue(Queue* queue);//出队
int get_length(Queue* queue);//获取长度
void travel_Queue(Queue* queue);//遍历
void clear_Queue(Queue* queue);//清空销毁int main()
{Queue myqueue;Queue_init(&myqueue);ElemType data[4] = { {15,"a"},{16,"wb"},{17,"htt"},{18,"jk"} };for (int i = 0; i < 4; i++) {enQueue(&myqueue, data[i]);}deQueue(&myqueue);travel_Queue(&myqueue);
}
//16 wb
//17 htt
//18 jk
//Printf over//初始化
void Queue_init(Queue* queue) {assert(queue);queue->front = NULL;queue->rear = NULL;queue->count=0;
}//创建节点
Node* create_node(ElemType data) {Node* new_node = (Node*)malloc(sizeof(Node));if (new_node) {new_node->data = data;new_node->next = NULL;return new_node;}else{printf("ERRPR\n");}
}//判断是否空队列
bool isEmpty(Queue* queue) {assert(queue);if (queue->count == 0){printf("The queue is etmpy\n");return true;}return false;
}//判断是否满队列
bool isFull(Queue* queue) {assert(queue);if (queue->count == Maxsize) {printf("The queue is full\n");return true;}return false;
}//入队
void enQueue(Queue* queue, ElemType data) {assert(queue);if (!isFull(queue)) {Node* new_node = create_node(data);//如果队尾指向空的时候,也就是队列为空时if (queue->rear == NULL) {queue->front = new_node;queue->rear = new_node;queue->count++;}//当队列不为空的时候else{queue->rear->next = new_node;queue->rear = new_node;queue->count++;}}else{printf("error\n");}
}//出队
Node* deQueue(Queue* queue) {assert(queue);if (!isEmpty(queue)) {Node* deNode = queue->front;queue->front = deNode->next;queue->count--;return deNode;}printf("error\n");return NULL;
}//获取队列长度
int get_length(Queue* queue) {assert(queue);return queue->count;
}//遍历队列
void travel_Queue(Queue* queue) {assert(queue);Node* cur = queue->front;while (cur) {printf("%d %s\n", cur->data.num, cur->data.name);cur = cur->next;}printf("Printf over\n");
}//清空队列
void clear_Queue(Queue* queue) {assert(queue);Node* cur = queue->front;while (cur) {//要把每一个节点的空间都释放,避免内存泄漏Node* p = cur->next;    //获取到当前节点的下一个节点free(cur);cur = p;}printf("Clear successfully!\n");
}

Python语言实现队列

链接:Python数据结构-----队列_python队列_灰勒塔德的博客-CSDN博客

 以上就是今天的全部内容了,我们下一期再见!!!

分享一张壁纸:

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

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

相关文章

彩色相机工作原理——bayer格式理解

早期&#xff0c;图像传感器只能记录光的强弱&#xff0c;无法记录光的颜色&#xff0c;所以只能拍摄黑白照片。 1974年,拜尔提出了bayer阵列&#xff0c;发明了bayer格式图片。不同于高成本的三个图像传感器方案&#xff0c;拜尔提出只用一个图像传感器&#xff0c;在其前面放…

k8s相关命令-命名空间

k8s相关命令目录 文章目录 前言一、创建命名空间二、删除命名空间三、查看命名空间列表四、查看命名空间列表五、查看特定命名空间下所有资源六、删除特定命名空间下所有资源 前言 记录k8s命名空间的相关操作命令 一、创建命名空间 kubectl create namespace <namespace&g…

java向数据库写入数据:如何使用Java将数据写入数据库

​​​​​​答&#xff1a;Java向数据库写入数据的步骤如下&#xff1a;需要创建一个JDBC连接&#xff0c;以便访问数据库。 答&#xff1a;Java向数据库写入数据的步骤如下&#xff1a; 1. 首先&#xff0c;需要创建一个JDBC连接&#xff0c;以便访问数据库。 2. 然后&…

tensorflow基础

windows安装tensorflow anaconda或者pip安装tensorflow&#xff0c;tensorflow只支持win7 64系统&#xff0c;本人使用tensorflow1.5版本&#xff08;pip install tensorflow1.5&#xff09; tensorboard tensorboard只支持chrome浏览器&#xff0c;而且加载过程中可能有一段…

JS new操作符具体做了什么?

1. 意义 在JavaScript中&#xff0c;“new” 操作符用于创建一个新的对象实例。具体来说&#xff0c;“new” 操作符会执行以下步骤&#xff1a; JavaScript中的new操作符是一个非常重要的操作符&#xff0c;它用于创建一个新的对象实例。 2. 实例化步骤 创建一个新的空对象。…

Java面向对象七大原则以及设计模式单例模式和工厂模式简单工厂模式

面向对象的七大原则&#xff08;OOP&#xff09; 1,开闭原则&#xff1a; 对扩展开发&#xff0c;对修改关闭 2.里氏替换原则&#xff1a; 继承必须确保超类所拥有的子类的性质在子类中仍然成立 3.依赖倒置原则&#xff1a; 面向接口编程&#xff0c;不要面向实现编程&am…

K8S pod资源、探针

目录 一.pod资源限制 1.pod资源限制方式 2.pod资源限制指定时指定的参数 &#xff08;1&#xff09;request 资源 &#xff08;2&#xff09; limit 资源 &#xff08;3&#xff09;两种资源匹配方式 3.资源限制的示例 &#xff08;1&#xff09;官网示例 2&#xff0…

super详解

父类 package com.mypackage.oop.demo06;public class Person06{public Person06() {System.out.println("Person06无参执行了");}protected String name "hexioahei";public void print(){System.out.println("Person");} }子类 package com…

机器学习第七课--情感分析系统

分词 分词是最基本的第一步。无论对于英文文本&#xff0c;还是中文文本都离不开分词。英文的分词相对比较简单&#xff0c;因为一般的英文写法里通过空格来隔开不同单词的。但对于中文&#xff0c;我们不得不采用一些算法去做分词。 常用的分词工具 # encodingutf-8 import …

Python爬虫:获取必应图片的下载链接

文章目录 1. 前言2. 实现思路3. 运行结果 1. 前言 首先&#xff0c;说明一下&#xff0c;本篇博客内容可能涉及到版权问题&#xff0c;为此&#xff0c;小编只说明一下实现思路&#xff0c;至于全部参考代码&#xff0c;小编不粘贴出来。不过&#xff0c;小编会说明详细一些&a…

docker查看镜像的latest对应的具体版本

查询容器镜像时&#xff0c;TAG只显示latest&#xff0c;而不是显示具体的版本号 docker images # 显示内容 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 20 months ago 141MB redis latest 7614ae945…

three.js——模型对象的使用材质和方法

模型对象的使用材质和方法 前言效果图1、旋转、缩放、平移&#xff0c;居中的使用1.1 旋转rotation&#xff08;.rotateX()、.rotateY()、.rotateZ()&#xff09;1.2缩放.scale()1.3平移.translate()1.4居中.center() 2、材质属性.wireframe 前言 BufferGeometry通过.scale()、…

LeetCode: 4. Median of Two Sorted Arrays

LeetCode - The Worlds Leading Online Programming Learning Platform 题目大意 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。 请你找出这两个有序数组的中位数&#xff0c;并且要求算法的时间复杂度为 O(log(m n))。 你可以假设 nums1 和 nums2 不会同时为空。 …

css动画旋转分层旋转图

1.代码 <template><view class"animations"><view class"animation animation1"><view class"animate1"></view><view class"animate2"></view><view class"animate3">&l…

Qt --- Day02

实现效果&#xff1a; 点击登录&#xff0c;检验用户密码是否正确&#xff0c;正确则弹出消息框&#xff0c;点击ok转到另一个页面 不正确跳出错误消息框&#xff0c;默认选线为Cancel&#xff0c;点击Yes继续登录 点击Cancel跳出问题消息框&#xff0c;默认选项No&#xff0c…

Java文字描边效果实现

效果&#xff1a; FontUtil工具类的完整代码如下&#xff1a; 其中实现描边效果的函数为&#xff1a;generateAdaptiveStrokeFontImage() package com.ncarzone.data.contentcenter.biz.img.util;import org.springframework.core.io.ClassPathResource; import org.springfr…

分子生物学——分子机器

分子生物学——分子机器 文章目录 前言一、2016年度诺贝尔化学奖1.1. 介绍1.2. 什么是分子机器&#xff1f;1.3. 分子机器的意义 总结 前言 对于本次搜集分子生物学领域的一个诺贝尔奖的有关内容的作业 参考文献&#xff1a; https://www.cas.cn/zt/sszt/2016nobelprize/hxj/2…

Redis布隆过滤亿级大数据

场景描述 小程序用户的openid作为最主要的业务查询字段&#xff0c;在做了缓存设计之后仍有非常高频的查询&#xff0c;通过埋点简单统计约在每日1000w次。 其中&#xff1a;由于有新增用户原因&#xff0c;导致请求的openid根本不存在MySQL数据库中&#xff0c;这部分统计约占…

使用vite创建vue3项目及项目的配置 | 环境准备 ESLint配置 prettier配置 husky配置 项目继承

文章目录 使用vite创建vue3项目及项目的配置1.环境准备2.项目配置ESLint校验代码工具配置 - js代码检测工具1.安装ESLint到开发环境 devDependencies2.生成配置文件:.eslint.cjs**3.安装vue3环境代码校验插件**4. 修改.eslintrc.cjs配置文件5.生成ESLint忽略文件6.在package.js…

[BJDCTF2020]Mark loves cat foreach导致变量覆盖

这里我们着重了解一下变量覆盖 首先我们要知道函数是什么 foreach foreach (iterable_expression as $value)statement foreach (iterable_expression as $key > $value)statement第一种格式遍历给定的 iterable_expression 迭代器。每次循环中&#xff0c;当前单元的值被…