【数据结构】栈

2024-03-04 1025阅读

温馨提示:这篇文章已超过385天没有更新,请注意相关的内容是否还可用!

简介:本系列博客为C数据结构系列内容,通过代码来具体实现某个经典简单数据结构

适宜人群:已大体了解C语法同学

作者留言:本博客相关内容如需转载请注明出处,本人学疏才浅,难免存在些许错误,望留言指正

作者博客链接:睡觉待开机

1.认识栈

栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除

操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)

的原则。

压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。

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

【数据结构】栈

【数据结构】栈

2.所有接口一览

【数据结构】栈

3.各个接口的实现

注:因为在实现上很简单,因而不做详细说明。

在实现上,底层是数组,也可以说是顺序表。

【数据结构】栈

1.初始化与销毁接口

【数据结构】栈

void StackInit(ST* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}
void StackDestroy(ST* ps)
{
	assert(ps);
	free(ps->arr);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
	ps = NULL;
}

2.入栈出栈

【数据结构】栈

【数据结构】栈

【数据结构】栈

void StackPush(ST* ps, STDateType x)
{
	assert(ps);
	
	if (ps->capacity == ps->top)
	{
		//初始值的情况
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDateType* temp = (STDateType*)realloc(ps->arr, newcapacity * sizeof(STDateType));
		if (temp == NULL)
		{
			perror("malloc fail!");
			exit(-1);
		}
		ps->arr = temp;
		ps->capacity = newcapacity;
	}
	ps->arr[ps->top++] = x;
}
void StackPop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}
STDateType StackTop(ST* ps)
{
	assert(ps);
	return ps->arr[ps->top - 1];
}

3.统计与判断

【数据结构】栈

bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}
int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

4.全部代码

#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
test1()
{
	ST st;
	
	StackInit(&st);
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);
	StackPush(&st, 5);
	StackPush(&st, 6);
	while (!StackEmpty(&st))
	{
		printf("%d", StackTop(&st));
		StackPop(&st);
	}
	
	StackDestroy(&st);
}
int main()
{
	test1();
	return 0;
}
#pragma once
#include
#include
#include
#include
//用数组的方式实现栈结构
typedef int STDateType;
typedef struct Stack
{
	STDateType* arr;
	int top;
	int capacity;
}ST;
//初始化与销毁
void StackInit(ST* ps);
void StackDestroy(ST* ps);
//入栈出栈
void StackPush(ST* ps,STDateType x);
STDateType StackTop(ST* ps);
void StackPop(ST* ps);
//统计与判断
bool StackEmpty(ST* ps);
int StackSize(ST* ps);
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
void StackInit(ST* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}
void StackDestroy(ST* ps)
{
	assert(ps);
	free(ps->arr);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
	ps = NULL;
}
void StackPush(ST* ps, STDateType x)
{
	assert(ps);
	
	if (ps->capacity == ps->top)
	{
		//初始值的情况
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDateType* temp = (STDateType*)realloc(ps->arr, newcapacity * sizeof(STDateType));
		if (temp == NULL)
		{
			perror("malloc fail!");
			exit(-1);
		}
		ps->arr = temp;
		ps->capacity = newcapacity;
	}
	ps->arr[ps->top++] = x;
}
void StackPop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}
STDateType StackTop(ST* ps)
{
	assert(ps);
	return ps->arr[ps->top - 1];
}
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}
int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

完。

VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]