当前位置:网站首页>简单实现栈的功能
简单实现栈的功能
2022-07-23 05:44:00 【潜水少年请求出战】
**
栈的特点
**
先进后出或者是后进先出
**
代码实现
**
.h文件
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int STDataType;
typedef struct STack
{
STDataType* data;
int top;
int capacity;
}ST;
//初始化
void StackInit(ST* ps);
//插入
void StackPush(ST* ps, STDataType x);
//删除
void StackPop(ST* ps);
//取出
STDataType StackTop(ST* ps);
//销毁
void StackDestroy(ST* ps);
//长度
int StackSize(ST* ps);
//判断空
bool StackEmpty(ST* ps);
.c文件
#include"Stack.h"
//初始化
void StackInit(ST* ps)
{
assert(ps);
ps->top = ps->capacity = 0;
ps->data = NULL;
}
//插入
void StackPush(ST* ps, STDataType x)
{
assert(ps);
//判断是否扩容
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity ? (ps->capacity) * 2 : 3;
STDataType* newdata = (STDataType*)realloc(ps->data, sizeof(STDataType) * newcapacity);
if (newdata == NULL)
{
printf("newdata::file");
exit(-1);
}
ps->data = newdata;
ps->capacity = newcapacity;
}
ps->data[ps->top] = x;
ps->top += 1;
}
//判断空
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
//删除
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top -= 1;
}
//取出
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->data[ps->top - 1];
}
//销毁
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->data);
ps->data = NULL;
ps->capacity=ps->top = 0;
}
//长度
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
#include"Stack.h"
void tectStack()
{
ST st;
StackInit(&st);
StackPush(&st, 1);
StackPush(&st, 2);
StackPush(&st, 3);
StackPush(&st, 4);
printf("%d ", StackTop(&st));
StackPop(&st);
printf("%d ", StackTop(&st));
printf("\n打印\n");
while (!StackEmpty(&st))
{
printf("%d ", StackTop(&st));
StackPop(&st);
}
printf("\n");
StackDestroy(&st);
}
int main()
{
tectStack();
return 0;
}
边栏推荐
- ARM架构与编程3--按键控制LED(基于百问网ARM架构与编程教程视频)
- obs插件基础
- 硬件知識1--原理圖和接口類型(基於百問網硬件操作大全視頻教程)
- Data analysis of time series (I): main components
- Data analysis of time series (III): decomposition of classical time series
- Use pyod to detect outliers
- 嵌入式从入门到精通(入土)——超详细知识点分享1
- 输入三角形边长,求面积
- [AUTOSAR CP general 1. how to read AUTOSAR official documents]
- 博客搭建四:将自己的博客加入百度和谷歌收录的方法
猜你喜欢
随机推荐
Six trends and eight technologies of high-performance computing in data centers under "data center white paper 2022" and "computing from the east to the west"
单片机学习笔记1--资料下载、环境搭建(基于百问网STM32F103系列教程)
Using pycaret: low code, automated machine learning framework to solve regression problems
博客搭建二:NexT主题相关设置beta
【Autosar CP通用 1.如何阅读Autosar官方文档】
输入三角形边长,求面积
#under指令
C语言中,对柔性数组的理解
博客搭建一:框架选择
ARM架构与编程6--重定位(基于百问网ARM架构与编程教程视频)
Questions and answers of basic principles of steel structure
【基于UDS服务的BootLoader架构和刷写流程】
Hardware knowledge 1 -- Schematic diagram and interface type (based on Baiwen hardware operation Daquan video tutorial)
Knowledge structure of advanced algebra
Opencv library installation path (don't open this)
ARM架构与编程1--LED闪烁(基于百问网ARM架构与编程教程视频)
(1)ASIO
【AUTOSAR COM 3.信号的收发流程TX/RX】
钢结构基本原理复习
Baidu Shen Shuo: focus on the scene, deeply cultivate the industry, and bring practical results to enterprise Digitalization








