C语言!!!!刚了解到栈 不知道咋做 有点懵
题目是修改下面的栈示例使它存储字符而不是整数。接下来,增加main函数,用来要求用户输入一串圆括号或花括号,然后指出它们之间的嵌套是否正确:
Enter parenteses and/or braces:((){}{()})
Parenteses/braces are nested properly
提示:读入左圆括号或左花括号时,把它们像字符一样压入栈中。当读入右圆括号或右花括号时,把栈顶的项弹出,并且检查弹出项是否是匹配的圆括号或花括号。(如果不是,那么圆括号或花括号嵌套不正确。)当程序读入换行符时,检查栈是否为空。如果为空,那么圆括号或花括号匹配;如果栈不为空(或者如果曾经调用过stack_underflow函数),那么圆括号或花括号不匹配。如果调用stack_underflow函数,程序显示信息Stack overflow,并且立刻终止。
下面是栈示例:
#include <stdbool.h>
#define STACK_SIZE 100
int contents[STACK_SIZE];
int top = 0;
void make_empty (void)
{
top = 0;
}
bool is_empty (void)
{
return top == 0;
}
bool is_full (void)
{
return top == STACK_SIZE;
}
void push (int i)
{
is (is_full))
stack_overflow();
else
contents[top++] = i;
}
int pop (void)
{
if (is_empty ())
stack_underflow ();
else
return contents[--top];
}