当前位置: 首页 > news >正文

数据结构系列-队列的结构和队列的实现

🌈个人主页:羽晨同学 

💫个人格言:“成为自己未来的主人~”  

队列

队列的概念及结构

队列:只允许在一端进行插入数据操作,在另一端进行删除删除数据操作的特殊线性表,队列具有先进先出FIFO,进行插入操作的一端称为队尾,进行删除操作的一端称为队头

队列的实现

队列也可以数组和链表的结构实现,使用链表的结构实现更优一点,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低

#pragma once
#include<stdio.h>
#include<stdbool.h>
#include<assert.h>typedef int QDataType;
typedef struct QueueNode
{int val;struct QueueNode* next;
}QNode;typedef struct Queue
{QNode* phead;QNode* ptail;int size;
}Queue;void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
//入队列
void QueuePush(Queue* pq, QDataType x);
//出队列
void QueuePop(Queue* pq);QDataType QueueFront(Queue*pq);
QDataType QueueBack(Queue* pq);
bool QueueEmpth(Queue* pq);
int QueueSize(Queue* pq);
#define _CRT_SECURE_NO_WARNINGS
#include"code.4.5.Queue.h"
void QueueInit(Queue* pq) {assert(pq);pq->phead = NULL;pq->ptail = NULL;pq->size = 0;
}void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->phead;while (cur) {QNode* next = cur->next;free(cur);cur = next;}pq->phead = pq->ptail = NULL;pq->size = 0;
}//入队列
void QueuePush(Queue* pq, QDataType x) {assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL) {perror("malloc fail");return;}newnode->val = x;newnode->next = NULL;if(pq->ptail){pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}
void QueuePop(Queue* pq)
{assert(pq);assert(pq->phead != NULL);if (pq->phead->next == NULL) {free(pq->phead);pq->phead = pq->ptail = NULL;}else{QNode* next = pq->phead->next;free(pq->phead);pq->phead = next;}pq->size--;
}QDataType QueueFront(Queue* pq) {assert(pq);assert(pq->phead != NULL);return pq->phead->val;
}
QDataType QueueBack(Queue* pq) {assert(pq);assert(pq->ptail != NULL);return pq->ptail->val;
}
bool QueueEmpth(Queue* pq)
{assert(pq);return pq->size == 0;
}
int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}
#define _CRT_SECURE_NO_WARNINGS
#include"code.4.5.stack.h"
//int main() {
//	ST s;
//	STInit(&s);
//	STPush(&s,1);
//	STPush(&s,2);
//	STPush(&s,3);
//	int top = STTop(&s);
//	printf("%d", top);
//
//	STDestroy(&s);
//	return 0;
//}
#include"code.4.5.Queue.h"
int main()
{Queue q;QueueInit(&q);QueuePush(&q, 1);QueuePush(&q, 2);printf("%d ", QueueFront(&q));QueuePop(&q);QueuePush(&q, 3);QueuePush(&q, 4);while (!QueueEmpth(&q)){printf("%d ", QueueFront(&q));QueuePop(&q);}QueueDestroy(&q);return 0;
}

http://www.lryc.cn/news/334302.html

相关文章:

  • MySQL——查询数据的处理
  • 【机器学习300问】59、计算图是如何帮助人们理解反向传播的?
  • ctfshow web入门 php特性 web108--web115
  • 京东API接口采集商品详情数据(测试入口如下)
  • Mac brew 安装软件
  • 【顶部距离计算】计算元素顶部与浏览器顶部的距离
  • 守护人类健康:人工智能赋能医疗领域创新应用
  • linux常用指令(一)——cat、more、cp
  • 基于RTThread的学习(三):正点原子潘多拉 QSPI 通信 W25Q128 实验
  • Mac反编译APK
  • Java数据结构-队列
  • JVM专题——类文件结构
  • 零基础10 天入门 Web3之第2天
  • Vue和FastAPI实现前后端分离
  • 34470A是德科技34470A数字万用表
  • iOS 开发中上传 IPA 文件的方法(无需 Mac 电脑
  • c语言多媒体文件管理及检索系统220
  • 链表之双向链表的实现
  • 小白学大模型:什么是生成式人工智能?
  • 并发编程01-深入理解Java并发/线程等待/通知机制
  • 3.类与对象(中篇)介绍了类的6个默认构造函数,列举了相关案例,实现了一个日期类
  • Vue实现手机APP页面的切换,如何使用Vue Router进行路由管理呢?
  • 软考--软件设计师(软件工程总结2)
  • 渗透测试之SSRF漏洞
  • 【C++】1957. 求三个数的平均数
  • GPU部署ChatGLM3
  • Windows远程执行
  • AJAX —— 学习(一)
  • Activity——idea(2020以后)配置actiBPM
  • MyBatis——配置优化和分页插件