数据结构-栈(C语言实现)

栈的文字介绍

栈(Stack)的概念

   栈(Stack)是一种特殊的数据结构,它遵循后进先出(LIFO,Last In First Out)的原则。想象一下一摞盘子,我们只能从顶部添加或移除盘子,而不能从中间或底部操作。这就是栈的基本思想。
栈的基本操作

入栈(Push):向栈中添加一个元素的操作叫做入栈。就像我们往一摞盘子上再放一个盘子,新加的盘子会被放在最上面。
出栈(Pop):从栈中移除一个元素的操作叫做出栈。就像我们从一摞盘子上拿走最上面的盘子,只有最上面的盘子才能被拿走。
查看栈顶元素(Peek):查看栈顶元素但不移除它的操作。就像我们看一眼最上面的盘子但不拿走它。
在这里插入图片描述

栈的特点

后进先出:这是栈最核心的特点。后入栈的元素会先出栈,反之,先入栈的元素会后出栈。
操作受限:与数组或链表不同,栈只能从一端(通常称为栈顶)进行操作,不能从中间或另一端进行操作。
临时存储:栈常用于临时存储数据,例如函数调用时的局部变量和返回地址。

栈的应用

函数调用栈:在计算机科学中,当一个函数被调用时,与该函数相关的局部变量、返回地址等信息会被压入一个特殊的栈,称为函数调用栈。当函数执行完毕后,这些信息会被弹出,以便程序继续执行上一个函数。
括号匹配:在编程中,我们经常需要检查代码中的括号是否正确匹配。这可以通过使用一个栈来实现,遇到左括号就入栈,遇到右括号就出栈并检查是否匹配。
逆序输出:栈也可以用来逆序输出一个序列,只需按顺序将序列中的元素入栈,然后再依次出栈即可。
浏览器的前进/后退功能:浏览器的历史记录功能也使用了栈的数据结构,用户可以回退到之前的页面(出栈操作)或前进到之后的页面(实际上是返回到之前出栈的元素,即“回退”的逆操作)。

代码实现

栈的定义

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int STDataType;
//栈的定义
typedef struct Stack {
	STDataType* a;//注意是STDataType,不是int
	int top;
	int capacity;
}ST;

栈的初始化

//栈的初始化
void STInit(ST* ps) {
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

栈的销毁

//栈的销毁
void STDestory(ST* ps) {
	assert(ps);
	free(ps->a);
	ps->a = NULL;//释放后记得指向空
	ps->top = ps->capacity = 0;
}

数据入栈

//数据入栈
void STPush(ST* ps, STDataType x) {//这里x是STDataType
	assert(ps);

	if (ps->top == ps->capacity) {
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));
		if (tmp == NULL) {
			perror("realloc fail");
			return;
		}
		ps->capacity = newCapacity;
		ps->a = tmp;
	}
	ps->a[ps->top] = x;//ps->a[ps->top]=x
	ps->top++;

}

判段栈是否空

如果return后面的条件符合,则该函数返回真,反之则为假

//判段栈是否空
bool STEmpty(ST* ps) {
	assert(ps);
	return ps->top == 0;
}

查看栈顶元素

//查看栈顶元素
STDataType STPeek(ST* ps) {
	assert(ps);
	assert(!STEmpty(ps));
	return ps->a[ps->top - 1];
}

数据出栈

//数据出栈/
void STPop(ST* ps) {
	assert(ps);
	assert(!STEmpty(ps));
	ps->top--;
}

测试

//测试
int main() {
	ST s;
    STInit(&s);//使用之前要初始化!!!

	STPush(&s, 10);
	STPush(&s, 20);
	STPush(&s, 30);
	STPush(&s, 40);
	STPush(&s, 50);
	STPush(&s, 60);

	for (int i = 0; i < 6; i++) {
		printf("%d ", STTop(&s));
		STPop(&s);
	}

	STDestory(&s);

	return 0;
}

完整代码

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int STDataType;
//栈的定义
typedef struct Stack {
	STDataType* a;//注意是STDataType,不是int
	int top;//栈顶
	int capacity;//容量
}ST;

//栈的初始化
void STInit(ST* ps) {
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

//栈的销毁
void STDestory(ST* ps) {
	assert(ps);
	free(ps->a);
	ps->a = NULL;//释放后记得指向空
	ps->top = ps->capacity = 0;
}

//数据入栈
void STPush(ST* ps, STDataType x) {//这里x是STDataType
	assert(ps);

	if (ps->top == ps->capacity) {
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));
		if (tmp == NULL) {
			perror("realloc fail");
			return;
		}
		ps->capacity = newCapacity;
		ps->a = tmp;
	}
	ps->a[ps->top] = x;//ps->a[ps->top]=x
	ps->top++;

}

//判断栈是否为空
bool STEmpty(ST* ps) {
	assert(ps);

	return ps->top == 0;
}

//数据出栈
void STPop(ST* ps) {
	assert(ps);
	assert(!STEmpty(ps));
	ps->top--;
}

//查看栈顶元素
STDataType STTop(ST* ps) {
	assert(ps);
	assert(!STEmpty(ps));
	return ps->a[ps->top - 1];
}

//测试
int main() {
	ST s;
    STInit(&s);//使用之前要初始化!!!

	STPush(&s, 10);
	STPush(&s, 20);
	STPush(&s, 30);
	STPush(&s, 40);
	STPush(&s, 50);
	STPush(&s, 60);

	for (int i = 0; i < 6; i++) {
		printf("%d ", STTop(&s));
		STPop(&s);
	}

	STDestory(&s);

	for (int i = 0; i < 6; i++) {
		printf("%d ", STTop(&s));
		STPop(&s);
	}
	return 0;
}

若有错误或问题,欢迎在评论区提出。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值