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

C语言数据结构-----栈和队列练习题(分析+代码)

前言

前面的博客写了如何实现栈和队列,下来我们来看一下队列和栈的相关习题。

链接: 栈和队列的实现

文章目录

  • 前言
  • 1.用栈实现括号匹配
  • 2.用队列实现栈
  • 3.用栈实现队列
  • 4.设计循环队列


1.用栈实现括号匹配

在这里插入图片描述

此题最重要的就是数量匹配和顺序匹配。
用栈可以完美的做到:
1.左括号入栈
2.有右括号,取栈顶左括号匹配

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
typedef char STDataType;typedef struct Stack
{STDataType* a;int top;		// 标识栈顶位置的int capacity;
}ST;void STInit(ST* pst);
void STDestroy(ST* pst);// 栈顶插入删除
void STPush(ST* pst, STDataType x);
void STPop(ST* pst);
STDataType STTop(ST* pst);bool STEmpty(ST* pst);
int STSize(ST* pst);void STInit(ST* pst)
{assert(pst);pst->a = NULL;pst->capacity = 0;// 表示top指向栈顶元素的下一个位置pst->top = 0;// 表示top指向栈顶元素
//pst->top = -1;
}void STDestroy(ST* pst)
{assert(pst);free(pst->a);pst->a = NULL;pst->top = pst->capacity = 0;
}// 栈顶插入删除
void STPush(ST* pst, STDataType x)
{assert(pst);if (pst->top == pst->capacity){int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType) * newcapacity);if (tmp == NULL){perror("realloc fail");return;}pst->a = tmp;pst->capacity = newcapacity;}pst->a[pst->top] = x;pst->top++;
}void STPop(ST* pst)
{assert(pst);// 不为空assert(pst->top > 0);pst->top--;
}STDataType STTop(ST* pst)
{assert(pst);// 不为空assert(pst->top > 0);return pst->a[pst->top - 1];
}bool STEmpty(ST* pst)
{assert(pst);/*if (pst->top == 0){return true;}else{return false;}*/return pst->top == 0;
}int STSize(ST* pst)
{assert(pst);return pst->top;
}
//这里以上都是栈的代码
bool isValid(char* s)
{ST st;STInit(&st);while (*s){if (*s == '[' || *s == '(' || *s == '{'){STPush(&st, *s);}else{if (STEmpty(&st)!=NULL)//右括号比左括号多{STDestroy(&st);return false;}char top = STTop(&st);STPop(&st);if ((*s == ']' && top != '[')//控制顺序不匹配|| (*s == '}' && top != '{')|| (*s == ')' && top != '(')){STDestroy(&st);return false;}}++s;}bool ret = STEmpty(&st);//如果栈空了,那么就是都匹配走了STDestroy(&st);         //只要不空,说明没匹配完,但只能解决左括号多,右括号少的问题return ret;
}

2.用队列实现栈

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
这样感觉没什么用,很低效,但是这题考的就是我们对于队列和栈性质的理解!

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int QDataType;
typedef struct QueueNode
{QDataType 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 QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);void QueueInit(Queue* pq)
{assert(pq);pq->phead = 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 == NULL){pq->ptail = pq->phead = newnode;}else{pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}void QueuePop(Queue* pq)
{assert(pq);// assert(pq->phead);QNode* del = pq->phead;pq->phead = pq->phead->next;free(del);del = NULL;if (pq->phead == NULL)pq->ptail = NULL;pq->size--;
}QDataType QueueFront(Queue* pq)
{assert(pq);// assert(pq->phead);return pq->phead->val;
}QDataType QueueBack(Queue* pq)
{assert(pq);// assert(pq->ptail);return pq->ptail->val;
}bool QueueEmpty(Queue* pq)
{assert(pq);return pq->phead == NULL;
}int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}
//这里之前都是队列的代码
typedef struct //匿名结构体
{Queue q1;Queue q2;
} MyStack;MyStack* myStackCreate() 
{MyStack* pst = (MyStack*)malloc(sizeof(MyStack));QueueInit(&pst->q1);QueueInit(&pst->q2);return pst;
}void myStackPush(MyStack* obj, int x) //插入,哪个不为空就往哪里插,都为空随便进一个都行
{if (!QueueEmpty(&obj->q1))QueuePush(&obj->q1, x);elseQueuePush(&obj->q2, x);
}int myStackPop(MyStack* obj) 
{//假设q1为空,q2不为空Queue* emptyq=&obj->q1;Queue* nonemptyq=&obj->q2;if (!QueueEmpty(&obj->q1))//如果假设错了,就交换{emptyq = &obj->q2;nonemptyq = &obj->q1;   }//非空队列钱N-1个元素导入空队列,最后一个就是栈顶元素while (QueueSize(nonemptyq)>1){QueuePush(emptyq, QueueFront(nonemptyq));//将非空队列插入空队列QueuePop(nonemptyq);//删掉}//并返回栈顶元素int top=QueueFront(nonemptyq);QueuePop(nonemptyq);return top;
}int myStackTop(MyStack* obj) 
{//谁不为空就返回谁的队尾数据,队尾数据就是导数据之后栈顶的数据if (!QueueEmpty(&obj->q1))return QueueBack(&obj->q1);elsereturn QueueBack(&obj->q2);
}bool myStackEmpty(MyStack* obj) 
{return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);//两个都为空才是空
}void myStackFree(MyStack* obj) 
{QueueDestroy(&obj->q1);QueueDestroy(&obj->q2);free(obj);
}

