栈及栈的应用(有效的括号 力扣20)

栈的概念

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

画个图理解一下

咱们可以观察到,进出数据只在栈顶进行

实现栈用数组最方便

栈的实现

cpp 复制代码
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int STDataType;

typedef struct Stack {
	STDataType* a;
	int top;
	int capacity;
}ST;

void STInit(ST* pst);//初始化
void STDestroy(ST* pst);//销毁

void STPush(ST* pst, STDataType x);//入栈
void STPop(ST* pst);//出栈

STDataType STTop(ST* pst);//获取栈顶元素
int STSize(ST* pst);//获取数据个数

bool STEmpty(ST* pst);//判空
cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
void STInit(ST* pst)
{
	assert(pst);
	pst->a = NULL;
	pst->capacity = 0;
	pst->top = 0;
}

void STDestroy(ST* pst)
{
	assert(pst);
	free(pst->a);
	pst->a = NULL;
	pst->capacity = 0;
	pst->top = 0;
}

void STPush(ST* pst, STDataType x)
{
	assert(pst);
	if (pst->capacity == pst->top) {
		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* temp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));//扩容扩的是数组,不是结构体
		if (temp == NULL) {
			perror("realloc fail");
			return;
		}
		pst->a = temp;
		pst->capacity = newcapacity;

	}
	pst->a[pst->top] = x;
	pst->top++;
}
void STPop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);
	pst->top--;
}

STDataType STTop(ST* pst) {
	assert(pst);
	return  pst->a[pst->top - 1];
}

int STSize(ST* pst)
{
	assert(pst);
	return pst->top - 1;
}

bool STEmpty(ST* pst)
{
	assert(pst);
	return pst->top == 0;
}

栈的应用

let's看题目

然后是代码实现

cpp 复制代码
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef char STDataType;

typedef struct Stack {
	STDataType* a;
	int top;
	int capacity;
}ST;


void STInit(ST* pst)
{
	assert(pst);
	pst->a = NULL;
	pst->capacity = 0;
	pst->top = 0;
}

void STDestroy(ST* pst)
{
	assert(pst);
	free(pst->a);
	pst->a = NULL;
	pst->capacity = 0;
	pst->top = 0;
}

void STPush(ST* pst, STDataType x)
{
	assert(pst);
	if (pst->capacity == pst->top) {
		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		ST* temp = (ST*)realloc(pst->a, newcapacity * sizeof(STDataType));//扩容扩的是数组,不是结构体
		if (temp == NULL) {
			perror("realloc fail");
			return;
		}
		pst->a = temp;
		pst->capacity = newcapacity;

	}
	pst->a[pst->top] = x;
	pst->top++;
}
void STPop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);
	pst->top--;
}

STDataType STTop(ST* pst) {
	assert(pst);
	return  pst->a[pst->top - 1];
}

int STSize(ST* pst)
{
	assert(pst);
	return pst->top - 1;
}

bool STEmpty(ST* pst)
{
	assert(pst);
	return pst->top == 0;
}

bool isValid(char* s) {
    ST st;
    STInit(&st);
    while(*s){
        if(*s=='('||*s=='{'||*s=='['){
            STPush(&st,*s);
        }
        else{
            if(STEmpty(&st)){
                return false;
            }
            char top=STTop(&st);
            STPop(&st);
            if((top=='('&&*s!=')')||(top=='{'&&*s!='}')||(top=='['&&*s!=']')){
                STDestroy(&st);
                return false;
            }
        }
        ++s;
    }
    bool ret=STEmpty(&st);
    STDestroy(&st);
    return ret;
}
相关推荐
py有趣30 分钟前
LeetCode算法学习之两数之和 II - 输入有序数组
学习·算法·leetcode
夏鹏今天学习了吗34 分钟前
【LeetCode热题100(62/100)】搜索二维矩阵
算法·leetcode·矩阵
缺点内向2 小时前
C#: 高效移动与删除Excel工作表
开发语言·c#·.net·excel
老前端的功夫2 小时前
Web应用的永生之术:PWA落地与实践深度指南
java·开发语言·前端·javascript·css·node.js
吃着火锅x唱着歌3 小时前
LeetCode 1128.等价多米诺骨牌对的数量
算法·leetcode·职场和发展
ᐇ9593 小时前
Java HashMap深度解析:数据结构、原理与实战指南
java·开发语言·数据结构
十八岁讨厌编程3 小时前
【算法训练营 · 补充】LeetCode Hot100(中)
算法·leetcode
橘颂TA3 小时前
【剑斩OFFER】算法的暴力美学——最小覆盖字串
算法·c/c++·就业
wearegogog1233 小时前
基于混合蛙跳算法和漏桶算法的无线传感器网络拥塞控制与分簇新方法
网络·算法
QT 小鲜肉3 小时前
【个人成长笔记】在 Linux 系统下撰写老化测试脚本以实现自动压测效果(亲测有效)
linux·开发语言·笔记·单片机·压力测试