数据结构——栈和队列

呀哈喽,我是结衣。

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

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

栈的概念

栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。**进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出 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;
}


相关推荐
RuoZoe6 分钟前
从 2026 年 3 月 1 日开源,到 26.10.9:Jalium UI 半年时间到底走了多远?
c语言·c++
aqiu11111113 分钟前
【LeetCode 902】最大为 N 的数字组合 - 详细题解与数位组合思路
数据结构·算法·leetcode·蓝桥杯·数位dp
鱼子星_1 小时前
【C++】继承和多态(上)
c++·笔记
xian_wwq2 小时前
【学习笔记】深度认知系列-第14讲 端侧AI崛起——为什么AI正在从云端走向本地
人工智能·笔记·学习
知无不研3 小时前
c语言中循环的介绍与简单应用
c语言·开发语言·算法·循环·for·while
策案案案3 小时前
YOLO: 将AI Agents嵌入到IntelliJ IDEA
java·大数据·人工智能·笔记·yolo·microsoft·intellij-idea
HRTOS4 小时前
HRTOS 4.0 驱动库:06_Storage 存储设备驱动——24C02 EEPROM与I²C驱动开发
c语言·驱动开发·嵌入式硬件·51单片机
2601_962297484 小时前
在python3中、下列输出变量a的正确写法是_2020超星大数据Python免费答案
数据结构·python·算法·编程·字符串操作
shehuiyuelaiyuehao5 小时前
算法29,前缀和,除自身以外的数组的乘积
java·数据结构·算法
啵啵啵鱼6 小时前
DS-顺序表
java·数据结构·笔记·后端·链表·obsidian