7、队列

news/2024/9/19 14:20:43/文章来源:https://www.cnblogs.com/xpp3/p/18416332

1、链队

#include<stdio.h>
#include<malloc.h>
#include<assert.h>#define ElemType inttypedef struct QueueNode{ElemType data;struct QueueNode* next;
}QueueNode; typedef struct LinkQueue{QueueNode* front;//队头QueueNode* tail;//队尾 
}LinkQueue;QueueNode* malloc_QueueNode(ElemType e){QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));assert(node != NULL);node->data = e;node->next = NULL;
}void initQueue(LinkQueue* queue){//构造虚拟头结点QueueNode* p = malloc_QueueNode(-1);queue->front = queue->tail = p;
}//入队
void enQueue(LinkQueue* queue,ElemType e){QueueNode* p = malloc_QueueNode(e);queue->tail->next = p;//修改队尾 queue->tail = p; 
}//出队
int deQueue(LinkQueue* queue){if(queue->front == queue->tail){printf("当前链队为空,不可删除.\n");return;}QueueNode* p = queue->front->next;queue->front->next = p->next;if(p == queue->tail){queue->tail = queue->front;}free(p);
}//得到队头元素
void getHead(LinkQueue* queue,ElemType* e){if(queue->front == queue->tail){printf("当前链队为空.\n");return;}QueueNode* p = queue->front->next;*e = p->data;
}//长度
int length(LinkQueue* queue){int len = 0;QueueNode* p = queue->front->next;while(p != NULL){p = p->next;len++;}return len;
}//清空链队
void clear(LinkQueue* queue){QueueNode* p = queue->front->next;while(p != NULL){queue->front->next = p->next;free(p);p = queue->front->next;}queue->tail = queue->front;
}//销毁
void destroy(LinkQueue* queue){clear(queue);free(queue->front);queue->front = queue->tail = NULL;
}
//打印链队
void showQueue(LinkQueue* queue){QueueNode* p = queue->front->next;while(p != NULL){printf("%d->",p->data);p = p->next;}printf("NULL.\n");
}
int main(){LinkQueue queue;initQueue(&queue);int i = 1;for(i;i <= 5;i++){enQueue(&queue,i); }showQueue(&queue);deQueue(&queue);showQueue(&queue);return 0;
}

2、顺序队

#include<stdio.h>
#include<malloc.h>
#include<assert.h>#define ElemType int#define MAXSIZE 8
//顺序队列 
typedef struct SeqQueue{ElemType* base;int front;//队头 int rear;//队尾 
}SeqQueue;//初始化顺序队列 
void initSeqQueue(SeqQueue* Q){Q->base = (ElemType*)malloc(sizeof(ElemType) * MAXSIZE);assert(Q->base != NULL);Q->front = 0;Q->rear = 0;
}//入队
void enQueue(SeqQueue* Q,ElemType e){if(Q->rear >= MAXSIZE){printf("队列空间已满,%d 不可入队.\n",e);return;}Q->base[Q->rear++] = e;
} //出队
void DeQueue(SeqQueue* Q){if(Q->front == Q->rear){printf("队列为空,不可出队.\n");return;}Q->front++;
}
//打印队列
void showSeqQueue(SeqQueue* Q){int i = Q->front;for(i;i < Q->rear;i++){printf("%d ",Q->base[i]);}printf("\n");
} void getHead(SeqQueue* Q,ElemType* e){if(Q->front == Q->rear){printf("队列为空,无法获取队头元素.\n");return;}*e = Q->base[Q->front];
}int length(SeqQueue* Q){return Q->rear - Q->front;
}void clear(SeqQueue* Q){Q->front = Q->rear = 0;
}void destroy(SeqQueue* Q){free(Q->base);Q->base = NULL;
}
int main(){SeqQueue Q;initSeqQueue(&Q);int i = 1;for(i;i <= 10;i++){enQueue(&Q,i);}showSeqQueue(&Q);DeQueue(&Q);showSeqQueue(&Q);return 0;
} 

3、循环队列

 

#include<stdio.h>
#include<malloc.h>
#include<assert.h>#define ElemType int#define MAXSIZE 8
//顺序队列 
typedef struct SeqQueue{ElemType* base;int front;//队头 int rear;//队尾 
}SeqQueue;//初始化顺序队列 
void initSeqQueue(SeqQueue* Q){Q->base = (ElemType*)malloc(sizeof(ElemType) * MAXSIZE);assert(Q->base != NULL);Q->front = 0;Q->rear = 0;
}//入队
void enQueue(SeqQueue* Q,ElemType e){if(((Q->rear + 1) % MAXSIZE) == Q->front){printf("队列空间已满,%d 不可入队.\n",e);return;}Q->base[Q->rear] = e;Q->rear = (Q->rear + 1) % MAXSIZE;
} //出队
void DeQueue(SeqQueue* Q){if(Q->front == Q->rear){printf("队列为空,不可出队.\n");return;}Q->front = (Q->front + 1) % MAXSIZE;
}
//打印队列
void showSeqQueue(SeqQueue* Q){int i = Q->front;for(i;i != Q->rear;i = (i + 1) % MAXSIZE){printf("%d ",Q->base[i]);}printf("\n");
} void getHead(SeqQueue* Q,ElemType* e){if(Q->front == Q->rear){printf("队列为空,无法获取队头元素.\n");return;}*e = Q->base[Q->front];
}int length(SeqQueue* Q){return Q->rear - Q->front;
}void clear(SeqQueue* Q){Q->front = Q->rear = 0;
}void destroy(SeqQueue* Q){free(Q->base);Q->base = NULL;
}
int main(){SeqQueue Q;initSeqQueue(&Q);int i = 1;for(i;i <= 10;i++){enQueue(&Q,i);}showSeqQueue(&Q);DeQueue(&Q);showSeqQueue(&Q);return 0;
} 

