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

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

复制代码
#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;
}
相关推荐
琢磨先生David6 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
qq_454245036 天前
基于组件与行为的树状节点系统
数据结构·c#
超级大福宝6 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
岛雨QA6 天前
常用十种算法「Java数据结构与算法学习笔记13」
数据结构·算法
weiabc6 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
wefg16 天前
【算法】单调栈和单调队列
数据结构·算法
岛雨QA6 天前
图「Java数据结构与算法学习笔记12」
数据结构·算法
czxyvX6 天前
020-C++之unordered容器
数据结构·c++
岛雨QA6 天前
多路查找树「Java数据结构与算法学习笔记11」
数据结构·算法
AKA__Zas6 天前
初识基本排序
java·数据结构·学习方法·排序