
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。 压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。 出栈:栈的删除操作叫做出栈。出数据也在栈顶
总的来说,栈是一种简单但功能强大的数据结构,它的后进先出特性使其在许多领域都有着重要的应用。


栈结构通常是用顺序表来实现的,如果学会了顺序表和链表再来实现栈结构就行显得简单的多。
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
STDataType* _a; //栈空间
int _top; // 栈顶
int _capacity; // 容量
}Stack;
void StackInit(Stack* ps);// 初始化栈
void StackPush(Stack* ps, STDataType data);// 入栈
void StackPop(Stack* ps);// 出栈
STDataType StackTop(Stack* ps);// 获取栈顶元素
int StackSize(Stack* ps);// 获取栈中有效元素个数
int StackEmpty(Stack* ps);// 检测栈是否为空
void StackDestroy(Stack* ps);// 销毁栈 void StackInit(Stack* ps)
{
assert(ps);
ps->_a = NULL;
ps->_top = ps->_capacity = 0;
}void StackPush(Stack* ps, STDataType data)
{
assert(ps);
if (ps->_top == ps->_capacity)
{
int dt = ps->_capacity == 0 ? 4 : ps->_capacity * 2;
STDataType* pnew = (STDataType*)realloc(ps->_a, sizeof(STDataType) * dt);//申请栈空间
assert(pnew);
ps->_a = pnew;
ps->_capacity = dt;//更新空间大小
}
ps->_a[ps->_top] = data;
ps->_top++;
}void StackPop(Stack* ps)
{
assert(ps);
assert(ps->_top);
ps->_top--;
}STDataType StackTop(Stack* ps)
{
assert(ps);
assert(ps->_top);
return ps->_a[ps->_top];
}// 检测栈是否为空,如果为空返回0结果,如果不为空返回非零
int StackEmpty(Stack* ps)
{
assert(ps);
return ps->_top == 0 ? 1 : 0;
}void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->_a);
ps->_a = NULL;
ps->_top = ps->_capacity = 0;
}Stack.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
STDataType* _a; //栈空间
int _top; // 栈顶
int _capacity; // 容量
}Stack;
// 初始化栈
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType data);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);Stack.c
#include"Stack.h"
// 初始化栈
void StackInit(Stack* ps)
{
assert(ps);
ps->_a = NULL;
ps->_top = ps->_capacity = 0;
}
// 入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
if (ps->_top == ps->_capacity)
{
int dt = ps->_capacity == 0 ? 4 : ps->_capacity * 2;
STDataType* pnew = (STDataType*)realloc(ps->_a, sizeof(STDataType) * dt);//申请栈空间
assert(pnew);
ps->_a = pnew;
ps->_capacity = dt;//更新空间大小
}
ps->_a[ps->_top] = data;
ps->_top++;
}
// 出栈
void StackPop(Stack* ps)
{
assert(ps);
assert(ps->_top);
ps->_top--;
}
// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(ps->_top);
return ps->_a[ps->_top];
}
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->_top;
}
// 检测栈是否为空,如果为空返回0结果,如果不为空返回非零
int StackEmpty(Stack* ps)
{
assert(ps);
return ps->_top == 0 ? 1 : 0;
}
// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->_a);
ps->_a = NULL;
ps->_top = ps->_capacity = 0;
}test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
int main()
{
Stack pst;
Stack* pr = &pst;
// 初始化栈
StackInit(pr);
StackPush(pr, 1);
StackPush(pr, 2);
StackPush(pr, 3);
StackPush(pr, 4);
StackPush(pr, 5);
StackPush(pr, 6);
StackPop(pr);
while (!StackEmpty(pr))
{
printf("%d ", StackTop(pr));
}
printf("\n%d", StackSize(pr));
StackDestroy(pr);
return 0;
}