3.用栈实现队列

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

和队列实现栈的思路一样,主要考察栈和队列的性质

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>typedef int STDataType;typedef struct Stack
{STDataType* a;int top;		// 标识栈顶位置的int capacity;
}ST;void STInit(ST* pst);
void STDestroy(ST* pst);// 栈顶插入删除
void STPush(ST* pst, STDataType x);
void STPop(ST* pst);
STDataType STTop(ST* pst);bool STEmpty(ST* pst);
int STSize(ST* pst);void STInit(ST* pst)
{assert(pst);pst->a = NULL;pst->capacity = 0;// 表示top指向栈顶元素的下一个位置pst->top = 0;// 表示top指向栈顶元素//pst->top = -1;
}void STDestroy(ST* pst)
{assert(pst);free(pst->a);pst->a = NULL;pst->top = pst->capacity = 0;
}// 栈顶插入删除
void STPush(ST* pst, STDataType x)
{assert(pst);if (pst->top == pst->capacity){int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType) * newcapacity);if (tmp == NULL){perror("realloc fail");return;}pst->a = tmp;pst->capacity = newcapacity;}pst->a[pst->top] = x;pst->top++;
}void STPop(ST* pst)
{assert(pst);// 不为空assert(pst->top > 0);pst->top--;
}STDataType STTop(ST* pst)
{assert(pst);// 不为空assert(pst->top > 0);return pst->a[pst->top - 1];
}bool STEmpty(ST* pst)
{assert(pst);/*if (pst->top == 0){return true;}else{return false;}*/return pst->top == 0;
}int STSize(ST* pst)
{assert(pst);return pst->top;
}
//以上为栈的代码
typedef struct 
{ST pushst;//入数据栈ST popst;//出数据栈
} MyQueue;MyQueue* myQueueCreate() 
{MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));STInit(&obj->popst);STInit(&obj->pushst);return obj;
}void myQueuePush(MyQueue* obj, int x) 
{STPush(&obj->pushst, x);
}int myQueuePop(MyQueue* obj)
{int front = myQueuePeek(obj);//有数据不动,没数据导数据STPop(&obj->popst);return front;
}int myQueuePeek(MyQueue* obj) //返回队列开头的元素
{if (STEmpty(&obj->popst))//如果popst不为空,跳到最后返回栈顶元素{                        while (!STEmpty(&obj->pushst)){//popst为空,那就导数据,把pushst的数据导入popst,再返回栈顶元素STPush(&obj->popst, STTop(&obj->pushst));STPop(&obj->pushst);}}return STTop(&obj->popst);//返回栈顶元素
}bool myQueueEmpty(MyQueue* obj) 
{return STEmpty(&obj->popst) && STEmpty(&obj->pushst);
}void myQueueFree(MyQueue* obj) 
{STDestroy(&obj->popst);STDestroy(&obj->pushst);free(obj);
}

部分函数的结构如下:
在这里插入图片描述

