文章目录
🎈前言
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站https://www.captainbed.cn/mldxtj
这篇文章男女通用,看懂了就去给异性讲吧。
🎈什么是栈
可以把栈当成是弹夹,只能从栈顶放入数据和取出数据,而且每次取出的时候都是从栈顶取,先进后出后进先出
🎈动图演示栈
🎈链表实现栈:
🎈线性表实现栈:
![img](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%253A%252F%252Fupload-images.jianshu.io%252Fupload_images%252F20750832-366085c55e69d206.gif%26refer%3Dhttp%253A%252F%252Fupload-images.jianshu.io%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Dauto%3Fsec%3D1650785101%26t%3D7350373736d054e3f81d1c5345493b44&pos_id=img-LiZQpYIL-1698411798346%29)
![img](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%253A%252F%252Fimg2020.cnblogs.com%252Fblog%252F1544400%252F202109%252F1544400-20210922221410637-1311459640.gif%26refer%3Dhttp%253A%252F%252Fimg2020.cnblogs.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Dauto%3Fsec%3D1650785101%26t%3Dca3ef56e55f9d5ec5bf856979a3b787f&pos_id=img-pr9Rn3Uc-1698411799078%29)
🎈栈的具体实现
🎈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;
}
🎈结语
都看到这里了点个赞呗