[C语言]栈的实现

一、栈的基本概念

栈是一种遵循后进先出(Last In First Out,LIFO)原则的数据结构,想象它就像一个只能在一端进行操作的容器,这一端被称为栈顶(Top)。就好比往一个竖着的桶里放东西,最后放进去的东西要最先被取出来。例如,我们将元素依次压入栈中,那么取元素的时候,最先取出的一定是最后压入栈的那个元素。

二、基于数组实现栈

在c语言中实现栈的方式主要有两种,一种是数组,一种是链表;两种方法各有优缺点,下面是使用数组的方式来实现。

1.定义栈的结构

cpp 复制代码
typedef int STDataType;
typedef struct StackNode
{
    STDataType* arr;
    int capacity;//栈的空间大小
    int top;//栈顶
}ST;

2.栈的初始化

cpp 复制代码
void STInit(ST* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}

3.入栈

cpp 复制代码
void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->capacity == ps->top)//判断空间是否足够
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->arr, newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("relloc fail!!!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}

4.判断元素个数是否为空

cpp 复制代码
bool StackEmpty(ST* ps)
{
    assert(ps);
    return ps->top == 0;
}

5.出栈

cpp 复制代码
void StackPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	--ps->top;
}

6.取栈顶元素

cpp 复制代码
STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->arr[ps->top-1];
}

7.获取栈的有效元素个数

cpp 复制代码
int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

8.销毁栈

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

三.栈的完整代码

Stack.h

cpp 复制代码
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

//定义栈的结构
typedef int STDataType;
typedef struct StackNode
{
	STDataType* arr;
	int capacity;//栈的空间大小
	int top;//栈顶
}ST;

//栈的初始化
void STInit(ST* ps);
//入栈
void StackPush(ST* ps, STDataType x);
//出栈
void StackPop(ST* ps);
//取栈顶元素
STDataType StackTop(ST* ps);
//获取栈的有效元素个数
int STSize(ST* ps);
//销毁栈
void STDestroy(ST* ps);

stack.c

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

//栈的初始化
void STInit(ST* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}
//入栈
void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->capacity == ps->top)//判断空间是否足够
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->arr, newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("relloc fail!!!");
			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;
}
//取栈顶元素
STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->arr[ps->top-1];
}
//获取栈的有效元素个数
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;
}

text.c

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

void text()
{
	ST st;
	STInit(& st);
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);
	printf("%d\n", StackTop(&st));
	StackPop(&st);

	printf("%d\n", STSize(&st));

	STDestroy(&st);
}

int main()
{
	text();
	return 0;
}
相关推荐
姜不吃葱6 分钟前
【力扣热题100】哈希——两数之和
算法·leetcode·哈希算法·力扣热题100
符生落华12 分钟前
c语言:插入排序,二维数组
c语言
AI4Sci.16 分钟前
在云服务器上基于lora微调Qwen2.5-VL-7b-Instruct模型(下)
人工智能·算法·机器学习·大模型·lora微调·大模型本地部署·qwen2.5-vl-7b
一只小风华~17 分钟前
JavaScript:数组常用操作方法的总结表格
前端·javascript·数据结构·vue.js·算法
TiAmo zhang42 分钟前
深度学习与图像处理 | 基于PaddlePaddle的梯度下降算法实现(线性回归投资预测)
图像处理·深度学习·算法
一匹电信狗1 小时前
【C++】手搓一个STL风格的vector容器
c语言·数据结构·c++·算法·leetcode·stl·visual studio
生信探索1 小时前
SeuratExtend 可视化教程(1):单细胞分析的高颜值绘图指南
算法
小小小白的编程日记1 小时前
C语言中的数据结构--栈和队列(2)
c语言·数据结构
李永奉1 小时前
C语言-数组:数组(定义、初始化、元素的访问、遍历)内存和内存地址、数组的查找算法和排序算法;
c语言·算法·排序算法
星辰大海的精灵1 小时前
深入解析 CopyOnWriteArrayList
java·后端·算法