看了这篇关于栈的文章就可以去给异性讲了


🎈前言

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站https://www.captainbed.cn/mldxtj
这篇文章男女通用,看懂了就去给异性讲吧。

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

🎈什么是栈

可以把栈当成是弹夹,只能从栈顶放入数据和取出数据,而且每次取出的时候都是从栈顶取,先进后出后进先出

🎈动图演示栈

🎈链表实现栈:

img

🎈线性表实现栈:

img img

🎈栈的具体实现

🎈stak.h 定义函数,结构体,所需要的头文件

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int STDataType;

typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
} ST;
	
void StackInit(ST*ps);//初始化栈
void StackPush(ST* ps, STDataType x);//入栈
void StackPop(ST*ps);//出栈
int  StackSize(ST*ps);//栈中数据的数量
bool StackEmpty(ST* ps);//判断栈是否为空
STDataType StackTop(ST* ps);//栈顶元素
void Destroy(ST* ps);//销毁栈

🎈stack.c实现函数

🎈 StackInit(ST*ps);//初始化栈

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

🎈StackPush(ST* ps, STDataType x);//入栈

void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : (ps->capacity * 2);
		ps->a = (STDataType*)realloc(ps->a, sizeof(STDataType) * newCapacity);
		if (ps->a == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->capacity = newCapacity;
	}

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

🎈StackPop(ST*ps);//出栈

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

🎈StackSize(ST*ps);//栈中数据的数量

int  StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

🎈StackEmpty(ST* ps);//判断栈是否为空

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

	return (ps->top == 0);
}

🎈StackTop(ST* ps);//栈顶元素

STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}

🎈Destroy(ST* ps);//销毁栈

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

🎈stack.c完整代码

#define _CRT_SECURE_NO_WARNINGS 1
#include"stack.h"
void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : (ps->capacity * 2);
		ps->a = (STDataType*)realloc(ps->a, sizeof(STDataType) * newCapacity);
		if (ps->a == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->capacity = newCapacity;
	}

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

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

int  StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

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

	return (ps->top == 0);
}

STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}

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

🎈结语

都看到这里了点个赞呗
在这里插入图片描述

在这里插入图片描述

  • 19
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值