目录
💓 博客主页:C-SDN花园GGbond
⏩ 文章专栏:探索数据结构与算法
一. 栈的定义
- 栈是一种后进先出(LIFO, Last In First Out)的数据结构。
- 栈是一种只能在一端进行插入和删除操作的线性表。
- 允许进行插入和删除操作的一端称为栈顶(top),另一端称为栈底(bottom)。
- 栈中没有元素时,称为空栈。
二. 栈的分类
当我们了解栈的定义之后,我们就能大概知晓其实现方式无非就是顺序表或者单链表。根据其实现方式,我们又能将栈分为顺序栈与链式栈。
- 因为单链表头插的效率O(1)明显比尾差O(N)更高,所以我们用单链表实现栈时最好以链表的头为栈顶。如果一定要以尾节点作为栈顶的话,最好以双向链表来实现。本章实现链表栈时以头节点作为栈顶。
三. 栈的功能
- 栈的初始化。
- 判断栈是否为空。。
- 返回栈顶元素。
- 返回栈的大小。
- 入栈与出栈。
- 打印栈的元素。
- 销毁栈。
四.实现栈的基本操作 (顺序栈)
为什么使用数组实现栈?
- 数组是一种线性数据结构,能够连续存储数据,且通过索引可以方便地访问任意位置的元素。
- 因为栈只在栈顶增删,所以基于数组实现,既避免了插入需要移动数据的劣势,又保持了数组访问数据的优势,可以实现高效的栈操作。
定义栈结构
- 指向数组的指针(动态开辟的空间)
- 标记栈顶位置的变量 top
- 标记栈的大小的变量 capacity
// 支持动态增长的栈
typedef int STDataType;//对数据类型重命名,方便后期修改类型
typedef struct Stack
{
STDataType* a;
int top; // 栈顶
int capacity; // 容量
}Stack;//定义结构同时重命名
注意:栈顶位置的指向
1.如果top指向栈顶元素,初始不能为0,应该指向-1
2.如果top初始为0,其应该指向栈顶元素的下一个元素
对应的判定栈满和栈空有所不同
top==0时:
top==-1时:
1.初始化
- 对形参判空
- 数组指针初始指向空
- top和capacity初始化为0(这里top指向的是栈顶元素的下一个位置)
// 初始化栈
void StackInit(Stack* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
2.销毁
- 对形参判空
- 释放数组空间
- 数组指针指向空
- top和capacity改为0
// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
3.入栈
1.判空
2.判断是否需要扩容(top和capacity相等)
3.扩容步骤: 空间二倍增长 ,更新数组指针和容量4.数据插入到top位置,top位置++
// 入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
//判断是否需要扩容
if (ps->top == ps->capacity)
{
int newcapa = ps->capacity == 0 ? 4 : 2 * (ps->capacity);
STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapa);
if (tmp == NULL)
{
perror("realloc\n");
exit(1);
}
ps->a = tmp;
ps->capacity = newcapa;
}
//确定空间足够之后再插入数据
ps->a[ps->top] = data;
ps->top++;
}
4.出栈
- 对形参判空
- 对栈判空
- top--
(该方法对于栈只存在一个元素的情况也可以正确处理)
// 出栈
void StackPop(Stack* ps)
{
assert(ps);
assert(ps->top);
ps->top--;
}
5.查看栈顶元素
- 对形参判空
- 对栈判空
- 返回top前一个位置的元素
// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(ps->top);
return ps->a[ps->top-1];
}
6.对栈判空
- 对形参判空
- 返回top==0的结果(因为这里top指向的是栈顶元素的下一个元素,所以栈空时top==0)
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps)
{
assert(ps);
return ps->top == 0;
}
7.获取有效数据个数
- 对形参判空
- 返回top (top对应的下标是栈顶的下一个元素,top就是元素的个数)
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
8.打印栈元素
void StackPrint(Stack* st)
{
assert(st);
assert(!StackEmpty(st));
for (int i = st->top - 1; i >= 0; i--)
{
printf("%d\n", st->a[i]);
}
}
测试入栈出栈可以使用 打印
五.使用数组实现栈的C语言代码
Stack.h
#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 newcapa = ps->capacity == 0 ? 4 : 2 * (ps->capacity);
STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapa);
if (tmp == NULL)
{
perror("realloc\n");
exit(1);
}
ps->a = tmp;
ps->capacity = newcapa;
}
//确定空间足够之后再插入数据
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-1];
}
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps)
{
assert(ps);
return ps->top == 0;
}
// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}