当前位置: 首页 > 编程日记 > 正文

数据结构(队列实现篇)

在数据结构与算法中,队列queue是一种受限的线性储存结构,特殊之处在于它只允许在表的前端front进行删除操作,而在表的后端rear进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。遵循先进先出FIFO的规则。

队列结构示意图

队列结构使用

生活中很多地方使用到队列,例如医院门诊看病就是按挂号的号数来排队就诊;公司里面的打印机是按文件发送的顺序进行打印。

在编程中,队列常用作消息发送。例如消息发送队列。待发送的消息依次入队列,待一个消息出队列发送完毕,才会进行下一个消息出队列进行发送。

实现普通队列结构的功能

  • push(element):push是入队列操作,其中element是需要进队列的元素,返回操作成功与否布尔值。
  • shift():shift移除队列front元素操作,返回移除的移除元素。
  • size():获取栈中的队列元素数量,返回一个数字。
  • isEmpty():判断队列是否为空,返回一个布尔值。
  • peek():返回队头元素,但不移除栈顶元素。
  • bottom():返回队尾元素。
  • clear():清除所有队列元素,返回操作成功与否布尔值。

进队列与出队列流程

ES6 队列结构代码

    class Queue{constructor(){this.lists = [];}size(){return this.lists.length;}isEmpty(){return this.lists.length === 0;}peek(){return this.lists[0];}bottom(){const topIndex = this.size() -1;return this.lists[topIndex];}push(element){this.lists.push(element);return true;}shift(){return this.lists.shift();}clear(){this.lists = [];return true;}toString(){let result = "";this.lists.forEach(value=>{result = result + ""+value;});return result;}}
复制代码

ES5 队列结构代码

通过ES5把功能函数可以绑定在对象的实例上,也可以把功能函数加在函数的原型链上

    function Queue(){this.lists = [];}Queue.prototype.push = function(element){this.lists.push(element);return true;}Queue.prototype.shift = function(){return this.lists.shift();}Queue.prototype.clear = function(){this.lists = [];return true;}// 返回队头元素Queue.prototype.peek = function(){return this.lists[0];}Queue.prototype.size = function(){return this.lists.length;}Queue.prototype.isEmpty = function(){return this.lists.length === 0;}Queue.prototype.bottom = function(){var topIndex = this.size() -1;return this.lists[topIndex];}Queue.prototype.toString = function(){var result = "";for(var i=0;i<this.lists.length;i++){result = result + '' +this.lists[i];}return result;}
复制代码

实现优先队列结构的功能

上面实现的是普通队列情况,在日常生活中总遇到需要紧急处理的情况,例如银行VIP客户,急诊室危急病人,紧急文件。这时候需要优先处理这种情况。

优先队列和普通队列的不同主要在优先队列的每一个元素都具有一个权值,代表该元素的优先权大小。还有就是根据权值大小插入队列之中。

