数据结构——栈(附图文讲解 | 超详细)

一、前言

这篇博客我们来聊聊数据结构——栈

二、栈

2.1 定义

概念:⼀种特殊的线性表,其只允许在固定的⼀端进⾏插⼊和删除元素操作。进⾏数据插⼊和删除操作的⼀端称为栈顶,另⼀端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。

1.后进先出

那么是什么是后进先出呢?

先进去的数据后出来,而后进去的数据先出来

如图:

在这里插入图片描述

2.压栈和出栈

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

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

2.2 栈的实现

对于栈的实现来说可以使⽤数组或者链表实现,相对⽽⾔数组的结构实现更优⼀些。因为数组在尾上插⼊数据的代价⽐较⼩。所以我们这里用数组来实现。

1. 创建栈
//栈
typedef int STDataType; //自定义数据元素类型
typedef struct Stack
{
	STDataType* arr;
	int top; //有效数据个数     
	int capacity; //空间容量
}ST;
2. 栈的初始化
//初始化
void StackInit(ST* ps)
{
	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}
3. 栈的销毁
//栈的销毁
void StackDestroy(ST* ps)
{
	if (ps->arr)
		free(ps->arr);
	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}
4. 入栈

先向内存申请空间,再从栈顶入栈

在这里插入图片描述

//入栈——栈顶
void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
	ps->arr[ps->top++] = x;
}
5. 判断栈是否为空

用于之后的功能接口进行断言

//判断栈是否为空
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}
6. 出栈

从栈顶出栈

在这里插入图片描述

//出栈
void StackPop(ST* ps)
{
	assert(!StackEmpty(ps));
	--ps->top;
}
7. 取栈顶

取栈顶数据

//取栈顶
STDataType StackTop(ST* ps)
{
	assert(!StackEmpty(ps));
	return ps->arr[ps->top - 1];
}
8. 有效元素个数
//取有效元素个数
int StackSize(ST* ps)
{
	return ps->top;
}

三、完整代码

Stack.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

//栈
typedef int STDataType;
typedef struct Stack
{
	STDataType* arr;
	int top;     
	int capacity;
}ST;

//初始化
void StackInit(ST* ps);
//销毁
void StackDestroy(ST* ps);

//入栈——栈顶
void StackPush(ST* ps, STDataType x);
//
bool StackEmpty(ST* ps);
//出栈
void StackPop(ST* ps);

//取栈顶数据
STDataType StackTop(ST* ps);
//有效元素个数
int StackSize(ST* ps);
Stack.c
#include"Stack.h"

//初始化
void StackInit(ST* ps)
{
	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}
//销毁
void StackDestroy(ST* ps)
{
	if (ps->arr)
		free(ps->arr);
	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}
//入栈——栈顶
void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
	ps->arr[ps->top++] = x;
}
//判断栈是否为空
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}
//出栈
void StackPop(ST* ps)
{
	assert(!StackEmpty(ps));
	--ps->top;
}
//取栈顶数据
STDataType StackTop(ST* ps)
{
	assert(!StackEmpty(ps));
	return ps->arr[ps->top - 1];
}
//有效元素个数
int StackSize(ST* ps)
{
	return ps->top;
}
test.c
#include"Stack.h"

void test01()
{
	ST st;
	StackInit(&st);
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);
	StackPush(&st, 5);
	//StackPop(&st);
	//StackPop(&st);
	//StackPop(&st);
	//StackPop(&st);
	//StackPop(&st);
	//while (!StackEmpty(&st))
	//{
	//	int top = StackTop(&st);
	//	printf("%d ", top);
	//	StackPop(&st);
	//}
	int size = StackSize(&st);
	printf("size:%d\n", size);

	StackDestroy(&st);
}

int main()
{
	test01(); //测试
	return 0;
}
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值