数据结构——栈和队列

呀哈喽,我是结衣。

今天我要讲的数据结构是栈和队列中的栈,提到栈你会想到什么呢?

所以下面我们要讲栈的基本概念。

栈的概念

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

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

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

用图像来表示就是这样的。

这样看的话就很直观了。

栈的实现

介绍完了就要进入正题了。

c 复制代码
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>


typedef int STDataType;

typedef struct Stack
{
	STDataType* a;
	int top;		// 标识栈顶位置的
	int capacity;
}ST;

void STInit(ST* pst);
void STDestroy(ST* pst);

// 栈顶插入删除
void STPush(ST* pst, STDataType x);
void STPop(ST* pst);
STDataType STTop(ST* pst);

bool STEmpty(ST* pst);
int STSize(ST* pst);

这里就直接贴代码了,下面详细讲解函数的实现。其实栈和顺序表差不多的。

函数的实现

初始化

首先是最经典的初始化

c 复制代码
void STInit(ST* pst)
{
	assert(pst);//防止结构体为空
	pst->a = NULL;
	pst->capacity = 0;
	pst->top = -1;

}

可能会有人对top= -1,有些疑问,如果你想赋值为0也是可以的,但是如果你把它赋值为0了,那么它所代表的意思就是栈顶元素的下一个了,为什么呢?

pst->a的本质是数组,首元素的下表是为0的,我们要利用top去访问首元素,未存放数据的时候,top就表示首元素就不太合适了,如果存放了数据也是如此,所以开始赋值0,就表示的栈顶元素的下一个。我们这里用的是-1来表示,如果你用0,在后续的代码做调整就可以了。

栈顶插入删除数据

插入

是不是很熟悉,就是和顺序表的插入相似。
顺序表

如果你还不会顺序表可以去看我的这一篇博客。

c 复制代码
void STPush(ST* pst, STDataType x)
{
	assert(pst);
	//判断空间是否足够
	if (pst->top + 1 == pst->capacity)
	{
		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			peeror("realloc fail");
			return;
		}
		pst->a = tmp;
		pst->capacity = newcapacity;
	}
	pst->top++;
	pst->a[pst->top] = x;
}

删除

删除超级简单。

c 复制代码
void STPop(ST* pst)
{
	assert(pst);
	pst->top--;
}

返回栈顶元素

记得加判断就可以了。

c 复制代码
STDataType STTop(ST* pst)
{
	assert(pst);
	assert(pst->top != -1);
	return pst->a[pst->top];
}

判断是否为空

c 复制代码
bool STEmpty(ST* pst)
{
	assert(pst);
	if (pst->top == -1)
	{
		return false;
	}
	return true;
}

返回有效数据的个数

c 复制代码
int STSize(ST* pst)
{
	assset(pst);
	return pst->top + 1;
}

这几个的都太简单了,看代码可以看出来的。就不解释了哈

测试

写完代码,我们来测试一下代码是否正确。

写一个test.c的文件来测试。

插入删除都没有问题,应该就是没有问题了。但是我们忘记写销毁函数了。现在写吧。

销毁

c 复制代码
void STDestroy(ST* pst)
{
	assert(pst);
	free(pst->a);
	pst->a = NULL;
	pst->capacity = 0;
	pst->top = -1;
}

记得free就可以了。

代码

stack.h

c 复制代码
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>


typedef int STDataType;

typedef struct Stack
{
	STDataType* a;
	int top;		// 标识栈顶位置的
	int capacity;
}ST;

void STInit(ST* pst);
void STDestroy(ST* pst);

// 栈顶插入删除
void STPush(ST* pst, STDataType x);
void STPop(ST* pst);
STDataType STTop(ST* pst);

bool STEmpty(ST* pst);
int STSize(ST* pst);

stack.c

c 复制代码
#include "stack.h"

void STInit(ST* pst)
{
	assert(pst);//防止结构体为空
	pst->a = NULL;
	pst->capacity = 0;
	pst->top = -1;

}

// 栈顶插入删除
void STPush(ST* pst, STDataType x)
{
	assert(pst);
	//判断空间是否足够
	if (pst->top + 1 == pst->capacity)
	{
		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		pst->a = tmp;
		pst->capacity = newcapacity;
	}
	pst->top++;
	pst->a[pst->top] = x;
}
void STPop(ST* pst)
{
	assert(pst);
	pst->top--;
}

STDataType STTop(ST* pst)
{
	assert(pst);
	assert(pst->top != -1);
	return pst->a[pst->top];
}

bool STEmpty(ST* pst)
{
	assert(pst);
	if (pst->top == -1)
	{
		return false;
	}
	return true;
}
int STSize(ST* pst)
{
	assert(pst);
	return pst->top + 1;
}
void STDestroy(ST* pst)
{
	assert(pst);
	free(pst->a);
	pst->a = NULL;
	pst->capacity = 0;
	pst->top = -1;
}

test.c

c 复制代码
#include "stack.h"

void test1()
{
	ST s;
	STInit(&s);
	STPush(&s, 1);
	STPush(&s, 2);
	STPush(&s, 3);
	//打印
	while (s.top+1)
	{
		printf("%d ",STTop(&s));
		STPop(&s);
	}
	STDestroy(&s);
}

int main()
{
	test1();

	return 0;
}


相关推荐
liangbm39 分钟前
MATLAB系列09:图形句柄
图像处理·笔记·计算机视觉·matlab·matlab绘图·工程基础·图形句柄
David猪大卫10 分钟前
数据结构修炼——顺序表和链表的区别与联系
c语言·数据结构·学习·算法·leetcode·链表·蓝桥杯
Iceberg_wWzZ12 分钟前
数据结构(Day14)
linux·c语言·数据结构·算法
微尘814 分钟前
C语言存储类型 auto,register,static,extern
服务器·c语言·开发语言·c++·后端
liangbm320 分钟前
MATLAB系列05:自定义函数
开发语言·笔记·matlab·教程·函数·自定义函数·按值传递
五味香1 小时前
C++学习,动态内存
java·c语言·开发语言·jvm·c++·学习·算法
Beauty.5681 小时前
P1328 [NOIP2014 提高组] 生活大爆炸版石头剪刀布
数据结构·c++·算法
Aurora20051 小时前
蓝桥杯2024省C
c语言·算法·蓝桥杯
爱棋笑谦1 小时前
二叉树计算
java·开发语言·数据结构·算法·华为od·面试
蟹至之1 小时前
字符函数 和 字符串函数 的使用与模拟
c语言·字符串·指针·const关键词