栈和队列的动态实现(C语言实现)

请添加图片描述

✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅
✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨
🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿
🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟
🌟🌟 追风赶月莫停留 🌟🌟
🍀🍀🍀🍀🍀🍀🍀🍀🍀🍀🍀🍀🍀🍀🍀🍀
🌟🌟 平芜尽处是春山🌟🌟
🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟
🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿🌿
✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨
✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅

🍋栈和队列

  • 🍑栈
    • 🍍栈的含义
    • 🍍栈的结构
    • 🍍栈的实现
      • 🍌栈的补充条件
      • 🍌初始化栈
      • 🍌入栈
      • 🍌出栈
      • 🍌获取栈顶元素
      • 🍌获取栈中有效元素的个数
      • 🍌检查栈是否为空
      • 🍌销毁栈
    • 🍍栈的整体代码的实现
  • 🍑队列
    • 🍍队列的含义
    • 🍍队列的结构
    • 🍍队列的实现
      • 🍌队列的补充条件
      • 🍌初始化队列
      • 🍌队尾入队列
      • 🍌队头出队列
      • 🍌获取队列头部元素
      • 🍌获取队列队尾元素
      • 🍌获取队列中有效元素个数
      • 🍌检测队列是否为空
      • 🍌 销毁队列
    • 🍍队列的整体代码的实现

🍑栈

🍍栈的含义

栈是一种特殊类型的线性表,它的特点是仅允许在其一端进行插入(压入)和删除(弹出)操作。这一端被称为栈顶,而相对的另一端则被称为栈底。栈通常遵循“后进先出”(LIFO)的原则,意味着新加入的元素总是位于栈顶,而要访问或移除的元素必须从前部移除。

🍍栈的结构

在这里插入图片描述

栈的结构就是如图片中的形容的类似,满足先进后出

🍍栈的实现

🍌栈的补充条件

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int STDatetype;//方便后续数据不只是int,也可以方便的换其他类型

typedef struct Stack//利用结构体来定义栈
{
	STDatetype* a;
	int top; 
	int capacity;
}ST;


int main()
{
	ST st;
	STInia(&st);
	STPush(&st, 1);
	STPush(&st, 2);
	STPush(&st, 3);
	STPush(&st, 4);
	STPush(&st, 5);

	while (!STEmpty(&st))
	{
		printf("%d ", STTop(&st));
		STPop(&st);
	}

	printf("\n");

	STDestroy(&st);

	return 0;
}

🍌初始化栈

void STInia(ST* ps)
{
	assert(ps);
	ps->top = ps->capacity = 0;
	ps->a = NULL;
}

🍌入栈

void STPush(ST* ps, STDatetype x)
{
	assert(ps);

	if (ps->top == ps->capacity)
	{
		int newcapacity = 0;
		if (ps->capacity == 0)
		{
			newcapacity = 2;
		}
		else
		{
			newcapacity = newcapacity * 2;
		}

		STDatetype* tem = (STDatetype*)realloc(ps->a, sizeof(STDatetype) * newcapacity);
		if (tem == NULL)
		{
			perror("realloc  fail");
			exit(-1);
		}

		ps->a = tem;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

(1)之所以在这里不用malloc创建空间,是因为后面还要用realloc进行扩容,所以就直接用realloc进行空间的创建。
(2)在ps->top和ps->capacity相等时进行扩容,在这里进行了判断,有两种情况。第一种是ps->capacity等于0,那就得先创建空间。第二种是ps->capacity不为0,就直接扩容为原来2倍的空间

🍌出栈

void STPop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);//判断数据是否为空

	(ps->top)--;

}

🍌获取栈顶元素

STDatetype STTop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);//判断是否为空

	return ps->a[ps->top - 1];
}

🍌获取栈中有效元素的个数

int STSize(ST* ps)
{
	assert(ps);

	return ps->top;
}

在栈中数据个数其实就是ps->top

🍌检查栈是否为空

bool STEmpty(ST* ps)
{
	assert(ps);

	return ps->top == 0;
}

如果为空就返回1,不为空就返回0

🍌销毁栈

void STDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

🍍栈的整体代码的实现

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int STDatetype;

typedef struct Stack
{
	STDatetype* a;
	int top; 
	int capacity;
}ST;

void STInia(ST* ps)
{
	assert(ps);
	ps->top = ps->capacity = 0;
	ps->a = NULL;
}

void STDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

void STPush(ST* ps, STDatetype x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newcapacity = 0;
		if (ps->capacity == 0)
		{
			newcapacity = 2;
		}
		else
		{
			newcapacity = ps->capacity * 2;
		}

		STDatetype* tem = (STDatetype*)realloc(ps->a, sizeof(STDatetype) * newcapacity);
		if (tem == NULL)
		{
			perror("realloc  fail");
			exit(-1);
		}

		ps->a = tem;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	(ps->top)++;
}


