首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >数据结构——关于栈

数据结构——关于栈

作者头像
迷迭所归处
发布2024-11-19 17:04:50
发布2024-11-19 17:04:50
10900
代码可运行
举报
文章被收录于专栏:动态规划动态规划
运行总次数:0
代码可运行

1.栈

1.1栈的概念及结构 栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作

进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出的原则

比如:羽毛球桶,弹夹等等 压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶

出栈:栈的删除操作叫做出栈,出数据也在栈顶


2. 动态栈的实现

栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小

Stack.h

代码语言:javascript
代码运行次数:0
运行
复制
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>

typedef int STDataType;
typedef struct Stack
{
	STDataType* a;//指向数组的指针,命名为a
	int top;// 栈顶
	int capacity; // 容量
}ST;


// 初始化栈
void StackInit(ST* pst);

// 销毁栈
void StackDestroy(ST* pst);

// 入栈
void StackPush(ST* pst, STDataType x);


// 出栈
void StackPop(ST* pst);


// 获取栈顶元素top
STDataType StackTop(ST* pst);


// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(ST* pst);


// 获取栈中有效元素个数
int StackSize(ST* pst);

Stack.c

初始化栈

代码语言:javascript
代码运行次数:0
运行
复制
// 初始化栈
void StackInit(ST* pst)
{
	assert(pst);
	pst->a = NULL;

	// top为-1直接指向栈顶元素
	// pst->top = -1;

	//top为0直接指向栈顶元素的下一个位置
	pst->top = 0;
	pst->capacity = 0;
}

 如:

销毁栈

代码语言:javascript
代码运行次数:0
运行
复制
// 销毁栈
void StackDestroy(ST* pst)
{
	assert(pst);
	free(pst->a);
	pst->a = NULL;
	pst->top = pst->capacity = 0;
}

入栈/插入

代码语言:javascript
代码运行次数:0
运行
复制
// 入栈/插入
void StackPush(ST* pst, STDataType x)
{
	assert(pst);
	//扩容
	if (pst->top == pst->capacity)
	{
		//如果capacity为0,则初始化给4,如果不为0,则原来的空间*2
		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		pst->a = tmp;
		pst->capacity = newcapacity;
	}
	pst->a[pst->top] = x;//放数据
	pst->top++;
	//如果是给与-1则是先top++再放数据
}

 出栈/删除

代码语言:javascript
代码运行次数:0
运行
复制
// 出栈/删除
void StackPop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);
	pst->top--;
}

获取栈顶元素top 

代码语言:javascript
代码运行次数:0
运行
复制
// 获取栈顶元素top
STDataType StackTop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);

	return pst->a[pst->top - 1];
}

判断栈是否为空

代码语言:javascript
代码运行次数:0
运行
复制
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(ST* pst)
{
	assert(pst);
	//如果初始化为-1则是 pst->top+1 == 0;
	return pst->top == 0;
}

获取栈中有效元素个数

代码语言:javascript
代码运行次数:0
运行
复制
// 获取栈中有效元素个数
int StackSize(ST* pst)
{
	assert(pst);
	//因为top为0接指向栈顶元素的下一个位置,所以在这里相当于size
	return pst->top;
}

3.全部代码

1.Stack.c

代码语言:javascript
代码运行次数:0
运行
复制
#include "Stack.h"

 
// 初始化栈
void StackInit(ST* pst)
{
	assert(pst);
	pst->a = NULL;

	// top为-1直接指向栈顶元素
	// pst->top = -1;

	//top为0直接指向栈顶元素的下一个位置
	pst->top = 0;
	pst->capacity = 0;
}

// 销毁栈
void StackDestroy(ST* pst)
{
	assert(pst);
	free(pst->a);
	pst->a = NULL;
	pst->top = pst->capacity = 0;
}

// 入栈/插入
void StackPush(ST* pst, STDataType x)
{
	assert(pst);
	//扩容
	if (pst->top == pst->capacity)
	{
		//如果capacity为0,则初始化给4,如果不为0,则原来的空间*2
		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		pst->a = tmp;
		pst->capacity = newcapacity;
	}
	pst->a[pst->top] = x;//放数据
	pst->top++;
	//如果是给与-1则是先top++再放数据
}


// 出栈/删除
void StackPop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);
	pst->top--;
}


// 获取栈顶元素top
STDataType StackTop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);

	return pst->a[pst->top - 1];
}


// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(ST* pst)
{
	assert(pst);
	//如果初始化为-1则是 pst->top+1 == 0;
	return pst->top == 0;
}


// 获取栈中有效元素个数
int StackSize(ST* pst)
{
	assert(pst);
	//因为top为0接指向栈顶元素的下一个位置,所以在这里相当于size
	return pst->top;
}

2.Test.c

代码语言:javascript
代码运行次数:0
运行
复制
#include"Stack.h"
#include<stdio.h>
#include<stdlib.h>
//
//int main()
//{
//	// 原地扩容
//	// 异地扩容
//	int* p1 = (int*)malloc(8);
//	printf("%p\n", p1);
//
//	int* p2 = (int*)realloc(p1, 80);
//	printf("%p\n", p2);
//
//	free(p2);
//
//
//	int i = 0;
//	int ret1 = ++i;
//
//	int ret2 = i++;
//
//
//
//	return 0;
//}



//int main()
//{
//	ST s;
//	STInit(&s);
//	STPush(&s, 1);
//	STPush(&s, 2);
//	STPush(&s, 3);
//	STPush(&s, 4);
//
//	printf("%d\n", STTop(&s));
//	STPop(&s);
//	printf("%d\n", STTop(&s));
//	STPop(&s);
//	STPop(&s);
//	STPop(&s);
//	STPop(&s);
//
//	//printf("%d\n", STTop(&s));
//
//	STDestroy(&s);
//
//	return 0;
//}

int main()
{
	// 入栈:1 2 3 4
	// 出栈:4 3 2 1  /  2 4 3 1
	ST s;
	STInit(&s);
	STPush(&s, 1);
	STPush(&s, 2);

	printf("%d ", STTop(&s));
	STPop(&s);

	STPush(&s, 3);
	STPush(&s, 4);

	while (!STEmpty(&s))
	{
		printf("%d ", STTop(&s));
		STPop(&s);
	}

	STDestroy(&s);
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-08-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.栈
    • 1.1栈的概念及结构 栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作
  • 2. 动态栈的实现
  • 3.全部代码
    • 1.Stack.c
    • 2.Test.c
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档