注:上诉使用空一个空间来方便判断空和判满,如果不想浪费这一个空间,可通过设置一个标志位来用于判空和判满。

 

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

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

相关文章

内存对齐和缓冲区溢出攻击

.一、问候语 欢迎你来到我的博客! 二、什么是内存对齐计算机中内存空间都是按照字节(byte)进行划分的,所以从理论上讲对于任何类型的变量访问都可以从任意地址开始,但是在实际情况中,在访问特定类型变量的时候经常在特定的内存地址访问,所以这就需要把各种类型数据按照一定…

ARM SMMU原理与IOMMU技术(“VT-d” DMA、I/O虚拟化、内存虚拟化)

名词缩写ASID:Address Space ID 地址空间标识符 CD:Context Descriptor; 上下文描述符; CTP:Context-table pointer 上下文表指针 EPT:Extended Page Table 扩展页表 GPA:Guest Phyical Address 客人的实际地址 GVA:Guest Virtual Address 访客虚拟地址 HPA:Host…

博客园评论区头像换页更新解决方案

使用 MutationObserver 解决了评论区头像换页无法更新的问题。前言 博客园博客正文的评论区的每一条评论其实都是带用户头像链接的,因此有些博客主题利用这个链接,对评论新增了头像显示功能。 但是这部分功能只能在第一次加载页面时有效,一旦出现评论翻页、排序等操作,头像…

数学知识(初赛)

求最大公约数的技巧 利用辗转相除法,gcd(A,B)=gcd(B,A%B),就可以很快速求解。 应用:化简分数,数学题等等。原理:理解为一个长方形,然后要尽量去铺最大正方形,以满足铺满长方形。最后一个铺满长方形的那个正方形肯定是最大公约数。 如下图,这个长方形先铺一个绿色…

高等数学 2.4 隐函数及由参数方程确定的函数的导数

目录一、隐函数求导二、由参数方程所确定的函数的导数三、相关变化率 一、隐函数求导 函数 \(y = f(x)\) 表示两个变量 \(y\) 与 \(x\) 之间的对应关系,这种对应关系可以用各种不同方式表达,例如 \(y = \sin x\) ,\(y = \ln x + \sqrt{1 - x^2}\) 等。这种函数表达方式的特点…

小林coding学习笔记(内存页面置换算法)

缺页中断示意图1 在CPU里执行一条查找某个页面A的指令,首先是虚拟内存,会到虚拟内存和物理内存的映射关系的页表中查询。 2 页表中不存在需要查找的页面A的有效信息。 3 则触发缺页中断信号给操作系统,操作系统收到缺页中断信号后,就会去磁盘里面查找该页面。 4 操作系统在…

Unity中的三种渲染路径

Unity中的渲染路径 Unity的渲染路径 在Unity里,渲染路径(Rendering Path)决定了光照是如何应用到Unity Shader中的。因此,我们只有为Shader正确地选择和设置了需要的渲染路径,该shader的光照计算才可以被正确执行。 unity中的渲染路径:Forward Rendering Path (向前渲染…

白云龙期货投资-第一讲

# 期货散户投资者常见错误 抄底摸顶,进场无依据,无参照物 小赢大亏(小鸟吃食 大象拉屎资金曲线) 用错误的方法分析行情(金死叉) 过分依赖各种交易软件,公式 持亏损的单子隔夜 死扛(进场条件)期货散户投资者常见错误 抄底摸顶,进场无依据,无参照物 小赢大亏(小鸟吃食 大…

408五级流水线强化课笔记

408强化课录播五级流水线相关内容笔记指令流水线基本概念:简单的概念题 指令流水线基本实现:指令按序发射,按序完成 各种冒险:结合MIPS指令序列分析并处理冒险 超标量和动态流水线:简单的概念题五级流水线 五级流水线的设计是为了通过并行提高处理器的吞吐量。图片来自CSA…

关键字检索分析-案例2:开源代码分析

第一次拿来分析一下开源代码,上效果图代码和文件地址 具体内容参考我前面一篇博客 spring-frameworkredis, redis单个文件非常大mybatis我实话我没有看过这些源码。 之前的博客里的脚本,bug挺多的。可以以gitee这里为主(链接在最上面)。

小林coding学习笔记(进程调度算法)

//进程调度算法 进程调度算法是CPU通过进程调度算法决定某个时刻去调用哪个进程到CPU上运行的算法 1、先来先服务调度算法 每次从就绪队列的队头调度到CPU上运行,直到进程退出或被阻塞,才会继续从队列中调度进程运行。 特点:对短作业不利,对长作业有利,无法平衡短作业与长…

spring mvc详细讲解(前后端分离模式)

在前后端分离模式下,Spring MVC 的作用主要集中在处理后端的业务逻辑和 API 接口,而不再直接管理视图部分。也就是说,Spring MVC 的重点是如何处理客户端的请求并返回数据(通常以 JSON 或 XML 格式),而视图渲染交给前端框架(如 Vue.js、React 等)来完成。 下面是针对前…