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

【“栈、队列”的应用】408数据结构代码

王道数据结构强化课——【“栈、队列”的应用】代码,持续更新
在这里插入图片描述

链式存储栈(单链表实现),并基于上述定义,栈顶在链头,实现“出栈、入栈、判空、判满”四个基本操作

#include <stdio.h>
#include <stdlib.h>// 定义链表节点
struct Node {int data;struct Node* next;
};// 定义栈结构
struct Stack {struct Node* top; // 栈顶指针
};// 初始化栈
void initStack(struct Stack* stack) {stack->top = NULL;
}// 入栈操作
void push(struct Stack* stack, int value) {struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));if (newNode == NULL) {printf("内存分配失败,无法执行入栈操作\n");return;}newNode->data = value;newNode->next = stack->top;stack->top = newNode;
}// 出栈操作
int pop(struct Stack* stack) {if (stack->top == NULL) {printf("栈为空,无法执行出栈操作\n");return -1; // 返回一个错误值}struct Node* temp = stack->top;int poppedValue = temp->data;stack->top = temp->next;free(temp);return poppedValue;
}// 判空操作
int isEmpty(struct Stack* stack) {return (stack->top == NULL);
}// 判满操作(对于链式存储的栈,通常不会满,所以返回0表示不满)
int isFull(struct Stack* stack) {return 0;
}// 释放栈内存
void freeStack(struct Stack* stack) {while (stack->top != NULL) {struct Node* temp = stack->top;stack->top = temp->next;free(temp);}
}int main() {struct Stack stack;initStack(&stack);// 入栈操作push(&stack, 1);push(&stack, 2);push(&stack, 3);// 出栈操作printf("出栈操作: %d\n", pop(&stack));// 判空操作printf("栈是否为空: %s\n", isEmpty(&stack) ? "是" : "否");// 判满操作printf("栈是否满: %s\n", isFull(&stack) ? "是" : "否");// 释放栈内存freeStack(&stack);return 0;
}

链式存储栈(双向链表实现)

#include <stdio.h>
#include <stdlib.h>// 定义链表节点
struct Node {int data;struct Node* next;struct Node* prev;
};// 定义栈结构
struct Stack {struct Node* top; // 栈顶指针,链尾
};// 初始化栈
void initStack(struct Stack* stack) {stack->top = NULL;
}// 入栈操作
void push(struct Stack* stack, int value) {struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));if (newNode == NULL) {printf("内存分配失败,无法执行入栈操作\n");return;}newNode->data = value;newNode->next = NULL;if (stack->top == NULL) {newNode->prev = NULL;stack->top = newNode;} else {newNode->prev = stack->top;stack->top->next = newNode;stack->top = newNode;}
}// 出栈操作
int pop(struct Stack* stack) {if (stack->top == NULL) {printf("栈为空,无法执行出栈操作\n");return -1; // 返回一个错误值}struct Node* temp = stack->top;int poppedValue = temp->data;if (stack->top->prev != NULL) {stack->top = stack->top->prev;stack->top->next = NULL;} else {stack->top = NULL;}free(temp);return poppedValue;
}// 判空操作
int isEmpty(struct Stack* stack) {return (stack->top == NULL);
}// 判满操作(对于链式存储的栈,通常不会满,所以返回0表示不满)
int isFull(struct Stack* stack) {return 0;
}// 释放栈内存
void freeStack(struct Stack* stack) {while (stack->top != NULL) {struct Node* temp = stack->top;stack->top = temp->prev;free(temp);}
}int main() {struct Stack stack;initStack(&stack);// 入栈操作push(&stack, 1);push(&stack, 2);push(&stack, 3);// 出栈操作printf("出栈操作: %d\n", pop(&stack));// 判空操作printf("栈是否为空: %s\n", isEmpty(&stack) ? "是" : "否");// 判满操作printf("栈是否满: %s\n", isFull(&stack) ? "是" : "否");// 释放栈内存freeStack(&stack);return 0;
}

顺序存储的队列(数组实现)

#include <stdio.h>
#include <stdlib.h>#define MAX_QUEUE_SIZE 10 // 队列的最大容量// 定义队列结构
struct Queue {int front, rear; // 前后指针int data[MAX_QUEUE_SIZE];
};// 初始化队列
void initQueue(struct Queue* queue) {queue->front = -1;queue->rear = -1;
}// 判空操作
int isEmpty(struct Queue* queue) {return (queue->front == -1);
}// 判满操作
int isFull(struct Queue* queue) {return ((queue->rear + 1) % MAX_QUEUE_SIZE == queue->front);
}// 入队操作
void enqueue(struct Queue* queue, int value) {if (isFull(queue)) {printf("队列已满,无法执行入队操作\n");return;}if (isEmpty(queue)) {queue->front = 0;}queue->rear = (queue->rear + 1) % MAX_QUEUE_SIZE;queue->data[queue->rear] = value;
}// 出队操作
int dequeue(struct Queue* queue) {if (isEmpty(queue)) {printf("队列为空,无法执行出队操作\n");return -1; // 返回一个错误值}int dequeuedValue = queue->data[queue->front];if (queue->front == queue->rear) {// 队列中只有一个元素,出队后队列为空queue->front = -1;queue->rear = -1;} else {queue->front = (queue->front + 1) % MAX_QUEUE_SIZE;}return dequeuedValue;
}int main() {struct Queue queue;initQueue(&queue);// 入队操作enqueue(&queue, 1);enqueue(&queue, 2);enqueue(&queue, 3);// 出队操作printf("出队操作: %d\n", dequeue(&queue));// 判空操作printf("队列是否为空: %s\n", isEmpty(&queue) ? "是" : "否");// 判满操作printf("队列是否满: %s\n", isFull(&queue) ? "是" : "否");return 0;
}

链式存储队列(单链表实现)

#include <stdio.h>
#include <stdlib.h>// 定义链表节点
struct Node {int data;struct Node* next;
};// 定义队列结构
struct Queue {struct Node* front; // 队列前端struct Node* rear;  // 队列后端
};// 初始化队列
void initQueue(struct Queue* queue) {queue->front = NULL;queue->rear = NULL;
}// 判空操作
int isEmpty(struct Queue* queue) {return (queue->front == NULL);
}// 判满操作(对于链式存储的队列,通常不会满,所以返回0表示不满)
int isFull(struct Queue* queue) {return 0;
}// 入队操作
void enqueue(struct Queue* queue, int value) {struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));if (newNode == NULL) {printf("内存分配失败,无法执行入队操作\n");return;}newNode->data = value;newNode->next = NULL;if (isEmpty(queue)) {queue->front = newNode;} else {queue->rear->next = newNode;}queue->rear = newNode;
}// 出队操作
int dequeue(struct Queue* queue) {if (isEmpty(queue)) {printf("队列为空,无法执行出队操作\n");return -1; // 返回一个错误值}struct Node* temp = queue->front;int dequeuedValue = temp->data;queue->front = temp->next;free(temp);if (queue->front == NULL) {// 如果出队后队列为空,需要更新rear指针queue->rear = NULL;}return dequeuedValue;
}// 释放队列内存
void freeQueue(struct Queue* queue) {while (queue->front != NULL) {struct Node* temp = queue->front;queue->front = temp->next;free(temp);}
}int main() {struct Queue queue;initQueue(&queue);// 入队操作enqueue(&queue, 1);enqueue(&queue, 2);enqueue(&queue, 3);// 出队操作printf("出队操作: %d\n", dequeue(&queue));// 判空操作printf("队列是否为空: %s\n", isEmpty(&queue) ? "是" : "否");// 判满操作printf("队列是否满: %s\n", isFull(&queue) ? "是" : "否");// 释放队列内存freeQueue(&queue);return 0;
}

相关文章:

【“栈、队列”的应用】408数据结构代码

王道数据结构强化课——【“栈、队列”的应用】代码&#xff0c;持续更新 链式存储栈&#xff08;单链表实现&#xff09;&#xff0c;并基于上述定义&#xff0c;栈顶在链头&#xff0c;实现“出栈、入栈、判空、判满”四个基本操作 #include <stdio.h> #include <…...

es的nested查询

一、一层嵌套 mapping: PUT /nested_example {"mappings": {"properties": {"name": {"type": "text"},"books": {"type": "nested","properties": {"title": {"t…...

<一>Qt斗地主游戏开发:开发环境搭建--VS2019+Qt5.15.2

1. 开发环境概述 对于Qt的开发环境来说&#xff0c;主流编码IDE界面一般有两种&#xff1a;Qt Creator或VSQt。为了简单起见&#xff0c;这里的操作系统限定为windows&#xff0c;编译器也通用VS了。Qt版本的话自己选择就可以了&#xff0c;当然VS的版本也是依据Qt版本来选定的…...

python:进度条的使用(tqdm)

摘要&#xff1a;为python程序进度条&#xff0c;可以知道程序运行进度。 python中&#xff0c;常用的进度条模块是tqdm&#xff0c;将介绍tqdm的安装和使用 1、安装tqdm: pip install tqdm2、tqdm的使用&#xff1a; &#xff08;1&#xff09;在for循环中的使用&#xff1…...

Java类型转换和类型提升

目录 一、类型转换 1.1 自动类型转换&#xff08;隐式&#xff09; 1.1.1 int 与 long 之间 1.1.2 float 与 double 之间 1.1.3 int 与 byte 之间 1.2 强制类型转换&#xff08;显示&#xff09; 1.2.1 int 与 long 之间 1.2.2 float 与 double 之间 1.2.3 int 与 d…...

C# 读取 Excel xlsx 文件,显示在 DataGridView 中

编写 read_excel.cs 如下 using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Data; using System.Linq; using System.Text; using System.Data.OleDb;namespace ReadExcel {public partial class Program{static…...

Docker02基本管理

目录 1、Docker 网络 1.1 Docker 网络实现原理 1.2 Docker 的网络模式 1.3 网络模式详解 1.4 资源控制 1.5 进行CPU压力测试 1.6 清理docker占用的磁盘空间 1.7 生产扩展 1、Docker 网络 1.1 Docker 网络实现原理 Docker使用Linux桥接&#xff0c;在宿主机虚拟一个Docke…...

Scala第十章

Scala第十章 章节目标 1.数组 2.元组 3.列表 4.集 5.映射 6.迭代器 7.函数式编程 8.案例&#xff1a;学生成绩单 scala总目录 文档资料下载...

10.4 校招 实习 内推 面经

绿泡*泡&#xff1a; neituijunsir 交流裙 &#xff0c;内推/实习/校招汇总表格 1、校招 | 集度2024届秋招正式启动&#xff08;内推&#xff09; 校招 | 集度2024届秋招正式启动&#xff08;内推&#xff09; 2、校招 | 道通科技2024秋季校园招聘正式启动啦&#xff01; …...

从0开始深入理解并发、线程与等待通知机制(中)

一&#xff0c;深入学习 Java 的线程 线程的状态/生命周期 Java 中线程的状态分为 6 种&#xff1a; 1. 初始(NEW)&#xff1a;新创建了一个线程对象&#xff0c;但还没有调用 start()方法。 2. 运行(RUNNABLE)&#xff1a;Java 线程中将就绪&#xff08;ready&#xff09;和…...

UE5报错及解决办法

1、编译报错&#xff0c;内容如下&#xff1a; Unable to build while Live Coding is active. Exit the editor and game, or press CtrlAltF11 if iterating on code in the editor or game 解决办法 取消Enable Live Coding勾选...

怎么通过docker/portainer部署vue项目

这篇文章分享一下如何通过docker将vue项目打包成镜像文件&#xff0c;并使用打包的镜像在docker/portainer上部署运行&#xff0c;写这篇文章参考了vue-cli和docker的官方文档。 首先&#xff0c;阅读vue-cli关于docker部署的说明&#xff0c;上面提供了关键的几个步骤。 从上面…...

【面试经典150 | 矩阵】旋转图像

文章目录 写在前面Tag题目来源题目解读解题思路方法一&#xff1a;原地旋转方法二&#xff1a;翻转代替旋转 写在最后 写在前面 本专栏专注于分析与讲解【面试经典150】算法&#xff0c;两到三天更新一篇文章&#xff0c;欢迎催更…… 专栏内容以分析题目为主&#xff0c;并附带…...

机器人制作开源方案 | 家庭清扫拾物机器人

作者&#xff1a;罗诚、李旭洋、胡旭、符粒楷 单位&#xff1a;南昌交通学院 人工智能学院 指导老师&#xff1a;揭吁菡 在家庭中我们有时无法到一些低矮阴暗的地方进行探索&#xff0c;比如茶几下或者床底下&#xff0c;特别是在部分家庭中&#xff0c;如果没有及时对这些阴…...

C++算法 —— 动态规划(8)01背包问题

文章目录 1、动规思路简介2、模版题&#xff1a;01背包第一问第二问优化 3、分割等和子集4、目标和5、最后一块石头的重量Ⅱ 背包问题需要读者先明白动态规划是什么&#xff0c;理解动规的思路&#xff0c;并不能给刚接触动规的人学习。所以最好是看了之前的动规博客&#xff0…...

ASUS华硕天选4笔记本FA507NU7735H_4050原装出厂Win11系统

下载链接&#xff1a;https://pan.baidu.com/s/1puxQOxk4Rbno1DqxhkvzXQ?pwdhkzz 系统自带网卡、显卡、声卡等所有驱动、出厂主题壁纸、Office办公软件、MyASUS华硕电脑管家、奥创控制中心等预装程序...

金蝶OA server_file 目录遍历漏洞

漏洞描述 金蝶OA server_file 存在目录遍历漏洞&#xff0c;攻击者通过目录遍历可以获取服务器敏感信息 漏洞影响 金蝶OA 漏洞复现 访问漏洞url&#xff1a; 漏洞POC Windows服务器&#xff1a; appmonitor/protected/selector/server_file/files?folderC://&suffi…...

read_image错误

File is no BMP-File(Halcon 错误代码5560&#xff09;类似的错误一般都是图片内部封装的格式与外部扩展名不一致导致&#xff08;也就是扩展名并不是真实图片的格式扩展&#xff09;。 通过软件“UltraEdit”(http://www.onlinedown.net/soft/7752.htm)使用16进制查看&#x…...

文本分词排序

文本分词 在这个代码的基础上 把英语单词作为一类汉语&#xff0c;作为一类然后列出选项 1. 大小排序 2. 小大排序 3. 不排序打印保存代码 import jieba# 输入文本&#xff0c;让我陪你聊天吧~ lines [] print("请输入多行文本&#xff0c;以\"2333.3\"结束&am…...

SQL与关系数据库基本操作

SQL与关系数据库基本操作 文章目录 第一节 SQL概述一、SQL的发展二、SQL的特点三、SQL的组成 第二节 MySQL预备知识一、MySQL使用基础二、MySQL中的SQL1、常量&#xff08;1&#xff09;字符串常量&#xff08;2&#xff09;数值常量&#xff08;3&#xff09;十六进制常量&…...

(二)TensorRT-LLM | 模型导出(v0.20.0rc3)

0. 概述 上一节 对安装和使用有个基本介绍。根据这个 issue 的描述&#xff0c;后续 TensorRT-LLM 团队可能更专注于更新和维护 pytorch backend。但 tensorrt backend 作为先前一直开发的工作&#xff0c;其中包含了大量可以学习的地方。本文主要看看它导出模型的部分&#x…...

python如何将word的doc另存为docx

将 DOCX 文件另存为 DOCX 格式&#xff08;Python 实现&#xff09; 在 Python 中&#xff0c;你可以使用 python-docx 库来操作 Word 文档。不过需要注意的是&#xff0c;.doc 是旧的 Word 格式&#xff0c;而 .docx 是新的基于 XML 的格式。python-docx 只能处理 .docx 格式…...

sqlserver 根据指定字符 解析拼接字符串

DECLARE LotNo NVARCHAR(50)A,B,C DECLARE xml XML ( SELECT <x> REPLACE(LotNo, ,, </x><x>) </x> ) DECLARE ErrorCode NVARCHAR(50) -- 提取 XML 中的值 SELECT value x.value(., VARCHAR(MAX))…...

TRS收益互换:跨境资本流动的金融创新工具与系统化解决方案

一、TRS收益互换的本质与业务逻辑 &#xff08;一&#xff09;概念解析 TRS&#xff08;Total Return Swap&#xff09;收益互换是一种金融衍生工具&#xff0c;指交易双方约定在未来一定期限内&#xff0c;基于特定资产或指数的表现进行现金流交换的协议。其核心特征包括&am…...

2025盘古石杯决赛【手机取证】

前言 第三届盘古石杯国际电子数据取证大赛决赛 最后一题没有解出来&#xff0c;实在找不到&#xff0c;希望有大佬教一下我。 还有就会议时间&#xff0c;我感觉不是图片时间&#xff0c;因为在电脑看到是其他时间用老会议系统开的会。 手机取证 1、分析鸿蒙手机检材&#x…...

工业自动化时代的精准装配革新:迁移科技3D视觉系统如何重塑机器人定位装配

AI3D视觉的工业赋能者 迁移科技成立于2017年&#xff0c;作为行业领先的3D工业相机及视觉系统供应商&#xff0c;累计完成数亿元融资。其核心技术覆盖硬件设计、算法优化及软件集成&#xff0c;通过稳定、易用、高回报的AI3D视觉系统&#xff0c;为汽车、新能源、金属制造等行…...

Springboot社区养老保险系统小程序

一、前言 随着我国经济迅速发展&#xff0c;人们对手机的需求越来越大&#xff0c;各种手机软件也都在被广泛应用&#xff0c;但是对于手机进行数据信息管理&#xff0c;对于手机的各种软件也是备受用户的喜爱&#xff0c;社区养老保险系统小程序被用户普遍使用&#xff0c;为方…...

论文阅读笔记——Muffin: Testing Deep Learning Libraries via Neural Architecture Fuzzing

Muffin 论文 现有方法 CRADLE 和 LEMON&#xff0c;依赖模型推理阶段输出进行差分测试&#xff0c;但在训练阶段是不可行的&#xff0c;因为训练阶段直到最后才有固定输出&#xff0c;中间过程是不断变化的。API 库覆盖低&#xff0c;因为各个 API 都是在各种具体场景下使用。…...

深度学习之模型压缩三驾马车:模型剪枝、模型量化、知识蒸馏

一、引言 在深度学习中&#xff0c;我们训练出的神经网络往往非常庞大&#xff08;比如像 ResNet、YOLOv8、Vision Transformer&#xff09;&#xff0c;虽然精度很高&#xff0c;但“太重”了&#xff0c;运行起来很慢&#xff0c;占用内存大&#xff0c;不适合部署到手机、摄…...

0x-3-Oracle 23 ai-sqlcl 25.1 集成安装-配置和优化

是不是受够了安装了oracle database之后sqlplus的简陋&#xff0c;无法删除无法上下翻页的苦恼。 可以安装readline和rlwrap插件的话&#xff0c;配置.bahs_profile后也能解决上下翻页这些&#xff0c;但是很多生产环境无法安装rpm包。 oracle提供了sqlcl免费许可&#xff0c…...