【数据结构】第四站:栈(附源码)


一、栈的介绍

前文言堆在(逻辑上)乃是在树型结构上加上重重限制的产物,那么今天要介绍的栈,便是在类似顺序表的结构上加限制,先看栈的标准解释:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。 可知,栈是对顺序表进行了一边封口不操作。
在这里插入图片描述

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

二、栈的实现

1.试分析栈的结构体

我们这里可以用链表实现栈,如果将链表尾作为栈顶的话,我们就需要保存尾节点的上一个节点,我们可以用指针,也可以用双向链表实现,但是这里我们用顺序表更优,因为顺序表的天然优势便是在尾处修改十分便利,但是它也同时有顺序表的缺点,当容量不足时需要扩容。

//typedef int STDatatype;
typedef struct Stack
{
	STDatatype* a;
	int top;
	int capacity;
}ST;

2.栈的初始化

//我的栈初始化
void STInit(ST* ps)
{
	assert(ps);
	ps->a = (STDatatype*)malloc(sizeof(STDatatype)*N);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	ps->capacity = N;
	ps->top = 0;//这里的top是栈顶元素的下一个位置,也是当前元素的个数。
}

3.栈的销毁

//我的栈的销毁
void STDestroy(ST* ps)
{
	assert(ps);
	//free(ps);
	//ps = NULL;
	free(ps->a);
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
	ps = NULL;
}

4.压栈

//我的入栈
void STPush(ST* ps, STDatatype x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		//扩容操作
		STDatatype* tmp = (STDatatype*)realloc(ps->a,sizeof(STDatatype)  * ps->capacity*2);
		if (tmp == NULL)
		{
			perror("realloc fail");
		}
		else
		{
			ps->a = tmp;
			ps->capacity *= 2;
		}
	}
	ps->a[ps->top++] = x;
}

5.出栈

void STPop(ST* ps)
{
	assert(ps);
	//assert(ps->top != 0);
	assert(!STEmpty(ps));
	ps->top--;
}

6.查看栈的大小

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

7.判断栈是否为空

bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}
//断言ps还是有必要的,就算栈是空的,它的指针也不会为空指针。

8.查看栈顶元素

STDatatype STTop(ST* ps)
{
	assert(ps);
	//assert(ps->top != 0);
	assert(!STEmpty(ps));
	return ps->a[ps->top - 1];
	
}

三、完整源码

stack.c

#define _CRT_SECURE_NO_WARNINGS 1


#include"stack.h"

//我的栈初始化
void STInit(ST* ps)
{
	assert(ps);
	ps->a = (STDatatype*)malloc(sizeof(STDatatype)*N);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	ps->capacity = N;
	ps->top = 0;//这里的top是栈顶元素的下一个位置,也是当前元素的个数。
}


//我的入栈
void STPush(ST* ps, STDatatype x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		//扩容操作
		STDatatype* tmp = (STDatatype*)realloc(ps->a,sizeof(STDatatype)  * ps->capacity*2);
		if (tmp == NULL)
		{
			perror("realloc fail");
		}
		else
		{
			ps->a = tmp;
			ps->capacity *= 2;
		}
	}
	ps->a[ps->top++] = x;
}



//我的出栈
void STPop(ST* ps)
{
	assert(ps);
	//assert(ps->top != 0);
	assert(!STEmpty(ps));
	ps->top--;
}



//我的查看栈顶元素
STDatatype STTop(ST* ps)
{
	assert(ps);
	//assert(ps->top != 0);
	assert(!STEmpty(ps));
	return ps->a[ps->top - 1];
	
}




//我的判断栈是否为空
bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}
//断言ps还是有必要的,就算栈是空的,它的指针也不会为空指针。



//我的查找栈的大小
int STSize(ST* ps)
{
	assert(ps);
	return ps->capacity;
}



//我的栈的销毁
void STDestroy(ST* ps)
{
	assert(ps);
	//free(ps);
	//ps = NULL;
	free(ps->a);
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
	ps = NULL;
}
//这里直接用栈的结构体指针进行释放是十分经典且新手的释放,写出该代码说明还是没有正确理解栈和顺序表的结构。

2.stack.h

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
#define  STDatatype int 
#define N 5


//typedef int STDatatype;
typedef struct Stack
{
	STDatatype* a;
	int top;
	int capacity;
}ST;


//栈初始化
void STInit(ST* ps);
//栈的销毁
void STDestroy(ST* ps);
//入栈
void STPush(ST* ps, STDatatype x);
//出栈
void STPop(ST* ps);
//查找栈的大小
int STSize(ST* ps);
//判断栈是否为空
bool STEmpty(ST* ps);
//查看栈顶元素
STDatatype STTop(ST* ps);

总结

这里有了顺序表的铺垫,实现应该会变得非常简单。

本文章为作者的笔记和心得记录,顺便进行知识分享,有任何错误请评论指点:)。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值