{
free(ps->a);//释放栈
}
ps->a = NULL;
ps->top = 0;//-1也可以 //栈顶置0
ps->capacity = 0;//容量置0
}
//入栈
//进行入栈操作前,我们需要检测栈的当前状态,若已满,则需要先对其进行增容,然后才能进行入栈操作
void StackPush(ST* ps, STDatatype x)
{
assert(ps);
//检查空间够不够,不够就增容
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDatatype* tmp = realloc(ps->a, sizeof(STDatatype) * newcapacity);
if (tmp == NULL)
{
printf(“realloc fail!\n”);
exit(-1);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
ps->a[ps->top] = x;//栈顶位置存放元素x
ps->top++;//栈顶上移
}
//出栈
void StackPop(ST* ps)//删数据
{
assert(ps);
assert(!StackEmpty(ps));//检测栈是否为空
–ps->top;//栈顶下移
}
//检测栈是否为空
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
//获取栈中有效元素个数
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
//获取栈顶元素
//top记录的是栈顶,使用top的值便代表栈中有效元素的个数
STDatatype StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));//检测栈是否为空
return ps->a[ps->top - 1];//返回栈顶元素
}
简单测试:
#include"Stack.h"
int main()
{
ST st;
StackInit(&st);
StackPush(&st,1);
StackPush(&st,2);
StackPush(&st,3);
StackPush(&st,4);
StackPush(&st,5);
//若栈非空,逐个获取栈顶元素,出栈
while (!StackEmpty(&st))
{
printf(“%d”, StackTop(&st));
StackPop(&st);
}
printf(“\n”);
StackDestory(&st);
return 0;
}
运行结果:
(遵循后进先出)
根据题意,可利用栈解决
思路:
建立栈,遇到左括号入栈,遇到右括号,让入栈的左括号的栈顶数据(左括号)对应的右括号和遇到的右括号进行匹配,若相等,接着匹配,直到证明字符串有效,只要匹配时遇到不相等,则字符串无效。
代码实现:
1.先将我们我们之前写的栈原封不动拿过来:
注意:要将栈中存储的元素类型该为
char
typedef char STDatatype;//栈中存储的元素类型
typedef struct Stack
{
STDatatype* a;//栈
int top;//栈顶
int capacity;//容量,方便增容
}ST;
// 初始化栈
void StackInit(ST* ps);
// 销毁栈
void StackDestory(ST* ps);
// 入栈
void StackPush(ST* ps,STDatatype x);
// 出栈
void StackPop(ST* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(ST* ps);
// 获取栈中有效元素个数
int StackSize(ST* ps);
// 获取栈顶元素
STDatatype StackTop(ST* ps);
//初始化栈
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
//销毁栈
void StackDestory(ST* ps)
{
assert(ps);
if (ps->a)
{
free(ps->a);//释放栈
}
ps->a = NULL;
ps->top = 0;//-1也可以 //栈顶置0
ps->capacity = 0;//容量置0
}
//入栈
void StackPush(ST* ps, STDatatype x)
{
assert(ps);
//检查空间够不够,不够就增容
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDatatype* tmp = realloc(ps->a, sizeof(STDatatype) * newcapacity);
if (tmp == NULL)
{
printf(“realloc fail!\n”);
exit(-1);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
ps->a[ps->top] = x;//栈顶位置存放元素x
ps->top++;//栈顶上移
}
//出栈
void StackPop(ST* ps)//删
{
assert(ps);
assert(!StackEmpty(ps));//检测栈是否为空
–ps->top;//栈顶下移
}
//检测栈是否为空
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
//获取栈中有效元素个数
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
//获取栈顶元素
STDatatype StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));//检测栈是否为空
return ps->a[ps->top - 1];//返回栈顶元素
}
2.逻辑实现:
bool isValid(char * s){
ST st;
写在最后
很多人感叹“学习无用”,实际上之所以产生无用论,是因为自己想要的与自己所学的匹配不上,这也就意味着自己学得远远不够。无论是学习还是工作,都应该有主动性,所以如果拥有大厂梦,那么就要自己努力去实现它。
最后祝愿各位身体健康,顺利拿到心仪的offer!
由于文章的篇幅有限,所以这次的蚂蚁金服和京东面试题答案整理在了PDF文档里
(!StackEmpty(ps));//检测栈是否为空
return ps->a[ps->top - 1];//返回栈顶元素
}
2.逻辑实现:
bool isValid(char * s){
ST st;
写在最后
很多人感叹“学习无用”,实际上之所以产生无用论,是因为自己想要的与自己所学的匹配不上,这也就意味着自己学得远远不够。无论是学习还是工作,都应该有主动性,所以如果拥有大厂梦,那么就要自己努力去实现它。
最后祝愿各位身体健康,顺利拿到心仪的offer!
由于文章的篇幅有限,所以这次的蚂蚁金服和京东面试题答案整理在了PDF文档里
[外链图片转存中…(img-MpPMbDah-1721880145346)]
[外链图片转存中…(img-hpx86mXm-1721880145346)]
[外链图片转存中…(img-RIwoRcgJ-1721880145347)]