1.1栈的概念及结构
栈
:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶
了解进栈·出栈规则我们来看两个题:
第一题遵循后进先出原则,很显然选B
我们来具体看一下第二题:
A选项
故A选项成立
B选项
故B选项成立
C选项:
故C选项错误
D选项:
故D选项成立
1.2栈的实现
栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小,但是数组的缺点是需要增容。
具体实现:
#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 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);
#include"Stack.h"
//初始化栈
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;
}
//获取栈顶元素
//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.3栈经典例题<-(戳这里)
根据题意,可利用栈解决
思路:
建立栈,遇到左括号入栈,遇到右括号,让入栈的左括号的栈顶数据(左括号)对应的右括号和遇到的右括号进行匹配,若相等,接着匹配,直到证明字符串有效,只要匹配时遇到不相等,则字符串无效。
代码实现:
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;
StackInit(&st);
bool match=true;
while(*s)
{
if(*s=='['||*s=='('||*s=='{')
{
StackPush(&st,*s);
++s;
}
else
{
if(StackEmpty(&st))
{
match=false;
break;
}
char ch=StackTop(&st);
StackPop(&st);
if((*s==']'&&ch!='[')||(*s=='}'&&ch!='{')||(*s==')'&&ch!='('))
{
match=false;
break;
}
else{
++s;
}
}
}
if(match==true)
{
match=StackEmpty(&st);
}
StackDestory(&st);
return match;
}
– the End –
以上就是我分享的【栈的介绍与实现】相关内容,感谢阅读!
本文收录于专栏:C++
关注作者,持续阅读作者的文章,学习更多知识!
https://blog.csdn.net/weixin_53306029?spm=1001.2014.3001.5343
2021/11/20
————————————————