void STPop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);

	(ps->top)--;

}

int STSize(ST* ps)
{
	assert(ps);

	return ps->top;
}

bool STEmpty(ST* ps)
{
	assert(ps);

	return ps->top == 0;
}

STDatetype STTop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);

	return ps->a[ps->top - 1];
}


int main()
{
	ST st;
	STInia(&st);
	STPush(&st, 1);
	STPush(&st, 2);
	STPush(&st, 3);
	STPush(&st, 4);
	STPush(&st, 5);

	while (!STEmpty(&st))
	{
		printf("%d ", STTop(&st));
		STPop(&st);
	}

	printf("\n");

	STDestroy(&st);

	return 0;
}

🍑队列

🍍队列的含义

队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。

🍍队列的结构

在这里插入图片描述

队列和栈有点类似,只不过栈是先进后出,而队列是先进先出

🍍队列的实现

🍌队列的补充条件


#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int QDatetype;
typedef struct QueueNode
{
	struct QueueNode* next;
	QDatetype date;
}QNode;

typedef struct Queue
{
	QNode* head;
	QNode* tail;
	int size;
}Que;
///
int main()
{
	Que qq;
	QueueInia(&qq);
	QueuePush(&qq, 1);
	QueuePush(&qq, 2);
	QueuePush(&qq, 3);
	QueuePush(&qq, 4);
	while (!QueueEmpty(&qq))
	{
		printf("%d ", QueueFront(&qq));
		QueuePop(&qq);
	}
	printf("\n");
	return 0;
}

在这个队列中,我是采用了单链表(单向不循环)的结构来实现队列,所以再这里要注意头可能是空指针的问题,在前面我介绍单链表的时候是利用二级指针解决这个问题,而在这里是采用了新的方法,也就是结构体指针,把头和尾重新用一个结构体来定义

🍌初始化队列

void QueueInia(Que* ps)
{
	assert(ps);
	ps->head = NULL;
	ps->tail = NULL;
	ps->size = 0;
}

🍌队尾入队列

void QueuePush(Que* ps, QDatetype x)
{
	assert(ps);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc  fail");
		exit(-1);
	}
	newnode->next = NULL;
	newnode->date = x;

	if (ps->tail == NULL)
	{
		ps->head = ps->tail = newnode;
	}
	else
	{
		ps->tail->next = newnode;
		ps->tail = newnode;
	}
	(ps->size)++;
}

🍌队头出队列

void QueuePop(Que* ps)
{
	assert(ps);
	assert(ps->size > 0);

	if (ps->head->next == NULL)
	{
		free(ps->head);
		ps->head = ps->tail = NULL;
	}
	else
	{
		QNode* cur = ps->head->next;
		free(ps->head);
		ps->head = cur;
	}
	(ps->size)--;
}

🍌获取队列头部元素

QDatetype QueueFront(Que* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));

	return ps->head->date;
}

🍌获取队列队尾元素

QDatetype QueueBake(Que* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));

	return ps->tail->date;
}

🍌获取队列中有效元素个数

int QueueSize(Que* ps)
{
	assert(ps);

	return ps->size;
}

🍌检测队列是否为空

bool QueueEmpty(Que* ps)
{
	assert(ps);
	return ps->head == NULL;
}

🍌 销毁队列

void QueueDestroy(Que* ps)
{
	assert(ps);

	QNode* cur = ps->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	ps->head = ps->tail = NULL;
	ps->size = 0;
}

🍍队列的整体代码的实现

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int QDatetype;
typedef struct QueueNode
{
	struct QueueNode* next;
	QDatetype date;
}QNode;

typedef struct Queue
{
	QNode* head;
	QNode* tail;
	int size;
}Que;

void QueueInia(Que* ps)
{
	assert(ps);
	ps->head = NULL;
	ps->tail = NULL;
	ps->size = 0;
}

bool QueueEmpty(Que* ps)
{
	assert(ps);
	return ps->head == NULL;
}


void QueuePush(Que* ps, QDatetype x)
{
	assert(ps);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc  fail");
		exit(-1);
	}
	newnode->next = NULL;
	newnode->date = x;

	if (ps->tail == NULL)
	{
		ps->head = ps->tail = newnode;
	}
	else
	{
		ps->tail->next = newnode;
		ps->tail = newnode;
	}
	(ps->size)++;
}