ES6 最大优先队列结构代码

    class Node {constructor(element, prority) {this.element = element;this.prority = prority;}}class Queue {constructor() {this.lists = [];}size() {return this.lists.length;}isEmpty() {return this.lists.length === 0;}peek() {return this.list[0];}bottom() {const topIndex = this.size() - 1;return this.lists[topIndex];}//插入队列append(node) {//当队列为空时if (!this.lists.length) {this.lists.push(node);return true;} else {for(let i=0;i<this.lists.length;i++){if(this.lists[i]["prority"] < node.prority){this.lists.splice(i,0,node);return true;}}this.lists.push(node);}}shift() {return this.lists.shift();}clear() {this.lists = [];return true;}toString() {let result = "";this.lists.forEach(value => {result = result + "" + value;});return result;}}const q = new Queue();q.append(new Node(1, 1));q.append(new Node(2, 2));q.append(new Node(5, 3));q.append(new Node(4, 2));console.log(JSON.stringify(q));
复制代码

ES5 最大优先队列结构代码

通过ES5把功能函数可以绑定在对象的实例上,也可以把功能函数加在函数的原型链上

    function Node(element,prority){this.element = element;this.prority = prority;}function Queue(){this.lists = [];}Queue.prototype.append = function(node){//当队列为空时if(this.lists.length == 0){this.lists.push(node);return true;}for(var i=0;i<this.lists.length;i++){if(node.prority > this.lists[i]["prority"]){this.lists.splice(i,0,node);return true;}}this.lists.push(node);}Queue.prototype.shift = function(){return this.lists.shift();}Queue.prototype.clear = function(){this.lists = [];return true;}// 返回队头元素Queue.prototype.peek = function(){return this.lists[0];}Queue.prototype.size = function(){return this.lists.length;}Queue.prototype.isEmpty = function(){return this.lists.length === 0;}Queue.prototype.bottom = function(){var topIndex = this.size() -1;return this.lists[topIndex];}Queue.prototype.toString = function(){var result = "";for(var i=0;i<this.lists.length;i++){result = result + '' +this.lists[i];}return result;}
复制代码

循环队列结构图

循环队列就是把队头元素出队列后,再入队列。击鼓传花就是用到了循环队列的原理

循环队列代码

循环队列主要实现代码如下

  class SqQueue {constructor(length) {this.queue = new Array(length + 1)// 队头this.first = 0// 队尾this.last = 0// 当前队列大小this.size = 0}enQueue(item) {// 判断队尾 + 1 是否为队头// 如果是就代表需要扩容数组// % this.queue.length 是为了防止数组越界if (this.first === (this.last + 1) % this.queue.length) {this.resize(this.getLength() * 2 + 1)}this.queue[this.last] = itemthis.size++this.last = (this.last + 1) % this.queue.length}deQueue() {if (this.isEmpty()) {throw Error('Queue is empty')}let r = this.queue[this.first]this.queue[this.first] = nullthis.first = (this.first + 1) % this.queue.lengththis.size--// 判断当前队列大小是否过小// 为了保证不浪费空间,在队列空间等于总长度四分之一时// 且不为 2 时缩小总长度为当前的一半if (this.size === this.getLength() / 4 && this.getLength() / 2 !== 0) {this.resize(this.getLength() / 2)}return r}getHeader() {if (this.isEmpty()) {throw Error('Queue is empty')}return this.queue[this.first]}getLength() {return this.queue.length - 1}isEmpty() {return this.first === this.last}resize(length) {let q = new Array(length)for (let i = 0; i < length; i++) {q[i] = this.queue[(i + this.first) % this.queue.length]}this.queue = qthis.first = 0this.last = this.size}
}
复制代码

终于水完这篇数据结构队列

Github地址: github.com/Harhao

相关文章:

Markdown编辑器使用

Markdown编辑器使用欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题&#xff0c;有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚…

HOGDescriptor 描述类

struct CV_EXPORTS HOGDescriptor {// 高斯平滑参数enum { DEFAULT_WIN_SIGMA -1 };// 检测窗口的最大数量enum { DEFAULT_NLEVELS 64 };// 描述符存储格式enum { DESCR_FORMAT_ROW_BY_ROW, DESCR_FORMAT_COL_BY_COL };// 创建了特征描述符和检测器HOGDescriptor(Size win_si…

Linux----进程概念

程序 : 程序指的是一系列有逻辑, 有顺序结构的指令.进程 : 进程从两个角度来说:1 用户角度: 进程从用户角度来说就是运行中的程序2 操作系统的角度: 进程是操作系统对运行中程序的描述信息, 叫做进程描述符(程序控制块)简称PCBPCB : 在Linux下PCB指的是在内核中的task_struct 结…

codecheck

codecheck scan-build: clang-tools集成的静态检查工具, 使用clang static analyzer进行静态检查&#xff0c;使用方便 https://clang-analyzer.llvm.org/scan-build.html https://manpages.ubuntu.com/manpages/bionic/man1/scan-build.1.html CodeChecker: 爱立信推出的静…

ubuntu安装chrome浏览器

PPA方法&#xff0c;免FQ&#xff0c;否则&#xff0c;你得FQ下载chrome&#xff0c;你Firefox VPN配置好了吗&#xff01;&#xff01;&#xff01; wget -q -O - https://raw.githubusercontent.com/longhr/ubuntu1604hub/master/linux_signing_key.pub | sudo apt-key add s…

java中的注解(二)

今天我继续来介绍java中的注解。注解与接口和类不同的是注解是不允许继承的&#xff0c;但是注解中有一个和继承有关的元注解&#xff1a;Inherited。如果我们在定义注解时候加上这个元注解那么我们就可以在子类中监测到该注解的存在。 Target(ElementType.TYPE) Inherited Ret…

【C++】random随机数与【C++11】/rand()和srand()的用法

文章目录随机数1. c 11 random随机数的使用&#xff08;推荐使用&#xff09;1.11.21.31.42.1 C中随机函数rand()和srand()的用法&#xff08;老本版&#xff09;2.2 限制随机数的范围随机数 C 提供了一组函数以生成和使用随机数字。随机数字就是从一组可能的值中进行随机选择…

快速区域积分直方图实现

void cacHOGinCell(Mat& HOGCellMat,Rect roi,std::vector<Mat>& integrals) {// 实现快速积分HOGint x0 roi.x,y0 roi.y;int x1 x0 roi.width&#xff1b;int y1 y0 roi.height;for(int i 0; i < NBINS; i){// 根据矩阵的上下左右坐标计算Mat integra…

Resultset获取行数和列数

为什么80%的码农都做不了架构师&#xff1f;>>> 在Java中&#xff0c;获得ResultSet的总行数的方法有以下几种。 第一种&#xff1a;利用ResultSet的getRow方法来获得ResultSet的总行数 Statement stmt con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,Re…

object.create()

语法&#xff1a; Object.create(proto, [propertiesObject]) //方法创建一个新对象&#xff0c;使用现有的对象来提供新创建的对象的proto。 参数&#xff1a;proto : 必须。表示新建对象的原型对象&#xff0c;即该参数会被赋值到目标对象(即新对象&#xff0c;或说是最后返回…

codecheck_use_record

文章目录Step 1: Integrate CodeChecker into your build systemStep 2: Analyze your codeStep 3: Run the analysisStep 4: View the analysis results in the command lineStep 5: Hint: You can do the 1st and the 2nd step in one round by executing checkstep 6: Expor…

centos6.5 rsync+inotify同步配置笔记

以两台服务器为例&#xff1a; 主服务器&#xff1a; 192.168.1.100 从服务器&#xff1a; 192.168.1.101 1.安装rsync (主服务器与从服务器同时安装) 使用xinetd管理rsync yum install rsync xinetd 设置开机启动 vi /etc/xinetd.d/rsync ... 修改为 disable no ... 启动xin…

HOG 特征计算实现

// 获取HOG直方图 cv::Mat getHog(Point pt,std::vector<Mat> &integrals) {// 判断当前点的位置是否符合条件 if( pt.x - R < 0 ||pt.y - R < 0 ||pt.x R > integrals[0].cols ||pt.y R > integrals[0].rows ){return Mat();}// 直方图Mat hist(Size(…

Spring+SpringMVC+Mybatis整合

一、简单测试工程搭建1、Mybatis所需要的的jar包&#xff08;包含数据库驱动包和相关的日志包&#xff09;、SpringMVC和Spring的jar包2、然后构建一个基本的工程&#xff0c;这里我们使用mapper代理的方式进行Mybatis的编写&#xff0c;关于mapper代理请参考Mybatis简单入门中…

【Tools】Markdown数学符号公式(史上最全公式表)

Markdown数学符号&公式 文章目录Markdown数学符号&公式1. 希腊字母表2. 希腊字母3. 数学符号表4. 数学符号5. 数学符号补充表6. 数学符号补充1. 希腊字母表 符号代码符号代码α\alphaα\alphaA\AlphaA\Alphaβ\betaβ\betaB\BetaB\Betaγ\gammaγ\gammaΓ\GammaΓ\gam…

Editplus下载、安装并最佳配色方案(强烈推荐)

不多说&#xff0c;直接上干货&#xff01; Editplus下载 第一步&#xff1a;进入官网 https://www.editplus.com/ 第二步&#xff1a;下载 https://www.editplus.com/download.html Editplus安装 我这里&#xff0c;直接以一个压缩包来安装&#xff0c;需要的&#xff0c;请…

MySQL数据库开发规范-EC

最近一段时间一边在线上抓取SQL来优化&#xff0c;一边在整理这个开发规范&#xff0c;尽量减少新的问题SQL进入生产库。今天也是对公司的开发做了一次培训&#xff0c;PPT就不放上来了&#xff0c;里面有十来个生产SQL的案例。因为规范大部分还是具有通用性&#xff0c;所以也…

操作系统与内存管理

操作系统内存管理 文章目录操作系统内存管理1. 虚拟地址空间2. 内存地址空间含义及分配3. 虚拟内存诞生的前世与今生&#xff1f;3.1 内存管理的好处3.2 **内存管理实现总体策略**4. 不同进程如何划分内存地址空间&#xff1f;5 内存分配与回收5.1 buffer和cache5.2 malloc背后…

圆形 LBP 特征

template <typename _Tp> staticinline void elbp_(InputArray _src, OutputArray _dst,int radius, int neighbors) {// 得到数据矩阵Mat src _src.getMat();// 输出矩阵_dst.create(src.rows-2*radius, src.cols-2*radius, CV_32SC1);Mat dst _dst.getMat();// 初始化…

40个Java多线程问题总结

&#xff08;转&#xff09; 这篇文章作者写的真是不错 40个问题汇总 1、多线程有什么用&#xff1f; 一个可能在很多人看来很扯淡的一个问题&#xff1a;我会用多线程就好了&#xff0c;还管它有什么用&#xff1f;在我看来&#xff0c;这个回答更扯淡。所谓"知其然知其所…

SLAM十四讲笔记1

文章目录ch02 初识SLAMch02-01 经典视觉SLAM框架ch02-02 SLAM问题的数学表述ch03 三维空间刚体运动ch03.01 旋转矩阵&#xff1a;点和向量,坐标系01 向量a在线性空间的基[e1,e2,e3][e_1,e_2,e_3][e1​,e2​,e3​]下的坐标为[a1,a2,a3]T[a_1,a_2,a_3]^T[a1​,a2​,a3​]T.02 向量…

12、OpenCV实现图像的空间滤波——图像平滑

1、空间滤波基础概念 1、空间滤波基础 空间滤波一词中滤波取自数字信号处理&#xff0c;指接受或拒绝一定的频率成分&#xff0c;但是空间滤波学习内容实际上和通过傅里叶变换实现的频域的滤波是等效的&#xff0c;故而也称为滤波。空间滤波主要直接基于领域&#xff08;空间域…

计算 LBP 特征

#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/features2d/features2d.hpp> #include <opencv2/features2d/features2d.hpp> // 计算原始LBP特征 cv::Mat OLBP(cv::Mat& srcImage) {const int nRows …

三维重建【四】-------------------结构光 三维重建----论文调研

1. 动态目标实时三维重建-结构光方案 动态目标 三维重建 Stripe boundary codes for real-time structured-light range scanning of moving objects 我们提出了一种新的实时结构光扫描方法。在分析现有结构光技术的基本假设之后&#xff0c;我们基于编码投影条纹之间的边界&…

APP开发定制

app是什么意思? APP&#xff0c;application的简称&#xff0c;是智能手机的第三方应用程序&#xff0c;常见的有微信、手机qq、今日头条、手机支付宝、腾讯视频、微店等&#xff0c;随着智能手机和ipad等移动终端设备的普及&#xff0c;人们逐渐习惯了使用APP客户端上网的方式…

Haar 特征提取

double HaarExtract(double const ** image ,int type_, cv::Rect roi) {double value;double wh1, wh2;double bk1, bk2;int x roi.x;int y roi.y;int width roi.width;int height roi.height;switch (type_){// Haar水平边缘case 0: // HaarHEdgewh1 calcIntegral(image…

awk的基本⽤法

awk的基本⽤法 awk是报告⽣成器&#xff0c;格式化⽂本输出&#xff0c;有多种版本。centos中的是gawk即GNU awk版本。 awk⼯作原理&#xff1a;第⼀步&#xff1a;执⾏BEGIN{action;...}语句块中的语句。第⼆步&#xff1a;从⽂件或标准输⼊&#xff08;stdin&#xff09;读取…

视音频数据处理入门:RGB、YUV像素数据处理【转】

转自&#xff1a;http://blog.csdn.net/leixiaohua1020/article/details/50534150 视音频数据处理入门系列文章&#xff1a; 视音频数据处理入门&#xff1a;RGB、YUV像素数据处理 视音频数据处理入门&#xff1a;PCM音频采样数据处理 视音频数据处理入门&#xff1a;H.264视频…

SVO(SVO: fast semi-direct monocular visual odometry)

SVO&#xff08;SVO: fast semi-direct monocular visual odometry&#xff09;翻译 文章目录SVO&#xff08;SVO: fast semi-direct monocular visual odometry&#xff09;翻译1、介绍2、系统概述3、符号4、运动估计4.1、 基于稀疏模型的图像对齐4.2、 通过特征对齐松弛4.3、…

MSER 候选车牌区域检测

#include "opencv2/highgui/highgui.hpp" #include "opencv2/features2d/features2d.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> // Mser车牌目标检测 std::vector<cv::Rect> mserGetPlate(cv::Mat srcImage…