栈的定义
栈(stack)又名堆栈,是一种限定仅在表尾进行插入和删除操作的线性表
这一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出的原则。
向一个栈插入新元素又称作进栈、入栈或压栈,使之成为新的栈顶元素
从一个栈删除元素又称作出栈或退栈。
C语言实现栈
分析:
可以使用数组或者链表实现,但数组在尾上插入数据的代价比较小。完整程序如下:
头文件
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<assert.h>
typedef int STDatatype;
typedef struct Stack
{
STDatatype* p;
int top;
int capacity;
}ST;
//初始化栈
void StackInit(ST* ps);
//释放栈的空间
void StackDestroy(ST* ps);
//元素入栈
void StackPush(ST* ps, STDatatype x);
//出栈
void StackPop(ST* ps);
//判空
bool StackEmpty(ST* ps);
//计算栈内元素个数
int StackSize(ST* ps);
//返回栈顶元素
STDatatype StackTop(ST* ps);
函数实现文件
#include "Stack.h"
void StackInit(ST* ps)
{
assert(ps);
ps->p = NULL;
ps->top = 0;
ps->capacity = 0;
}
void StackDestroy(ST* ps)
{
assert(ps);
if (ps->p)//如果p为NULL,不可以free
{
free(ps->p);
}
ps->p= NULL; //free后置空,避免野指针问题
ps->top = 0;
ps->capacity = 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->p, sizeof(STDatatype)*newcapacity);
ps->p= tmp;
ps->capacity = newcapacity;
}
ps->p[ps->top] = x;
ps->top++;
}
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;//->优先级高于--
}
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
STDatatype StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->p[ps->top - 1];
}
主函数测试文件
#include "Stack.h"
int main()
{
ST st;
StackInit(&st);
StackPush(&st, 1);
StackPush(&st, 2);
StackPush(&st, 3);
printf("%d ", StackTop(&st));
StackPop(&st);
printf("%d ", StackTop(&st));
StackPop(&st);
StackPush(&st, 4);
StackPush(&st, 5);
while (!StackEmpty(&st))
{
printf("%d ", StackTop(&st));
StackPop(&st);
}
printf("\n");
StackDestroy(&st);
return 0;
}
运行结果
经典括号匹配问题
思路
遍历字符串,如果当前字符为左括号,入栈,等待出现右括号时判断是否匹配,如果是右括号,取栈顶字符与当前字符匹配,栈顶元素出栈,使栈顶字符变成更早入栈的左括号,方便与更靠后的右括号匹配。具体思路和细节难点展现在以下程序注释中
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<assert.h>
//首先实现一个栈
typedef int STDatatype;
typedef struct Stack
{
STDatatype* p;
int top;
int capacity;
}ST;
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->p, sizeof(STDatatype)*newcapacity);
ps->p = tmp;
ps->capacity = newcapacity;
}
ps->p[ps->top] = x;
ps->top++;
}
void StackInit(ST* ps)
{
assert(ps);
ps->p = NULL;
ps->top = 0;
ps->capacity = 0;
}
void StackDestroy(ST* ps)
{
assert(ps);
if (ps->p)//如果p为NULL,不可以free
{
free(ps->p);
}
ps->p = NULL; //free后置空,避免野指针问题
ps->top = 0;
ps->capacity = 0;
}
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;//->优先级高于--
}
STDatatype StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->p[ps->top - 1];
}
bool isValid(char * s){
ST st;
StackInit(&st);
bool matchflag = true;
while (*s)
{
if (*s == '{' || *s == '[' || *s == '(')
{
StackPush(&st, *s);//如果是左括号,入栈
s++;
}
else
{
//程序走到这里,当前字符为右括号
//如果栈内没有元素,说明没有与当前右括号匹配的左括号
if (StackEmpty(&st))
{
matchflag = false;
break;
}
//如果栈内有元素,取出来与当前右括号匹配
char ch = StackTop(&st);
//匹配过的左括号要出栈
StackPop(&st);
//看是否匹配
if (
(*s == '}'&&ch != '{') ||
(*s == ']'&&ch != '[') ||
(*s == ')'&&ch != '(')
)
{
matchflag = false;
break;
}
else
{
s++;//如果匹配,继续向后遍历
//看之后的右括号和栈顶元素是否匹配
//完美地运用了栈先进后出的特点
}
}
}
if (matchflag == true)
{
matchflag = StackEmpty(&st);
//如果遍历完字符且栈不为空,说明栈内左括号是孤单的,不匹配
}
StackDestroy(&st);
return matchflag;
}
int main()
{
//测试几个例子
char *p0 = "{[]}";
printf("%d\n", isValid(p0));
char *p1= "()";
printf("%d\n", isValid(p1));
char *p2 = "()[]{}";
printf("%d\n", isValid(p2));
char *p3 = "(]";
printf("%d\n", isValid(p3));
char *p4 = "([)]";
printf("%d\n", isValid(p4));
}
运行结果: