Stack 栈 -- C语言实现
栈
栈的概念
一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端
称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
- 压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
- 出栈:栈的删除操作叫做出栈。出数据也在栈顶。
顺序栈代码实现
Stack.h
#define _CRT_SECURE_NO_WARNINGS 1 #pragma once #include<stdio.h> #include<stdlib.h> #include<assert.h> #include<stdbool.h> //顺序表实现 typedef int STDataType; typedef struct Stack { STDataType *a; int top; //栈顶位置 int capacity; //栈容量 }Stack;//缩写ST //都用一级指针实现 //对栈元素初始化 void StackInit(Stack *ps); void StackDestroy(Stack *ps); void StackPush(Stack *ps, STDataType x); void StackPop(Stack *ps); STDataType StackTop(Stack *ps); bool StackEmpty(Stack *ps); int StackSize(Stack *ps);
Stack.c
#include"Stack.h" void StackInit(Stack *ps) { assert(ps); ps->a = NULL; ps->top = 0; ps->capacity = 0; } void StackDestroy(Stack *ps) { assert(ps); if (ps->a)//非空,有空间占用再释放 { free(ps->a); } ps->top = 0; ps->capacity = 0; ps->a = NULL; } void StackPush(Stack *ps,STDataType x) { assert(ps); if (ps->top == ps->capacity) { int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity; STDataType *tmp = (STDataType *)realloc(ps->a, newcapacity*sizeof(STDataType)); if (tmp == NULL) { perror("realloc fail"); exit(-1); } ps->capacity = newcapacity; ps->a = tmp; } ps->a[ps->top++] = x; //ps->top++; } void StackPop(Stack *ps) { assert(ps); assert(!StackEmpty(ps)); ps->top--; } STDataType StackTop(Stack *ps) { assert(ps); assert(!StackEmpty(ps)); return ps->a[ps->top - 1]; } bool StackEmpty(Stack *ps) { assert(ps); return ps->top == 0;//真(非零):返回tree。假(零):返回false } int StackSize(Stack *ps) { assert(ps); return ps->top; }
本文来自博客园,作者:HJfjfK,原文链接:https://www.cnblogs.com/DSCL-ing/p/18343985
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了