【数据结构详解】——线性表之顺序栈

📖 前言:在我们的软件应用、上网冲浪时,会发现都不约而同的有一个后退/撤销键。在这里插入图片描述
单击它就可以按照操作顺序的逆序回退,像时光倒流一样。
那么这是一种怎么的实现方式呢?接下来就引出我们本期要讨论的一种数据结构——栈


🎓 作者:HinsCoder
📦 作者的GitHub:代码仓库
📌 往期文章&专栏推荐:

  1. 【C语言详解】专栏
  2. 【C语言详解】——函数栈帧的创建与销毁(动图详解)
  3. 【数据结构详解】——线性表之顺序表(多图详解)
  4. 【数据结构详解】——线性表之单链表(动图详解)
  5. 【数据结构详解】——线性表之双向链表(动图详解)

🕒 1. 什么是栈

  • 栈是一种特殊的线性表

  • 只允许在固定的一端进行插入删除元素操作

  • 栈中的数据遵守LIFO(Last In First Out)【即元素的后进先出】的原则

  • 进行数据插入删除操作的一端称为栈顶,另一端称为栈底

请添加图片描述

压栈/入栈(push):栈的插入操作
出栈(pop):栈的删除操作

🕒 2. 栈的实现

我们可以通过三种我们学过的数据结构去实现它:

  1. 单链表:可以通过单链表去头插头删去模拟实现栈数据的入栈出栈
  2. 双向链表:这个结构去实现栈的操作非常便捷,可以快速的找到栈顶栈尾,并对栈顶实现插入删除数据的操作
  3. 顺序表:顺序表的内存空间连续。 尾插尾删效率较高,时间复杂度是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,以上就是本期知识点“顺序栈”的知识啦~~ ,感谢友友们的阅读。后续还会继续更新,欢迎持续关注哟📌~
🎉如果觉得收获满满,可以点点赞👍支持一下哟~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值