数据结构——栈的讲解(超详细)

数据结构------栈的讲解(超详细)-腾讯云开发者社区-腾讯云

复制代码
#include"Stack.h"
void STInit(ST* ps)
{
	ps->arr = NULL;
	ps->capacity = ps->top = 0;  //总空间个数和有用空间个数都初始化为0
}



void STDestroy(ST* ps)
{
	if (ps -> arr)   //先判断是否进行动态内存开辟了
	{
		free(ps -> arr);
	}
	ps->capacity = ps->top = 0;
}



void STPush(ST* ps, STDataType x)   //类似顺序表的尾插
{
	if (ps->capacity == ps->top)
	{
		int newcaopacity = ps->capacity == 0 ? 4 : 2 * ps -> capacity;
		STDataType* arr1 = (STDataType*)realloc(ps->arr, newcaopacity * sizeof(STDataType));
		assert(arr1);
		ps->arr = arr1;
		ps->capacity = newcaopacity;
	}  //扩容完成
	ps->arr[ps->top++] = x;
}


bool panduan(ST * ps)
{
	assert(ps);
	return ps -> top == 0;   //这个是来判断栈是不是空了
}


void STPop(ST* ps)
{
	assert(ps);
	assert(!panduan(ps));
	ps->top--;
}



STDataType STTop(ST* ps)
{
	return ps->arr[ps->top - 1];
}


int STSize(ST* ps)
{
	return ps->top;
}
相关推荐
D_FW8 小时前
数据结构第六章:图
数据结构·算法
Ka1Yan12 小时前
[链表] - 代码随想录 707. 设计链表
数据结构·算法·链表
scx2013100412 小时前
20260112树状数组总结
数据结构·c++·算法·树状数组
宵时待雨13 小时前
数据结构(初阶)笔记归纳3:顺序表的应用
c语言·开发语言·数据结构·笔记·算法
无限进步_14 小时前
【C语言&数据结构】二叉树遍历:从前序构建到中序输出
c语言·开发语言·数据结构·c++·算法·github·visual studio
菜鸟233号15 小时前
力扣518 零钱兑换II java实现
java·数据结构·算法·leetcode·动态规划
鱼跃鹰飞16 小时前
面试题:解释一下什么是全字段排序和rowid排序
数据结构·数据库·mysql
Ka1Yan17 小时前
[链表] - 代码随想录 206. 反转链表
数据结构·链表
学嵌入式的小杨同学17 小时前
C 语言实战:动态规划求解最长公共子串(连续),附完整实现与优化
数据结构·c++·算法·unity·游戏引擎·代理模式
良木生香18 小时前
【C语言进阶】文件操作的相关详解(1):
c语言·数据结构·c++