void QueuePop(Que* ps)
{
	assert(ps);
	assert(ps->size > 0);

	if (ps->head->next == NULL)
	{
		free(ps->head);
		ps->head = ps->tail = NULL;
	}
	else
	{
		QNode* cur = ps->head->next;
		free(ps->head);
		ps->head = cur;
	}
	(ps->size)--;
}

QDatetype QueueFront(Que* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));

	return ps->head->date;
}

QDatetype QueueBake(Que* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));

	return ps->tail->date;
}

int QueueSize(Que* ps)
{
	assert(ps);

	return ps->size;
}

void QueueDestroy(Que* ps)
{
	assert(ps);

	QNode* cur = ps->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	ps->head = ps->tail = NULL;
	ps->size = 0;
}


int main()
{
	Que qq;
	QueueInia(&qq);
	QueuePush(&qq, 1);
	QueuePush(&qq, 2);
	QueuePush(&qq, 3);
	QueuePush(&qq, 4);
	while (!QueueEmpty(&qq))
	{
		printf("%d ", QueueFront(&qq));
		QueuePop(&qq);
	}
	printf("\n");
	return 0;
}

队列和栈我就不详细介绍了,如果有需要可以看我写的这篇博客:单链表

本期的内容就结束了,文章有错误的地方欢迎大家指正!!!

请添加图片描述

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

探索数据可视化:Python 库 Matplotlib

探索数据可视化&#xff1a;Python 库 Matplotlib 在数据科学和机器学习的领域中&#xff0c;数据可视化是一种强大的工具&#xff0c;它能够将复杂的数据转化为易于理解和解释的图形形式。在 Python 的丰富生态系统中&#xff0c;Matplotlib 库被认为是最流行和最强大的数据可…

蓝桥杯备赛 week 4 —— DP 背包问题

目录 &#x1f308;前言&#x1f308;&#xff1a; &#x1f4c1; 01背包问题 分析&#xff1a; dp数组求解&#xff1a; 优化&#xff1a;滚动数组&#xff1a; &#x1f4c1; 完全背包问题 &#x1f4c1; 总结 &#x1f308;前言&#x1f308;&#xff1a; 这篇文章主…

谷歌seo服务费一般是多少?

谷歌SEO服务费是根据多种因素变化的&#xff0c;包括所需的服务范围、项目的规模和复杂性、所在地区的市场竞争情况以及您选择的SEO服务提供商 seo不应该仅仅只是提供技术服务&#xff0c;根据不同的服务内容可以分为不同的收费方式&#xff0c;比如收取固定费用&#xff0c;但…

chroot: failed to run command ‘/bin/bash’: No such file or directory

1. 问题描述及原因分析 在busybox的环境下&#xff0c;执行 cd rootfs chroot .报错如下&#xff1a; chroot: failed to run command ‘/bin/bash’: No such file or directory根据报错应该rootfs文件系统中缺少/bin/bash&#xff0c;进入查看确实默认是sh&#xff0c;换成…

谷歌seo服务商如何选择?

选择谷歌SEO服务商时&#xff0c;要考虑他们的经验、专业知识、成功案例、透明度、合规性、定制能力、时间线、客户支持、沟通以及是否能够建立长期合作关系。综合评估这些因素&#xff0c;确保找到一个可信赖的合作伙伴&#xff0c;能够帮助您提升网站在谷歌搜索中的表现&…

ctfshow web75

