【数据结构与算法】栈和队列(下)

news/2024/10/18 7:13:24/

记录自己所学,无详细讲解

队列的实现--使用动态链表

1.项目目录文件

2.头文件 queue.h

#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
struct QueueNode
{int data;struct Queue* next;
};
typedef struct QueueNode QueueNode;
typedef struct Queue
{QueueNode* head;QueueNode* tail;
}Queue;
void QueueInit(Queue* p); //初始化
void QueuePush(Queue* p,int n); //队尾入队列
void QueuePop(Queue* p); //队头出队列
int QueueFront(Queue* p); //队列头元素
int QueueBack(Queue* p); //队列列尾元素
bool QueueEmpty(Queue* p); //是否为空队列

3.函数定义源文件 queue.c

#include "queue.h"
void QueueInit(Queue* p)
{p->head = NULL;p->tail = NULL;
}
void QueuePush(Queue* p, int n)
{QueueNode * newnode = (QueueNode*)malloc(sizeof(QueueNode));assert(newnode);newnode->data = n;newnode->next = NULL;if (p->tail == NULL){p->head = newnode;p->tail = newnode;}else{(p->tail)->next = newnode;p->tail = (p->tail)->next;}
}
void QueuePop(Queue* p)
{assert(p); assert(p->head);QueueNode* newhead = (p->head)->next;free(p->head);p->head = newhead;if (p->head == NULL){p->tail = NULL;}
}
int QueueFront(Queue* p)
{assert(p->head);return (p->head)->data;
}
int QueueBack(Queue* p)
{assert(p->tail);return (p->tail)->data;
}
bool QueueEmpty(Queue* p)
{assert(p);return p->head == NULL;
}

4.函数调用测试源文件test.c

#include "queue.h"
int main()
{Queue p;QueueInit(&p);QueuePush(&p,3);QueuePush(&p, 4);QueuePush(&p, 5);QueuePop(&p);QueuePop(&p);QueuePop(&p);printf("%3d",QueueFront(&p));printf("%3d",QueueBack(&p));if (QueueEmpty(&p)){printf("为空");}else{printf("不为空");}return 0;
}


http://www.ppmy.cn/news/1539930.html

相关文章

软件框架和软件架构的概念

软件框架&#xff08;Software Framework&#xff09; 和 软件架构&#xff08;Software Architecture&#xff09; 的关系在于它们分别处于系统设计和实现的不同层次&#xff0c;互为补充。架构决定了系统的整体结构和设计&#xff0c;而框架则是实现这些设计的工具和技术。下…

BMC 中的日志类型:Audit Log、SEL Log、Sys Log 与 SOL Log 的功能与区别

在现代服务器和数据中心管理中&#xff0c;BMC&#xff08;Baseboard Management Controller&#xff09;作为一种关键的管理组件&#xff0c;负责监控和管理硬件设备的状态。为了确保系统的安全性和操作的可追溯性&#xff0c;BMC 提供了多种类型的日志&#xff0c;以记录不同…

C++算法练习-day5——59.螺旋矩阵2

题目来源&#xff1a;. - 力扣&#xff08;LeetCode&#xff09; 题目思路分析 给定一个整数 n&#xff0c;要求生成一个 n x n 的矩阵&#xff0c;其中的元素按螺旋顺序排列&#xff0c;从矩阵的左上角开始&#xff0c;向右、向下、向左、向上依次填充&#xff0c;直到所有元…

PROFINET开发EtherNet/IP开发Vline板卡在称重设备行业的应用

本次分享的&#xff0c;是我们VlinePROFINET开发EtherNet/IP开发嵌入式板卡在称重行业的典型应用。 应用背景 在现代科技高度发达的时代&#xff0c;无论是科学研究、医疗诊断、制药生产还是工业制造&#xff0c;准确的测量和称重都是保证质量和效率的关键。 随着新项目实施…

JavaWeb 22.Node.js_简介和安装

有时候&#xff0c;后退原来是向前 —— 24.10.7 一、什么是Node.js Node.js 是一个于 Chrome V8 的 JavaScript 运行时环境&#xff0c;可以使 JavaScript 运行在服务器端。使用 Node.js&#xff0c;可以方便地开发服务器端应用程序&#xff0c;如 Web 应用、API、后端服务&a…

C++_Stack和Queue的使用及其模拟实现

✨✨ 欢迎大家来到小伞的大讲堂✨✨ &#x1f388;&#x1f388;养成好习惯&#xff0c;先赞后看哦~&#x1f388;&#x1f388; 所属专栏&#xff1a;C学习 小伞的主页&#xff1a;xiaosan_blog 1.stack的介绍及使用 1.1stack的介绍 stack的文档介绍 深入了解栈在数据结构中…

SpringCloudStream使用StreamBridge实现延时队列

利用RabbitMQ实现消息的延迟队列 一、安装RabbitMQ 1、安装rabbitmq 安装可以看https://blog.csdn.net/qq_38618691/article/details/118223851,进行安装。 2、安装插件 安装完毕后,exchange是不支持延迟类型的,需要手动安装插件,需要和安装的rabbitmq版本一致 https:…

超级图床源码,自带图片压缩功能

超级图床源码&#xff0c;自带图片压缩功能 来自其他网站搬砖 一款专为个人需求设计的高效图床解决方案&#xff0c;集成了强大的图片压缩功 项目结构精简高效&#xff0c;提供自定义图片压缩率与尺寸设置&#xff0c;有效降低存储与带宽成本。 支持上传JPEG、PNG、GIF格式…