数据结构-栈的实现

一、栈的概念和结构

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

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

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

二、栈的实现

2.1 栈的结构

cpp 复制代码
// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;		// 栈顶
	int capacity;  // 容量 
}Stack;

2.2 栈的初始化StackInit

cpp 复制代码
// 初始化栈 
void StackInit(Stack* ps) {
	assert(ps);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}

2.3 入栈StackPush

cpp 复制代码
void CheckCapacity(Stack* ps) {
	if (ps->capacity == ps->top) {
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType));
		if (tmp == NULL) {
			perror("CheckCapacity()::realloc");
			exit(1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
}
// 入栈 
void StackPush(Stack* ps, STDataType data) {
	assert(ps);
	CheckCapacity(ps);
	ps->a[ps->top] = data;
	ps->top++;
}

2.4 出栈StackPop

cpp 复制代码
// 出栈 
void StackPop(Stack* ps) {
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}

2.5 取栈顶元素StackTop

cpp 复制代码
// 获取栈顶元素 
STDataType StackTop(Stack* ps) {
	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}

2.6 判空StackEmpty

cpp 复制代码
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps) {
	return ps->top == 0 ? 0 : 1;
}

2.7 获取栈中元素个数StackSize

cpp 复制代码
// 获取栈中有效元素个数 
int StackSize(Stack* ps) {
	return ps->top;
}

2.8 销毁StackDestroy

cpp 复制代码
// 销毁栈 
void StackDestroy(Stack* ps) {
	assert(ps);
	ps->capacity = ps->capacity = 0;
	free(ps->a);
	ps->a = NULL;
}

三、栈的应用

括号匹配问题:20. 有效的括号 - 力扣(LeetCode)

四、代码

4.1 Stack.h

cpp 复制代码
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;		// 栈顶
	int capacity;  // 容量 
}Stack;
// 初始化栈 
void StackInit(Stack* ps);
// 入栈 
void StackPush(Stack* ps, STDataType data);
// 出栈 
void StackPop(Stack* ps);
// 获取栈顶元素 
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数 
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps);
// 销毁栈 
void StackDestroy(Stack* ps);

4.2 Stack.c

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
// 初始化栈 
void StackInit(Stack* ps) {
	assert(ps);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}
// 销毁栈 
void StackDestroy(Stack* ps) {
	assert(ps);
	ps->capacity = ps->capacity = 0;
	free(ps->a);
	ps->a = NULL;
}
void CheckCapacity(Stack* ps) {
	if (ps->capacity == ps->top) {
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType));
		if (tmp == NULL) {
			perror("CheckCapacity()::realloc");
			exit(1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
}
// 入栈 
void StackPush(Stack* ps, STDataType data) {
	assert(ps);
	CheckCapacity(ps);
	ps->a[ps->top] = data;
	ps->top++;
}
// 出栈 
void StackPop(Stack* ps) {
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}
// 获取栈顶元素 
STDataType StackTop(Stack* ps) {
	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}
// 获取栈中有效元素个数 
int StackSize(Stack* ps) {
	return ps->top;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps) {
	return ps->top == 0 ? 0 : 1;
}

4.3 test.c

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
void test01()
{
	Stack st = { 0 };
	StackInit(&st);
	StackPush(&st, 1);//测试入栈
	StackPush(&st, 2);//测试入栈
	StackPush(&st, 3);//测试入栈
	StackPush(&st, 4);//测试入栈
	StackPush(&st, 5);//测试入栈
	int count = StackSize(&st);//测试获取栈中有效元素个数
	printf("%d\n", count);
	while (StackEmpty(&st)) {//检测栈是否为空,如果为空返回非零结果,如果不为空返回0
		printf("%d ", StackTop(&st));//测试取栈顶元素
		StackPop(&st);//测试出栈
	}
	StackDestroy(&st);

}

int main() {
	test01();//测试
	return 0;
}
相关推荐
智购科技无人售货机工厂店4 小时前
玻璃瓶饮料破损率突然上升,排查发现是取货口缓冲垫老化了~YH
java·开发语言·人工智能·python·eclipse
0+1114 小时前
算法 --归并排序
数据结构·算法·排序算法
枫叶丹44 小时前
语音 AI 怎样边听边答:实时对话系统的工作原理
开发语言·人工智能·chatgpt·开源·php·agent·codex
xie0510_4 小时前
Any类简要实现
开发语言·c++·算法
小狼Solar4 小时前
SAP MDG 功能范围说明(基于S/4HANA 2025)
java·开发语言
kimnoic4 小时前
Python常用标准库模块及查询使用方法
开发语言·python
字节跳动的猫4 小时前
LikeShop 种草社区模块二开:笔记发布、话题广场与商品挂载实现
数据结构·数据库
知识分享小能手4 小时前
C学习教程,从入门到精通,C语言输入和输出(4)
c语言·开发语言·学习
道尔柯南4 小时前
C++ 入门基础详解:从第一个程序到核心语法特性
开发语言·c++
盛世宏博智慧档案4 小时前
Modbus TCP+PoE以太网温湿度传感器项目部署全流程,附网络调试实操
开发语言·tcp/ip·php·以太网·传感器·温湿度