数据结构之栈

首先宏观上是在干什么?

概念

栈:⼀种特殊的线性表,其只允许在固定的⼀端进⾏插⼊和删除元素操作。进⾏数据插⼊和删除操作 的⼀端称为栈顶,另⼀端称为栈底。栈中的数据元素遵守后进先出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

代码如下:

相关推荐
YGGP8 分钟前
【每日八股】Redis篇(二):数据结构
数据结构·数据库·redis
hmywillstronger24 分钟前
【Excel】【VBA】根据内容调整打印区域
android·excel
web_1553427465633 分钟前
性能巅峰对决:Rust vs C++ —— 速度、安全与权衡的艺术
c++·算法·rust
9毫米的幻想35 分钟前
【Linux系统】—— 冯诺依曼体系结构与操作系统初理解
linux·运维·服务器·c语言·c++
coooliang1 小时前
【Android】ViewPager的使用
android
USER_A0012 小时前
【C语言】第五期——函数
c语言
xvch3 小时前
Kotlin 2.1.0 入门教程(二十五)类型擦除
android·kotlin
勤奋的凯尔森同学6 小时前
webmin配置终端显示样式,模仿UbuntuDesktop终端
linux·运维·服务器·ubuntu·webmin
丁卯4047 小时前
Go语言中使用viper绑定结构体和yaml文件信息时,标签的使用
服务器·后端·golang
chengooooooo7 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库