引言
比起顺序表与链表,栈存在更多限制。
顺序表与链表之间的优缺点
栈的概念
栈是一种线性表(逻辑上连续),它只允许在固定的一端进行插入和删除,进行数据插入和删除的一端称为栈顶,而另一端就被称为栈底。
压栈
栈的插入称为圧栈,入数据在栈顶。
出栈
栈的删除称为出栈,出数据在栈顶。
栈的实现
如果我们用链表实现,就将新节点头插在原节点上。
这里如果我们用数组实现
目录
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);
//判空
bool STEmpty(ST* pst);
//获取数据个数
int STSize(ST* pst);
栈的初始化
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
pst->top = 0;//代表top指向栈顶的下一位,如果top为-1则指向栈顶的下一个位置
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));//如果为空realloc执行malloc功能
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);
pst->top--;//栈顶减少一位就可以达成删除效果
}
取栈顶数据
STDataType STTop(ST* pst)
{
assert(pst);
assert(pst->top > 0);
return pst->a[pst->top - 1];
}
判断栈是否为空
bool STEmpty(ST* pst)
{
assert(pst);
return pst->top == 0;
}
获取数据个数
int STSize(ST* pst)
{
assert(pst);
return pst->top;
}
栈的打印
如果我们想要取出栈中的元素,我们就要每次将栈中的top删除,然后再每次访问栈顶,从而达到访问栈的效果。代码如下:
int main()
{
ST s;
STInit(&s);
STPush(&s, 1);
STPush(&s, 2);
STPush(&s, 3);
STPush(&s, 4);
while (!STEmpty(&s))//如果栈不为空
{
printf("%d ", STTop(&s));
STPop(&s);
}
STDestroy(&s);
return 0;
}
栈的应用
括号匹配问题
首先,我们应该对题目进行分析:这题如果我们只进行数量匹配,即左括号与右括号数量相等,会发现会存在反例:))((。于是,我们可以先让左括号入栈,再将右括号出栈顶与左括号进行匹配。由于栈的先进后出性质,这题与栈有天然的契合性。循环条件是指针*s.
1.如果*s为左括号,则入栈:
if(*s == '(' || *s == '[' || *s == '{')
{
STPush(&st, *s);
}
2.若栈为空(没有右括号的情况),则返回false,再对栈进行遍历,如果不匹配则返回false:
else
{
if(STEmpty(&st))
{
return false;
}
char top = STTop(&st);
STPop(&st);
if((top == '(' && *s != ')')
|| (top == '[' && *s != ']')
|| (top == '{' && *s != '}'))
{
STDestroy(&st);
return false;
}
}
++s;
3.最后判断若栈不为空,则说明括号没有匹配完,返回false
bool ret = STEmpty(&st);
STDestroy(&st);
return ret;
综上,总代码为:
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);
//判空
bool STEmpty(ST* pst);
//获取数据个数
int STSize(ST* pst);
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
pst->top = 0;//代表top指向栈顶的下一位,如果top为-1则指向栈顶的下一个位置
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));//如果为空realloc执行malloc功能
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);
pst->top--;//栈顶减少一位就可以达成删除效果
}
//取栈顶数据
STDataType STTop(ST* pst)
{
assert(pst);
assert(pst->top > 0);
return pst->a[pst->top - 1];
}
bool STEmpty(ST* pst)
{
assert(pst);
return pst->top == 0;
}
int STSize(ST* pst)
{
assert(pst);
return pst->top;
}
bool isValid(char* s) {
ST st;
STInit(&st);
while(* s)
{
if(*s == '(' || *s == '[' || *s == '{')
{
STPush(&st, *s);
}
else
{
if(STEmpty(&st))
{
return false;
}
char top = STTop(&st);
STPop(&st);
if((top == '(' && *s != ')')
|| (top == '[' && *s != ']')
|| (top == '{' && *s != '}'))
{
STDestroy(&st);
return false;
}
}
++s;
}
//栈不为空,说明左括号数量比右括号多,数量不匹配,返回false
bool ret = STEmpty(&st);
STDestroy(&st);
return ret;
}