数据结构之栈的实现


引入

在学完顺序表和链表之后,还有一种重要的数据结构,本文将详细介绍栈的实现,包括栈的初始化,入栈,出栈,销毁等一系列关于栈的操作

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

栈的底层结构

了解完栈的基本概念之后,我们知道栈也是一种线性表,那么栈的底层逻辑应该用顺序表来实现还是用链表来实现呢?
假如说我们用链表来实现栈,那么每次入栈都要进行增容操作,时间复杂度大大增加,并且如果用链表作为实现栈的底层结构的话,没有发挥出链表的优点,即插入和删除较为方便,但是在栈这一数据结构中,是不存在遍历,指定位置删除,指定位置插入这一说法的,因此我们使用顺序表作为栈的底层结构

定义

typedef int DataType;
typedef struct Stack
{
	DataType* arr;
	int capacity;
	int top;
}ST;

capacity表示栈的空间,top表示栈顶,是栈中的有效数据的个数,相当于顺序表中的size

初始化

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

这里要进行传址操作,因为要修改栈的值

入栈

void StackPush(ST* ps, DataType x)
{
	assert(ps);
	//判断空间是否充足
	if (ps->capacity == ps->top)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		DataType* tmp = (DataType*)realloc(ps->arr,sizeof(DataType)*newcapacity);
		if (tmp == NULL)
		{
			perror("realloc");
			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(ps);
	assert(!StackEmpty(ps));
	ps->top--;
}

直接让top- -即可

取栈顶元素

DataType StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->top;
}

打印栈中内容的时候会用到

获取栈中有效数据的个数

int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

用于观测此时栈中的元素个数

销毁

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

源码

Stack.h

#define  _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int DataType;
typedef struct Stack
{
	DataType* arr;
	int capacity;
	int top;
}ST;

//初始化
void STInit(ST* ps);

//销毁
void STDestroy(ST* ps);

//入栈
void StackPush(ST* ps, DataType x);

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

//判空
bool StackEmpty(ST* ps);

//取栈顶元素
DataType StackTop(ST* ps);

//获取栈中有效元素的个数
int STSize(ST* ps);

Stack.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

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

void StackPush(ST* ps, DataType x)
{
	assert(ps);
	//判断空间是否充足
	if (ps->capacity == ps->top)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		DataType* tmp = (DataType*)realloc(ps->arr, sizeof(DataType)*newcapacity);
		if (tmp == NULL)
		{
			perror("realloc");
			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(ps);
	assert(!StackEmpty(ps));
	ps->top--;
}

DataType StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->top;
}

int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

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

test.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

void stacktest()
{
	ST st;
	STInit(&st);
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);
	StackPush(&st, 5);

	printf("size->%d\n", STSize(&st));
	while (!StackEmpty(&st))
	{
		DataType data = StackTop(&st);
		printf("%d ", data);
		StackPop(&st);
	}
	printf("\n");
	printf("size->%d\n", STSize(&st));

	STDestroy(&st);
}

int main()
{
	stacktest();
	return 0;
}

总结

以上是数据结构中栈的相关知识,我们要像捡贝壳一样捡到自己的小篮子里哦,有不明白的地方欢迎留言,作者水平有限,文章不妥部分还请各位读者指正。

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值