一 .栈
1.概念与结构
1)概念:
栈:⼀种特殊的线性表,其只允许在固定的⼀端进⾏插⼊和删除元素操作。进⾏数据插⼊和删除操作的⼀端称为栈顶,另⼀端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
压栈:栈的插⼊操作叫做进栈/压栈/⼊栈,⼊数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶。
2)结构:栈的实现⼀般可以使⽤数组或者链表实现,相对⽽⾔数组的结构实现更优⼀些。因为数组在尾上插⼊数据的代价⽐较⼩。
stack.h----头文件
#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);
//判断栈是否为空
bool StackEmpty(ST* ps);
//入栈
void StackPush(ST* ps, STDataType x);
//出栈
void StackPop(ST* ps);
//取栈顶元素
STDataType StackTop(ST* ps);
//栈的销毁
void STDestroy(ST* ps);
//获取栈中有效元素个数
int STSize(ST* ps);
stack.c----源文件
#include"stack.h"
//栈的初始化
void STInit(ST* ps)
{
assert(ps);
ps->arr = NULL;
ps->capacity = ps->top = 0;
}
//判断栈是否为空
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
//入栈
void StackPush(ST* ps, STDataType x)
{
assert(ps);
//判断空间是否充足,不足增容
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;
}
//出栈
void StackPop(ST* ps)
{
assert(ps);
//判断栈是否没有数据,是空栈
//assert(ps->arr)
assert(!StackEmpty(ps));
--ps->top;
}
//取栈顶元素
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->arr[ps->top - 1];
}
//获取栈中有效元素个数
int STSize(ST* ps)
{
assert(ps);
return ps->top;
}
//栈的销毁
void STDestroy(ST* ps)
{
assert(ps);
if (ps->arr)
{
free(ps->arr);
}
ps->arr = NULL;
ps->top = ps->capacity = 0;
}
2.有关习题
有效的括号(leetcode/OJ平台20)
[思路:遍历字符串,遍历到左括号,入栈,遍历到右括号,取栈顶元素,并进行比较,匹配*ps,出栈,ps++;不匹配,返回false]
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
//首先定义一个栈,并写出栈的相关操作
typedef int STDataType;
typedef struct Stack
{
STDataType* arr;
int capacity; //栈的空间大小
int top; //栈顶
}ST;
void STInit(ST* ps)
{
assert(ps);
ps->arr = NULL;
ps->capacity = ps->top = 0;
}
//判断栈是否为空
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
//入栈
void StackPush(ST* ps, STDataType x)
{
assert(ps);
//判断空间是否充足,不足增容
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;
}
//出栈
void StackPop(ST* ps)
{
assert(ps);
//判断栈是否没有数据,是空栈
//assert(ps->arr)
assert(!StackEmpty(ps));
--ps->top;
}
//取栈顶元素
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->arr[ps->top - 1];
}
//获取栈中有效元素个数
int STSize(ST* ps)
{
assert(ps);
return ps->top;
}
//栈的销毁
void STDestroy(ST* ps)
{
assert(ps);
if (ps->arr)
{
free(ps->arr);
}
ps->arr = NULL;
ps->top = ps->capacity = 0;
}
bool isValid(char* s)
{
ST st;
STInit(&st);
//遍历字符串s
char* ps = s;
while (*ps != '\0')
{
//左括号入栈
if (*ps == '(' || *ps == '[' || *ps == '{')
{
StackPush(&st, *ps);
}
else
{
//右括号,比较
//为空(只有一个左括号)直接false
if (StackEmpty(&st))
{
return false;
}
char a = StackTop(&st);
if ((*ps == ')' && a == '(')
|| (*ps == ']' && a == '[')
|| (*ps = '}' && a == '{'))
{
StackPop(&st);
}
else
{
STDestroy(&st);
return false;
}
}
ps++;
}
bool ret = StackEmpty(&st) == true;
//销毁
STDestroy(&st);
return ret;
}