数据结构之栈

首先宏观上是在干什么?

概念

栈:⼀种特殊的线性表,其只允许在固定的⼀端进⾏插⼊和删除元素操作。进⾏数据插⼊和删除操作 的⼀端称为栈顶,另⼀端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。

压栈:栈的插⼊操作叫做进栈/压栈/⼊栈,⼊数据在栈顶。

出栈 :栈的删除操作叫做出栈。出数据也在栈顶。

栈底层结构选型

核心原则 :后来者居上

其次有哪些假设?

栈的实现可以用哪个数据结构实现?

栈的实现⼀般可以使⽤数组或者链表实现,相对⽽⾔数组的结构实现更优⼀些。因为数组在尾上插⼊ 数据的代价⽐较⼩。

栈的实现

入栈

代码如下:

C 复制代码
// ⼊栈
void STPush(ST* ps, STDataType x)
{
	assert(ps);
	//1.判断空间是否足够
	if (ps->capacity == ps->top)
	{
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
	//空间足够
	ps->arr[ps->top++] = x;
}

和写顺序表进行插入数据的操作非常的相似

出栈

如果栈为空,不可以出数据,所以要先判断栈是否为空

代码如下:

c 复制代码
/判断栈是否为空
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

根据栈的性质,我们可以返回栈顶的方式来判断

C 复制代码
//出栈
void STPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	--ps->top;
}

代码如下:

Stack.h

复制代码
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include<assert.h>
#include<stdbool.h>
//定义栈的结构
typedef int STDataType;
 typedef struct Stack
{
	STDataType* arr;
	int capacity;//栈的空间大小
	int top;//栈顶
}ST;
// 初始化栈
void STInit(ST* ps);
// 销毁栈
void STDestroy(ST* ps);
//栈顶--入数据 出数据
// ⼊栈
void STPush(ST* ps, STDataType x);
//出栈
void STPop(ST* ps);
//判断栈是否为空
bool StackEmpty(ST* ps);
//获取栈中有效元素个数
int STSize(ST* ps);

Stack.c

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"
// 初始化栈
void STInit(ST* ps) {
	assert(ps);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}
// 销毁栈
void STDestroy(ST* ps)
{
	assert(ps);
	if (ps->arr)
		free(ps->arr);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}
// ⼊栈
void STPush(ST* ps, STDataType x)
{
	assert(ps);
	//1.判断空间是否足够
	if (ps->capacity == ps->top)
	{
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
	//空间足够
	ps->arr[ps->top++] = x;
}
//判断栈是否为空
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}
//出栈
void STPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	--ps->top;
}
//获取栈中有效元素个数
int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

Test.c

C 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "stack.h"
void STTest()
{
	ST st;
	STInit(&st);
	STDestroy(&st);
	STPush(&st, 1);
	STPush(&st, 2);
	STPush(&st, 3);
	STPush(&st, 4);
	printf("size:%d\n", STSize(&st));
	//循环出栈,直至栈为空
	while (!StackEmpty(&st))
	{
		STDataType data = StackTop(&st);
		printf("%d", data);
		//出栈
		StackPop(&st);
	}
}
int main()
{
	STTest();
	return 0;
}

栈这个概念和我们之前学的概念有什么区别?

栈里面的数据不能被遍历,也不能被随机访问,这是和顺序表,链表不同的地方

有关栈的一道算法题

有效的括号

给定一个只包括 '('')''{''}''['']' 的字符串 s ,判断字符串是否有效。

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。
  3. 每个右括号都有一个对应的相同类型的左括号。

示例 1:

复制代码
输入:s = "()"
输出:true

示例 2:

复制代码
输入:s = "()[]{}"
输出:true

示例 3:

复制代码
输入:s = "(]"
输出:false

提示:

  • 1 <= s.length <= 104
  • s 仅由括号 '()[]{}' 组成

思路

借助数据结构中的栈来解决

先定义ps ,ps是{[()]}

若ps遍历到的符号是左括号,入栈,若ps遍历到的字符为右括号,取栈顶元素,与ps进行比较,栈顶元素匹配*ps,出栈,ps++;

栈顶元素不匹配*ps,返回false

代码如下:

相关推荐
元清加油2 分钟前
【Golang】:函数和包
服务器·开发语言·网络·后端·网络协议·golang
书弋江山40 分钟前
flutter 跨平台编码库 protobuf 工具使用
android·flutter
No0d1es42 分钟前
电子学会青少年软件编程(C/C++)5级等级考试真题试卷(2024年6月)
c语言·c++·算法·青少年编程·电子学会·五级
向日葵.2 小时前
fastdds.ignore_local_endpoints 属性
服务器·网络·php
大阳1233 小时前
线程(基本概念和相关命令)
开发语言·数据结构·经验分享·算法·线程·学习经验
来来走走3 小时前
Flutter开发 webview_flutter的基本使用
android·flutter
Jerry说前后端4 小时前
Android 组件封装实践:从解耦到架构演进
android·前端·架构
weixin_307779134 小时前
VS Code配置MinGW64编译GNU 科学库 (GSL)
开发语言·c++·vscode·算法
昵称为空C4 小时前
SpringBoot接口限流的常用方案
服务器·spring boot
学行库小秘4 小时前
ANN神经网络回归预测模型
人工智能·python·深度学习·神经网络·算法·机器学习·回归