目录
栈
初始化栈
入栈
出栈
获取栈顶元素
获取栈中有效元素个数
检测栈是否为空
销毁栈
一道经典的用栈实现的OJ题
栈
栈的概念及结构
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端
称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶。
栈的实现
栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的
代价比较小。
那么实现栈需要有哪些功能呢
// 下面是定长的静态栈的结构,实际中一般不实用,所以我们主要实现下面的支持动态增长的栈
typedef int STDataType;
#define N 10
typedef struct Stack
{
STDataType _a[N];
int _top; // 栈顶
}Stack;
// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top; // 栈顶
int capacity; // 容量
}Stack;
// 初始化栈
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType data);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);
初始化栈
// 初始化栈
void StackInit(Stack* ps)
{
assert(ps);//判空
ps->a = NULL;
ps->top = 0;//栈内元素个数为0
ps->capacity = 0;//容量为0
}
入栈
// 入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);//判空
if (ps->top == ps->capacity)//判断栈是否已满
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;//扩容(新的容量)
Stack* tmp = realloc(ps->a, sizeof(STDataType) * newcapacity);
if (tmp == NULL)//增容失败
{
printf("realloc failn");
exit(-1);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
ps->a[ps->top ] = data;
ps->top++;
}
出栈
// 出栈
void StackPop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));//栈非空
ps->top--;
}
获取栈顶元素
// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));//栈非空
return ps->a[ps->top - 1];
}
获取栈中有效元素个数
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
检测栈是否为空
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(Stack* ps)
{
assert(ps);
return ps->capacity == 0;
}
销毁栈
// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
if(ps->a)
free(ps->a);
ps->a == NULL;
ps->capacity = 0;
ps->top = 0;
}
// 入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);//判空
if (ps->top == ps->capacity)//判断栈是否已满
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;//扩容(新的容量)
Stack* tmp = realloc(ps->a, sizeof(STDataType) * newcapacity);
if (tmp == NULL)//增容失败
{
printf("realloc failn");
exit(-1);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
ps->a[ps->top ] = data;
ps->top++;
}
出栈
// 出栈
void StackPop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));//栈非空
ps->top--;
}
获取栈顶元素
// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));//栈非空
return ps->a[ps->top - 1];
}
获取栈中有效元素个数
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
检测栈是否为空
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(Stack* ps)
{
assert(ps);
return ps->capacity == 0;
}
销毁栈
// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
if(ps->a)
free(ps->a);
ps->a == NULL;
ps->capacity = 0;
ps->top = 0;
}
// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));//栈非空
return ps->a[ps->top - 1];
}
获取栈中有效元素个数
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
检测栈是否为空
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(Stack* ps)
{
assert(ps);
return ps->capacity == 0;
}
销毁栈
// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
if(ps->a)
free(ps->a);
ps->a == NULL;
ps->capacity = 0;
ps->top = 0;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(Stack* ps)
{
assert(ps);
return ps->capacity == 0;
}
销毁栈
// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
if(ps->a)
free(ps->a);
ps->a == NULL;
ps->capacity = 0;
ps->top = 0;
}
下面是总体代码
stack.h
#pragma once #include#include #include typedef int STDataType; // 支持动态增长的栈 typedef int STDataType; typedef struct Stack { STDataType* a; int top; // 栈顶 int capacity; // 容量 }Stack;
stack.c
#define _CRT_SECURE_NO_WARNINGS
#include"stack.h"
// 初始化栈
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType data);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);
// 初始化栈
void StackInit(Stack* ps)
{
assert(ps);//判空
ps->a = NULL;
ps->top = 0;//栈内元素个数为0
ps->capacity = 0;//容量为0
}
// 入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);//判空
if (ps->top == ps->capacity)//判断栈是否已满
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;//扩容(新的容量)
Stack* tmp = realloc(ps->a, sizeof(STDataType) * newcapacity);
if (tmp == NULL)
{
printf("realloc failn");
exit(-1);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
ps->a[ps->top ] = data;
ps->top++;
}
// 出栈
void StackPop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));//栈非空
ps->top--;
}
// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));//栈非空
return ps->a[ps->top - 1];
}
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(Stack* ps)
{
assert(ps);
return ps->capacity == 0;
}
// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
if(ps->a)
free(ps->a);
ps->a == NULL;
ps->capacity = 0;
ps->top = 0;
}
一道经典的用栈实现的OJ题
题目链接:力扣
给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。示例 1:
输入:s = "()"
输出:true
示例 2:输入:s = "()[]{}"
输出:true
示例 3:输入:s = "(]"
输出:false
示例 4:输入:s = "([)]"
输出:false
示例 5:输入:s = "{[]}"
输出:true
这里我们用的是C语言解法
typedef char STDatatype;
typedef struct stack
{
STDatatype *a;
int top;//栈顶
int capacity;//容量
}stack;
void StackInit(stack*p);
void StackDestroy(stack*p);
void StackPop(stack*p);
void StackPush(stack*p,STDatatype x);
bool StackEmpty(stack*p);
int StackSize(stack*p);
STDatatype StackTop(stack* p);
//初始化
void StackInit(stack*p)
{
assert(p);
p->a=NULL;
p->top=0;
p->capacity=0;
}
//销毁
void StackDestroy(stack*p)
{
assert(p);
if(p->a)
{
free(p->a);
}
p->a=NULL;
p->top=0;
p->capacity=0;
}
//压栈
void StackPush(stack*p,char x)
{
assert(p);
if(p->top==p->capacity)
{
int newcapacity=p->capacity==0?4:p->capacity*2;
STDatatype*tmp=(STDatatype*)realloc(p->a,sizeof(STDatatype)*newcapacity);
if(tmp==NULL)
{
printf("rellaoc failn");
exit(-1);
}
p->a=tmp;
p->capacity=newcapacity;
}
p->a[p->top]=x;
p->top++;
}
//出栈
void StackPop(stack*p)
{
assert(p);
assert(p->top);
p->top--;
}
//判空
bool StackEmpty(stack*p)
{
assert(p);
return p->top==0;
}
//个数
int StackSize(stack*p)
{
assert(p);
return p->top;
}
//栈顶元素
STDatatype StackTop(stack* p)
{
assert(p);
return p->a[p->top-1];
}
bool isValid(char * s){
stack p;
StackInit(&p);
char*tmp=s;
bool math=true;
while(*tmp)
{
// 左括号压栈
if(*tmp=='('||*tmp=='{'||*tmp=='[')
{
StackPush(&p,*tmp);
tmp++;
}
else
{
if(StackEmpty(&p))//先判空
{
math=false;
break;
}
char ch=StackTop(&p);
//右括号出栈
if( (ch=='('&&*tmp==')')
|| (ch=='['&&*tmp==']')
|| (ch=='{'&&*tmp=='}') )
{
StackPop(&p);
tmp++;
}
else
{
math=false;
break;
}
}
}
if(math==true) math=StackEmpty(&p);
StackDestroy(&p);
return math;
}



