[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;
}
相关推荐
鹿鹿学长13 小时前
2025年全国大学生数学建模竞赛(C题) 建模解析|婴儿染色体数学建模|小鹿学长带队指引全代码文章与思路
c语言·开发语言·数学建模
好家伙VCC13 小时前
数学建模模型 全网最全 数学建模常见算法汇总 含代码分析讲解
大数据·嵌入式硬件·算法·数学建模
伴杯猫13 小时前
【ESP32-IDF】基础外设开发2:系统中断矩阵
c语言·单片机·嵌入式硬件·mcu·物联网·github
liulilittle15 小时前
IP校验和算法:从网络协议到SIMD深度优化
网络·c++·网络协议·tcp/ip·算法·ip·通信
Want59515 小时前
C/C++圣诞树①
c语言·开发语言·c++
bkspiderx16 小时前
C++经典的数据结构与算法之经典算法思想:贪心算法(Greedy)
数据结构·c++·算法·贪心算法
l1t17 小时前
轻量级XML读写库Mini-XML的编译和使用
xml·c语言·解析器
中华小当家呐17 小时前
算法之常见八大排序
数据结构·算法·排序算法
沐怡旸18 小时前
【算法--链表】114.二叉树展开为链表--通俗讲解
算法·面试
tju新生代魔迷18 小时前
数据结构:双向链表
数据结构·链表