当前位置:网站首页>简单实现栈的功能
简单实现栈的功能
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;
}
边栏推荐
- Using Google or tools to solve logical problems: Zebra problem
- Use steps of Charles' packet capturing
- Smart pointer shared_ PTR and unique_ ptr
- [introduction to AUTOSAR com 4.com service layer module]
- 钢结构复习题
- Analyze the pre integration of vio with less rigorous but logical mathematical theory
- Connaissance du matériel 1 - schéma et type d'interface (basé sur le tutoriel vidéo complet de l'exploitation du matériel de baiman)
- Using or tools to solve the path planning problem (TSP)
- [talent column] can't you use Apache dolphin scheduler? It takes a month to write the most comprehensive introductory teaching [2]
- 《高分子合成工艺》简答题答案
猜你喜欢

时间序列的数据分析(一):主要成分

Deep convolution generation countermeasure network

时间序列的数据分析(二):数据趋势的计算

钢结构基本原理全面详细总结

Using or tools to solve the path planning problem (TSP)

高电压技术复习资料

Analyze the pre integration of vio with less rigorous but logical mathematical theory

【AUTOSAR CanTP 1.学习UDS诊断的网络层协议】

钢结构基本原理复习

【Autosar CP通用 1.如何阅读Autosar官方文档】
随机推荐
【AUTOSAR CanDrive 2.了解通信Hoh、CanId与PduID的Mapping关系】
What technologies are used in pharmaceutical research and development in the field of life sciences? Cryoelectron microscope? Molecular simulation? IND?
ARM架构与编程6--重定位(基于百问网ARM架构与编程教程视频)
Opencv library installation path (don't open this)
嵌入式从入门到精通(入土)——超详细知识点分享2
单片机学习笔记1--资料下载、环境搭建(基于百问网STM32F103系列教程)
单片机学习笔记7--SysTick定时器(基于百问网STM32F103系列教程)
钢结构基本原理全面详细总结
3.2daydayup举一反三:三天打鱼两天晒网式学习
大小写字母转换
Upper and lower case letter conversion
Check the sandbox file in the real app
嵌入式从入门到精通(入土)——超详细知识点分享1
钢结构复习题
利用or-tools来求解带容量限制的路径规划问题(CVRP)
钢结构基本原理试题及答案
Enter the triangle side length and calculate the area
Using or tools to solve path planning problem (VRP)
#under指令
Using Google or tools to solve logical problems: Zebra problem