4.设计循环队列

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdbool.h>typedef struct
{int* a;//数组指针(一开始开的空间)int front;//头int back;//尾int k;//数据个数
} MyCircularQueue;MyCircularQueue* myCircularQueueCreate(int k)
{MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));obj->a = (int*)malloc(sizeof(int) * (k + 1));obj->front = 0;obj->back = 0;obj->k = k;return obj;
}bool myCircularQueueIsEmpty(MyCircularQueue* obj)
{return obj->front == obj->back;//头等于尾是空
}bool myCircularQueueIsFull(MyCircularQueue* obj)
{return (obj->back + 1) % (obj->k + 1) == obj->front;//上述图片分析出的公式
}bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) //插入
{if (myCircularQueueIsFull(obj))return false;              //如果满了就返回错obj->a[obj->back] = value;obj->back++;obj->back %= (obj->k + 1);//防止越界return true;
}bool myCircularQueueDeQueue(MyCircularQueue* obj) //删除
{if (myCircularQueueIsEmpty(obj))return false;++obj->front;obj->front %= (obj->k + 1);return true;
}int myCircularQueueFront(MyCircularQueue* obj) //从队首获取元素。如果队列为空,返回 -1 。
{if (myCircularQueueIsEmpty(obj))return -1;return obj->a[obj->front];
}int myCircularQueueRear(MyCircularQueue* obj) //获取队尾元素。如果队列为空,返回 -1 。
{if (myCircularQueueIsEmpty(obj))return -1;if (obj->back == 0)return obj->a[obj->k];elsereturn obj->a[obj->back - 1];
}void myCircularQueueFree(MyCircularQueue* obj)
{free(obj->a);free(obj);
}

部分代码结构如下:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

相关文章:

C语言数据结构-----栈和队列练习题(分析+代码)

前言 前面的博客写了如何实现栈和队列&#xff0c;下来我们来看一下队列和栈的相关习题。 链接: 栈和队列的实现 文章目录 前言1.用栈实现括号匹配2.用队列实现栈3.用栈实现队列4.设计循环队列 1.用栈实现括号匹配 此题最重要的就是数量匹配和顺序匹配。 用栈可以完美的做到…...

uniapp基础-教程之HBuilderX配置篇-01

uniapp教程之HBuilderX配置篇-01 为什么要做这个教程的梳理&#xff0c;主要用于自己学习和总结&#xff0c;利于增加自己的积累和记忆。首先下载HBuilderX&#xff0c;并保证你的软件在C盘进行运行&#xff0c;最好使用英文或者拼音&#xff0c;这个操作是为了保证软件的稳定…...

【备忘录】快速回忆ElasticSearch的CRUD

导引——第一条ElasticSearch语句 测试分词器 POST /_analyze {"text":"黑马程序员学习java太棒了","analyzer": "ik_smart" }概念 语法规则 HTTP_METHOD /index/_action/IDHTTP_METHOD 是 HTTP 请求的方法&#xff0c;常见的包括…...

影响PPC广告成本预算的因素,如何计算亚马逊PPC广告预算——站斧浏览器

亚马逊PPC&#xff0c;又称按点击付费(Pay Per Click)&#xff0c;是一种只有用户点击你的广告时才会向你收费的模式。那么影响PPC广告成本预算的因素,如何计算亚马逊PPC广告预算&#xff1f; 影响PPC广告成本预算的因素 1、产品类别&#xff1a;不同类别的产品竞争程度不同&…...

Qt 信号和槽

目录 概念 代码 mainwindow.h me.h xiaohuang.h main.cc mainwindow.cc me.cc xianghuang.cc mainwindow.ui 自定义信号的要求和注意事项: 自定义槽的要求和注意事项: 概念 信号槽是 Qt 框架引以为豪的机制之一。所谓信号槽&#xff0c;实际就是观察者模式(发布-订…...

Linux基本命令二

Linux基本命令二 1、head 命令 head ​ **作用&#xff1a;**用于查看文件的开头部分的内容&#xff0c;有一个常用的参数 -n 用于显示行数&#xff0c;默认为 10&#xff0c;即显示 10 行的内容 ​ **语法&#xff1a;**head [参数] [文件] ​ 命令参数&#xff1a; 参数…...

isbn api开放接口

接口地址&#xff1a;http://openapi.daohe168.com.cn/api/library/isbn/query?isbn9787115618085&appKeyd7c6c07a0a04ba4e65921e2f90726384 响应结果&#xff1a; { "success": true, "code": "200", "message": …...

助力企业实现更简单的数据库管理,ATOMDB 与 TDengine 完成兼容性互认

为加速数字化转型进程&#xff0c;当下越来越多的企业开始进行新一轮数据架构改造升级。在此过程中&#xff0c;全平台数据库管理客户端提供了一个集中管理和操作数据库的工具&#xff0c;提高了数据库管理的效率和便利性&#xff0c;减少了人工操作的复杂性和错误率&#xff0…...

