📖 前言:在我们的软件应用、上网冲浪时,会发现都不约而同的有一个后退/撤销键。
单击它就可以按照操作顺序的逆序回退,像时光倒流一样。
那么这是一种怎么的实现方式呢?接下来就引出我们本期要讨论的一种数据结构——栈
🎓 作者:HinsCoder
📦 作者的GitHub:代码仓库
📌 往期文章&专栏推荐:
目录📌
🕒 1. 什么是栈
-
栈是一种特殊的
线性表
-
只允许在固定的
一端
进行插入
和删除
元素操作 -
栈中的数据遵守
LIFO
(Last In First Out)【即元素的后进先出
】的原则 -
进行数据
插入
和删除
操作的一端称为栈顶
,另一端称为栈底
。
压栈/入栈(push):栈的插入
操作
出栈(pop):栈的删除
操作
🕒 2. 栈的实现
我们可以通过三种我们学过的数据结构去实现它:
- 单链表:可以通过单链表去
头插
、头删
去模拟实现栈数据的入栈
、出栈
- 双向链表:这个结构去实现栈的操作非常便捷,可以快速的找到
栈顶
和栈尾
,并对栈顶
实现插入
和删除
数据的操作 - 顺序表:顺序表的内存空间连续。
尾插
、尾删
效率较高,时间复杂度是O(1)
, 支持随机访问,是实现栈结构的不二之选。
接下来我们开始实现栈的接口
🕘 2.1 定义顺序栈
typedef int STDataType;
typedef struct Stack
{
STDataType* a; //动态开辟数组
int top; //栈顶
int capacity; //容量
}ST;
🕘 2.2 初始化顺序栈
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
🕘 2.3 销毁顺序栈
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
🕘 2.4 检查容量
void StackCheckCapacity(ST* ps)
{
if (ps->top == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
exit(-1);
}
ps->a = tmp;
ps->capacity = newCapacity;
}
}
🕘 2.5 栈的判空
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
⚡ 注意:有些同学可能会喜欢直接在main函数里判空操作,比如if (st.top > 0 )
这样判断,其实在实际使用中是不太合适的。因为有的栈是从0开始,有的栈是从-1开始,必须去看底层设计才能判断。因此记住一个原则:
数据结构建议不要直接访问结构数据,一定要通过函数接口访问
正确的方式:if (!StackEmpty(&st))
在写底层的时候,要保证无论main函数怎么操作(比如:StackPush(&st, 1);
),都能正确实现,不用使用者操心,这种思路叫解耦,最终目标是低耦合 高内聚
🕘 2.6 入栈(尾插)
void StackPush(ST* ps, STDataType x)
{
assert(ps);
// 检查容量
StackCheckCapacity(ps);
ps->a[ps->top] = x;
ps->top++;
}
🕘 2.7 出栈(尾删)
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
🕘 2.8 取栈顶元素
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
🕘 2.9 获取元素数量
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
⌛ 总结:本期关于栈的实现其实没有什么新的内容,如果有哪里不清楚的都属于顺序表相关知识没掌握好,移步 🔎【数据结构详解】——线性表之顺序表(多图详解)哟~~
🕒 3. 完整源码
// Stack.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#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 StackInit(ST* ps);
void StackDestroy(ST* ps);
void StackPush(ST* ps, STDataType x);
void StackPop(ST* ps);
STDataType StackTop(ST* ps);
bool StackEmpty(ST* ps);
int StackSize(ST* ps);
// Stack.c
#include"Stack.h"
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
void StackCheckCapacity(ST* ps)
{
if (ps->top == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
exit(-1);
}
ps->a = tmp;
ps->capacity = newCapacity;
}
}
void StackPush(ST* ps, STDataType x)
{
assert(ps);
// 检查容量
StackCheckCapacity(ps);
ps->a[ps->top] = x;
ps->top++;
}
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
// test.c
#include "Stack.h"
void TestStack()
{
ST st;
StackInit(&st);
StackPush(&st, 1);
StackPush(&st, 2);
StackPush(&st, 3);
printf("%d ", StackTop(&st));
StackPop(&st);
printf("%d ", StackTop(&st));
StackPop(&st);
StackPush(&st, 4);
StackPush(&st, 5);
while (!StackEmpty(&st))
{
printf("%d ", StackTop(&st));
StackPop(&st);
}
printf("\n");
}
int main()
{
TestStack();
return 0;
}
OK,以上就是本期知识点“顺序栈”的知识啦~~ ,感谢友友们的阅读。后续还会继续更新,欢迎持续关注哟📌~
🎉如果觉得收获满满,可以点点赞👍支持一下哟~