一、栈的概念及结构
栈是一种特殊的线性表,只允许在固定的一端进行插入删除元素操作。
进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。
栈中的数据元素遵循后进先出LIFO(Last In First Out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈,出数据也在栈顶。
二、栈的实现
栈的实现一般可以使用数组或链表实现:
相对而言数组结构实现更优,因为数组尾插数据代价比较小。
Stack.h文件:
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.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);
Stack.c文件:
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
//初始化
void STInit(ST* pst)
{assert(pst);pst->a = NULL;pst->top = pst->capacity = 0;//top指向栈顶数据的下一个位置
}
//销毁
void STDestroy(ST* pst)
{assert(pst);free(pst->a);pst->a = NULL;pst->top = pst->capacity = 0;
}
//入栈
void STPush(ST* pst, STDataType x)
{assert(pst);//扩容if (pst->top == pst->capacity){int newCapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;STDataType* tmp = (STDataType*)realloc(pst->a, newCapacity * sizeof(STDataType));if (tmp == NULL){perror("realloc fail");return;}pst->a = tmp;pst->capacity = newCapacity;}pst->a[pst->top] = x;pst->top++;
}
//出栈
void STPop(ST* pst)
{assert(pst);assert(pst->top > 0);return pst->top--;
}
//获取栈顶元素
STDataType STTop(ST* pst)
{assert(pst);assert(pst->top > 0);return pst->a[pst->top - 1];
}
//获取栈中有效元素个数
int STSize(ST* pst)
{assert(pst);return pst->top;
}
//检测栈是否为空,如果为空返回非0,不为空返回0
bool STEmpty(ST* pst)
{assert(pst);if (pst->top == 0){return 1;}else{return 0;}
}
Test.c文件:
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
int main()
{ST s;STInit(&s);STPush(&s, 1);STPush(&s, 2);STPush(&s, 3);STPush(&s, 4);STPop(&s);printf("%d\n", STTop(&s));while (!STEmpty(&s)){printf("%d ", STTop(&s));STPop(&s);}STDestroy(&s);return 0;
}
三、括号匹配问题
20. 有效的括号 - 力扣(LeetCode)
typedef char 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);//初始化
void STInit(ST* pst)
{assert(pst);pst->a = NULL;pst->top = 0;pst->capacity = 0;
}
//销毁
void STDestroy(ST* pst)
{assert(pst);free(pst->a);pst->a = NULL;pst->top = pst->capacity = 0;
}
//入栈
void STPush(ST* pst, STDataType x)
{assert(pst);//扩容if (pst->top == pst->capacity){int newCapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;STDataType* tmp = (STDataType*)realloc(pst->a, newCapacity * sizeof(STDataType));if (tmp == NULL){perror("realloc fail");return;}pst->a = tmp;pst->capacity = newCapacity;}pst->a[pst->top] = x;pst->top++;
}
//出栈
void STPop(ST* pst)
{assert(pst);pst->top--;
}
//获取栈顶元素
STDataType STTop(ST* pst)
{assert(pst);return pst->a[pst->top - 1];
}
//获取栈中有效元素个数
int STSize(ST* pst)
{assert(pst);return pst->top;
}
//检测栈是否为空,如果为空返回非0,不为空返回0
bool STEmpty(ST* pst)
{assert(pst);return pst->top == 0;
}bool isValid(const char* s) {ST st;STInit(&st);while (*s){//左括号入栈if (*s == '(' || *s == '[' || *s == '{'){STPush(&st, *s);}else {if (STEmpty(&st)){STDestroy(&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;
}