如何通过低代码工具,提升运输行业的运营效率和服务质量

《中国数字货运发展报告》显示&#xff0c;2022年我国公路货运市场规模在5万亿元左右。其中&#xff0c;数字货运整体市场规模约为7000亿元&#xff0c;市场渗透率约为15%。而以小微企业为主的货运行业&#xff0c;却以小、散、乱的行业特征&#xff0c;承载着5万亿元左右的市场…...

Vue3中调用外部iframe链接方法

业务场景&#xff0c;点击某个按钮需要跳转到外部iframe的地址&#xff0c;但是需要在本项目内显示。以前项目中写过调用外部链接的功能&#xff0c;是有菜单的&#xff0c;但是这次是按钮&#xff0c;所以不能直接把地址配到菜单里。 实现方法&#xff1a;在本地路由文件里写个…...

Node——事件的监听与触发

Node.js是由事件驱动的&#xff0c;每个任务都可以当作一个事件来处理&#xff0c;本贴将对Node.js中的events模块及其中处理事件的类EventEmitter的使用进行详细讲解。 1、EventEmitter对象 在JavaScript中&#xff0c;通过事件可以处理许多用户的交互&#xff0c;比如鼠标…...

一个基于.NET Core开源、跨平台的仓储管理系统

前言 今天给大家推荐一个基于.NET Core开源、跨平台的仓储管理系统&#xff0c;数据库支持MSSQL/MySQL&#xff1a;ZEQP.WMS。 仓储管理系统介绍 仓储管理系统&#xff08;Warehouse Management System&#xff0c;WMS&#xff09;是一种用于管理和控制仓库操作的软件系统&…...

主机安全-WindowsLinux的SSH安全加固

信息安全相关 - 建设篇 第三章 主机安全-Linux的SSH安全加固 信息安全相关 - 建设篇系列文章回顾下章内容主机安全-Linux的SSH安全加固前言Windows openssh相关命令&#xff0c;安装openssh获取openssh命令Windows openssl相关命令&#xff0c;安装Git获取openssl命令修复 CVE-…...

pcie-2-rj45速度优化

背景: 目前用iperf3打流传输速率达不到要求,千兆实际要求跑到800M以上: 优化方案: 1.优化defconfig: 首先编译user版本验证看是否正常 debug版本关闭CONFIG_SLUB_DEBUG_ON宏控。 2.找FAE ,通过更换驱动,或者更新驱动来优化 3.绑定大核: 以8125网卡为例,udp…...

AWVS 使用方法归纳

1.首先确认扫描的网站&#xff0c;以本地的dvwa为例 2.在awvs中添加目标 输入的地址可以是域名也可以是ip&#xff0c;只要本机可以在浏览器访问的域名或ip即可 添加地址及描述之后&#xff0c;点击保存&#xff0c;就会展现出目标设置选项 business criticality译为业务关键…...

数据库基础入门 — SQL运算符

我是南城余&#xff01;阿里云开发者平台专家博士证书获得者&#xff01; 欢迎关注我的博客&#xff01;一同成长&#xff01; 一名从事运维开发的worker&#xff0c;记录分享学习。 专注于AI&#xff0c;运维开发&#xff0c;windows Linux 系统领域的分享&#xff01; 本…...

SELinux零知识学习二十九、SELinux策略语言之类型强制(14)

接前一篇文章:SELinux零知识学习二十八、SELinux策略语言之类型强制(13) 二、SELinux策略语言之类型强制 4. 类型规则 类型规则在创建客体或在运行过程中重新标记时指定其默认类型。在策略语言中定义了两个类型规则: type_transtition在域转换过程中标记行为发生时以及创…...

Git控制指令

git status查看当前本地分支的修改状态 git diff 文件路径 查看具体文件的修改内容 git log打印用户信息 git remote -v查看远程地址 git checkout -- *还原被删除的文件 git rm -r --force .删除本地所有文件 git commit -m "Remove all files from repositor…...

C#中警告CA1050、CA1821、CA1822、CA1859、CA2249及处理

目录 一、CA1050警告及处理 1.如何解决冲突&#xff1a; 2.何时禁止显示警告&#xff1a; 二、CA1821警告及处理 三、CA1822警告及处理 四、CA1859警告及处理 1.警告解决之前 2.警告解决之后 3.解决办法 1.警告解决之前 2.警告解决之后 3.解决办法 五、CA2249警告…...