开启环境: 先直接用伪协议获取 flag 位置 c?><?php $anew DirectoryIterator("glob:///*"); foreach($a as $f) {echo($f->__toString(). );} exit(0); ?> ctry {$dbh new PDO(mysql:hostlocalhost;dbnamectftraining, root, root);foreach($dbh-&g…

Jmeter实现造10个账户、单元数据

今天简单介绍Jemeter的入门,Jmeter 的安装这边就跳过,直接讲述如何使用JMETER,如何运用Jmeter进行测试。Jmeter实现造10个账户、单元数据,之后大数据量批量造数据以此类推。 1.下载jmeter软件 2.安装jmeter软件 3.运行\bin\jmeter.bat批处理文件 4.选择脚本文件 5.…

旧物回收小程序开发的价值与前景

在当今社会&#xff0c;随着科技的进步和人们生活方式的改变&#xff0c;物品的更新换代速度越来越快&#xff0c;这导致大量的旧物被闲置或丢弃。然而&#xff0c;这些旧物中很多仍有再利用的价值。为了更好地利用这些资源&#xff0c;旧物回收小程序的开发显得尤为重要。 一…

单片机学习笔记---矩阵键盘密码锁

目录 一&#xff0c;设置密码按键 1.设置密码区域 2.设置输入的数字左移 3.设置记录按键的次数 二&#xff0c;设置确认键 1.密码正确时显示OK 2.密码错误时显示ERR 3.密码错误恢复初始状态重输 三&#xff0c;设置取消键 学了这么久&#xff0c;迫不及待想要做一个密…

Apipost-cli、Jenkins持续集成配置

安装 Apipost-cli npm install -g apipost-cli 运行脚本 安装好Apipost-cli后&#xff0c;在命令行输入生成的命令&#xff0c;即可执行测试用例&#xff0c;运行完成后会展示测试进度并生成测试报告。 Jenkins配置 Apipost cli基于Node js运行 需要在jenkins上配置NodeJs依…

深入浅出理解目标检测的非极大值抑制(NMS)

一、参考资料 物体检测中常用的几个概念迁移学习、IOU、NMS理解 目标定位和检测系列&#xff08;3&#xff09;&#xff1a;交并比&#xff08;IOU&#xff09;和非极大值抑制&#xff08;NMS&#xff09;的python实现 Pytorch&#xff1a;目标检测网络-非极大值抑制(NMS) …

眼底增强型疾病感知蒸馏模型 FDDM:无需配对,fundus 指导 OCT 图

眼底增强型疾病感知蒸馏模型 FDDM&#xff1a;fundus 指导 OCT 图 核心思想设计思路训练和推理 效果总结子问题: 疾病特定特征的提取与蒸馏子问题: 类间关系的理解与建模 核心思想 论文&#xff1a;https://arxiv.org/pdf/2308.00291.pdf 代码&#xff1a;https://github.com…

文心一言情感关怀之旅

【AGIFoundathon】文心一言情感关怀之旅,让我们一起来体验吧! 上传一张照片,用ernie-bot生成专属于你的小故事! 此项目主要使用clip_interrogator获取图片的关键信息,然后将此关键信息用百度翻译API翻译成中文后,使用封装了⼀⾔API的Ernie Bot SDK(ernie-bot)生成故事…

利用aiohttp异步爬虫实现网站数据高效抓取

前言 大数据时代&#xff0c;网站数据的高效抓取对于众多应用程序和服务来说至关重要。传统的同步爬虫技术在面对大规模数据抓取时往往效率低下&#xff0c;而异步爬虫技术的出现为解决这一问题提供了新的思路。本文将介绍如何利用aiohttp异步爬虫技术实现网站数据抓取&#x…

学校“数据结构”课程Project—扩展功能(自主设计)

目录 一、设想功能描述 想法缘起 目标功能 二、问题抽象 三、算法设计和优化 1. 易想的朴素搜索 / dp 搜索想法 动态规划&#xff08;dp&#xff09;想法 2. 思考与优化 四、算法实现 五、结果示例 附&#xff1a;使用的地图API 一、设想功能描述 想法缘起 OSM 导出…

【昕宝爸爸小模块】什么是POI,为什么它会导致内存溢出?

➡️博客首页 https://blog.csdn.net/Java_Yangxiaoyuan 欢迎优秀的你&#x1f44d;点赞、&#x1f5c2;️收藏、加❤️关注哦。 本文章CSDN首发&#xff0c;欢迎转载&#xff0c;要注明出处哦&#xff01; 先感谢优秀的你能认真的看完本文&…

缓存问题 | 缓存穿透,缓存击穿,缓存雪崩

缓存穿透 关键字&#xff1a;强调缓存和数据库都没有数据并发访问 缓存穿透是指数据库和缓存都没有的数据&#xff0c;每次都要经过缓存去访问数据库&#xff0c;大量的请求有可能导致DB宕机。 应对策略&#xff1a; 使用布隆过滤器&#xff08;Bloom Filter&#xff09;&am…

react中优化类名写法(类似与vue的动态class对象方式)

安装和引入方式 npm install classnamesimport classNames form classsnames//render 方法中&#xff0c;需要动态className的地方直接参照上图使用

基于 java+springboot+mybatis电影售票网站管理系统前台+后台设计和实现

基于 javaspringbootmybatis电影售票网站管理系统前台后台设计和实现 &#x1f345; 作者主页 央顺技术团队 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; &#x1f345; 文末获取源码联系方式 &#x1f4dd; &#x1f345; 查看下方微信号获取联系方式 承…

数学建模-------误差来源以及误差分析

绝对误差&#xff1a;精确值-近似值&#xff1b; 举个例子&#xff1a;从A到B&#xff0c;应该有73千米&#xff0c;但是我们近似成了70千米&#xff1b;从C到D&#xff0c;应该是1373千米&#xff0c;我们近似成了1370千米&#xff0c;如果使用绝对误差&#xff0c;结果都是3…
最新文章