数据结构(栈【顺序存储】)

前言

栈是特殊的线性表,规定插入和删除在同一端进行
进行插入和删除的那一端成为栈顶,另一端为栈底
插入为进栈,删除为出栈
先进后出
在这里插入图片描述

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Automan之鸿鹄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值