数据结构之栈

首先宏观上是在干什么?

概念

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

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

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

在这里插入图片描述

栈底层结构选型

核心原则后来者居上

其次有哪些假设?

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

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

在这里插入图片描述

栈的实现

入栈

在这里插入图片描述

代码如下:

// ⼊栈
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;
}

代码如下:

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

#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

#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

代码如下:
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

相关推荐

  1. 数据结构

    2024-07-23 07:30:01       40 阅读
  2. 数据结构

    2024-07-23 07:30:01       23 阅读
  3. 03 数据结构

    2024-07-23 07:30:01       35 阅读
  4. 数据结构(LIFO)

    2024-07-23 07:30:01       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-23 07:30:01       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-23 07:30:01       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-23 07:30:01       45 阅读
  4. Python语言-面向对象

    2024-07-23 07:30:01       55 阅读

热门阅读

  1. 驱动开发系列07 - 驱动程序如何分配内存

    2024-07-23 07:30:01       17 阅读
  2. C++链表

    C++链表

    2024-07-23 07:30:01      14 阅读
  3. “微软蓝屏”事件暴露的网络安全问题

    2024-07-23 07:30:01       18 阅读
  4. 【SpringBoot】第3章 系统配置之日志配置

    2024-07-23 07:30:01       15 阅读
  5. Hopfield 网络简介

    2024-07-23 07:30:01       21 阅读
  6. 为什么要学习网安技术?

    2024-07-23 07:30:01       12 阅读
  7. R语言读取txt和csv只有1列时

    2024-07-23 07:30:01       17 阅读
  8. 2024 7.15~7.21 周报

    2024-07-23 07:30:01       15 阅读
  9. 力扣 102题 二叉树的层次遍历 记录

    2024-07-23 07:30:01       15 阅读
  10. 【通俗理解】对数边缘似然:公式与应用

    2024-07-23 07:30:01       15 阅读
  11. mariadb安装centos再次踩坑

    2024-07-23 07:30:01       15 阅读
  12. PostgreSQL 8.4 ROW_NUMBER()函数

    2024-07-23 07:30:01       14 阅读
  13. 通过队列名寻找某队列-linux

    2024-07-23 07:30:01       9 阅读