前言
栈是特殊的线性表,规定插入和删除在同一端进行
进行插入和删除的那一端成为栈顶,另一端为栈底
插入为进栈,删除为出栈
先进后出
PS:例如:ABC进栈、出栈共有几种情况
- CBA
- ACB
- BAC
- ABC
- BCA
错误操作
上溢:超出规定的空间大小还进行插入操作
下溢:栈中元素已经用完了还进行删除操作
源码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAXSIZE 100
typedef struct {
int a[MAXSIZE];
int top;
}sequence_stack;
void init(sequence_stack *st) { //初始化栈
st->top = 0;
}
int empty(sequence_stack st) { //判断栈是否为空
return st.top ? 0 : 1;
}
int read(sequence_stack st) { //取得栈顶节点值
if (empty(st)) {
printf("栈是空的!\n");
exit(1);
}
else
return st.a[st.top - 1];
}
void push(sequence_stack *st, int x) { //栈的插入
if (st->top == MAXSIZE) {
printf("栈是满的!\n");
exit(0);
}
st->a[st->top] = x;
++st->top;
}
void pop(sequence_stack *st) { //栈的删除操作
if (st->top == 0) {
printf("栈是空的!\n");
exit(1);
}
--st->top;
}
void display(sequence_stack st) { //打印栈
if (st.top == 0) {
printf("栈是空的!\n");
exit(1);
}
for (int i = 0; i < st.top; ++i)
printf("%5d", st.a[i]);
}
void do_empty(sequence_stack *st) { //清空栈
if (st->top == 0) {
printf("栈是空的!");
exit(1);
}
st->top = 0;
}
int main()
{
srand((unsigned)time(NULL));
sequence_stack st;
int num;
printf("请输入栈的大小:");
scanf("%d", &num);
st.top = num;
for (int i = 0; i < st.top; ++i)
st.a[i] = rand() % 100;
printf("自动生成的栈为:");
display(st);
printf("\n-----------------------------------------------------------\n");
printf("进行栈的插入操作(插入20)\n");
push(&st, 20);
printf("插入后的栈为:");
display(st);
printf("\n-----------------------------------------------------------\n");
printf("进行栈的删除操作(删除最后一个)\n");
pop(&st);
printf("删除后的栈为:");
display(st);
printf("\n-----------------------------------------------------------\n");
printf("下面进行栈的清空操作!\n");
do_empty(&st);
display(st);
return 0;
}
栈的应用–括号匹配
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef struct {
int a[MAXSIZE];
int top;
}sequence_stack;
void init(sequence_stack *st) { //初始化栈
st->top = 0;
}
int empty(sequence_stack st) { //判断栈是否为空
return st.top ? 0 : 1;
}
int read(sequence_stack st) { //取得栈顶节点值
if (empty(st)) {
printf("栈是空的!\n");
exit(1);
}
else
return st.a[st.top - 1];
}
void pop(sequence_stack *st) { //栈的删除操作
if (st->top == 0) {
printf("栈是空的!\n");
exit(1);
}
--st->top;
}
void push(sequence_stack *st, int x) { //栈的插入
if (st->top == MAXSIZE) {
printf("栈是满的!\n");
exit(0);
}
st->a[st->top] = x;
++st->top;
}
int match_kuohao(char c[]) { // 括号匹配
int i = 0;
sequence_stack s;
init(&s);
while (c[i] != '#') {
switch (c[i])
{
case '{':
case '[':
case '(':
push(&s, c[i]);
break;
case '}':
if (!empty(s) && read(s) == '{') {
pop(&s);
break;
}
else
return 0;
case ']':
if (!empty(s) && read(s) == '[') {
pop(&s);
break;
}
else
return 0;
case ')':
if (!empty(s) && read(s) == '(') {
pop(&s);
break;
}
else
return 0;
}
++i;
}
return empty(s);/*栈为空则匹配,否则不匹配*/
}
int main()
{
sequence_stack st;
int num = 1;
while (num&&num % 2 != 0) {
printf("请输入栈的大小(必须为偶数):");
scanf("%d", &num);
}
st.top = num;
char c[MAXSIZE];
printf("请输入括号(包括(){}[]):");
for (int i = 0; i < st.top; ++i) {
scanf("%c", &c[i]);
}
if (!match_kuohao)
printf("不匹配");
else
printf("匹配");
return 0;
}