【Cmake】Cmake基础学习

CMake学习 一、基础学习 1. 利用Cmake进行单个源代码构建可执行文件 (1)基础命令 最基本的 CMake项目是由单个源代码文件构建的可执行文件。对于这样的简单项目,只需要一个包含三个命令的 CMakeLists.txt 文件。 注意: 虽然 CMake 支持大写、小写和混合大小写命令,但是…...

网络六边形受到攻击

大家读完觉得有帮助记得关注和点赞&#xff01;&#xff01;&#xff01; 抽象 现代智能交通系统 &#xff08;ITS&#xff09; 的一个关键要求是能够以安全、可靠和匿名的方式从互联车辆和移动设备收集地理参考数据。Nexagon 协议建立在 IETF 定位器/ID 分离协议 &#xff08;…...

ServerTrust 并非唯一

NSURLAuthenticationMethodServerTrust 只是 authenticationMethod 的冰山一角 要理解 NSURLAuthenticationMethodServerTrust, 首先要明白它只是 authenticationMethod 的选项之一, 并非唯一 1 先厘清概念 点说明authenticationMethodURLAuthenticationChallenge.protectionS…...

搭建DNS域名解析服务器(正向解析资源文件)

正向解析资源文件 1&#xff09;准备工作 服务端及客户端都关闭安全软件 [rootlocalhost ~]# systemctl stop firewalld [rootlocalhost ~]# setenforce 0 2&#xff09;服务端安装软件&#xff1a;bind 1.配置yum源 [rootlocalhost ~]# cat /etc/yum.repos.d/base.repo [Base…...

mac 安装homebrew (nvm 及git)

mac 安装nvm 及git 万恶之源 mac 安装这些东西离不开Xcode。及homebrew 一、先说安装git步骤 通用&#xff1a; 方法一&#xff1a;使用 Homebrew 安装 Git&#xff08;推荐&#xff09; 步骤如下&#xff1a;打开终端&#xff08;Terminal.app&#xff09; 1.安装 Homebrew…...

Mac flutter环境搭建

一、下载flutter sdk 制作 Android 应用 | Flutter 中文文档 - Flutter 中文开发者网站 - Flutter 1、查看mac电脑处理器选择sdk 2、解压 unzip ~/Downloads/flutter_macos_arm64_3.32.2-stable.zip \ -d ~/development/ 3、添加环境变量 命令行打开配置环境变量文件 ope…...

SpringCloud优势

目录 完善的微服务支持 高可用性和容错性 灵活的配置管理 强大的服务网关 分布式追踪能力 丰富的社区生态 易于与其他技术栈集成 完善的微服务支持 Spring Cloud 提供了一整套工具和组件来支持微服务架构的开发,包括服务注册与发现、负载均衡、断路器、配置管理等功能…...

Vue3 hooks

export default function(){ let name; function getName(){ return name; } return {name,getName} } use it ----------------------------------------------- import useName from hooks/useName const {name,getName} useName(); 这段代码展示了一个自定义 Vue3钩…...

深度学习学习率优化方法——pytorch中各类warm up策略

warm-up具体原理以及为什么这么做在之前的博客有介绍&#xff0c;这里直接介绍如何直接使用pytorch中的warm-up策略&#xff0c;在pytorch中对于warm-up所有支持的方法都有描述&#xff0c;可以直接阅读1。 深度学习中各类学习率优化方法(AdaGrad/RMSprop/Adam/Warm-UP)原理及其…...

Python编码格式化之PEP8编码规范

文章目录 概要PEP8编码风格py文本组织规范命名规范编码风格 PEP8编码检查工具pylintflake8PyCharm中配置检查工具 PEP8编码格式化工具blackautopep8PyCharm配置格式化工具本地git配置hook 总结 概要 在Python项目开发过程中&#xff0c;代码的可读性和一致性对于项目的长期维护…...

PC与Windows远程连接与串流:方案简介(ZeroTier + Parsec、Moonlight + Sunshine、网易UU远程)

简介 在远程办公、云游戏、家用 NAS 串流、图形远程渲染等需求增长的背景下&#xff0c;越来越多用户开始寻找低延迟、高画质、跨网络可用的远程连接方案。今天这篇文章将深度分析三种目前在玩家圈和远程办公中都非常流行的组合方案&#xff1a; &#x1f7e2; ZeroTier Pars…...