当前位置:网站首页>顺序栈1.0版本
顺序栈1.0版本
2022-06-24 19:02:00 【悟空不买菜了】
之前做了一个顺序栈和链式栈,这里为什么说把顺序栈升级一下,在于我在写用顺序栈遍历二叉树的时候,出现了一个问题,看看之前我写的顺序栈里面的数据存储

这里面存放的是int 类型的元素,但是对于二叉树来说,我们需要遍历出复杂的数据结构类型

也就是把这个节点的地址传入到栈里面,所以我们必须修改栈里面的数据类型,以至于可以来存放任何的数据类型
话不多说,直接上代码:
seqstack1.0.h
#ifndef _SEQSTACK1_H_
#define _SEQSTACK1_H_
#define EMPTY_INDEX -1//代表栈李阿敏没有元素
#define MAX 100//定义栈空间大小
typedef void* element_type;
//定义栈的头部空间
typedef struct _t_seq_stack {
element_type arr[MAX];
int top_index;
}t_seq_stack;
//创建栈
t_seq_stack* create_stack();
//判断栈是否为空
int is_empty(t_seq_stack *stack);
//销毁栈
void destroy_stack(t_seq_stack *stack);
//清空栈
void make_empty(t_seq_stack *stack);
//出栈
void pop_stack(t_seq_stack *stack);
//入栈
void push_stack(t_seq_stack *stack,element_type value);
//拿到栈顶元素
element_type top_stack(t_seq_stack *stack);
#endif
seqstack1.0.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "seqstack1.0.h"
//创建栈
t_seq_stack* create_stack()
{
t_seq_stack* t_stack = (t_seq_stack*)malloc(sizeof(t_seq_stack));
if(t_stack != NULL) {
//初始这片数组空间
memset(t_stack->arr,0,sizeof(element_type));
//初始化角标
t_stack->top_index = EMPTY_INDEX;
}
return t_stack;
}
//判断栈是否为空
int is_empty(t_seq_stack *t_stack)
{
if(t_stack != NULL) {
return t_stack->top_index == -1 ? 1 : 0;
}
}
//销毁栈
void destroy_stack(t_seq_stack *t_stack)
{
if(t_stack != NULL) {
free(t_stack);
}
}
//清空栈
void make_empty(t_seq_stack *t_stack)
{
if(t_stack != NULL) {
t_stack->top_index = -1;
}
}
//出栈
void pop_stack(t_seq_stack *t_stack)
{
//出栈必须判定栈是否为空
if(t_stack != NULL && (t_stack->top_index != -1)) {
t_stack->top_index--;
}
}
//入栈
void push_stack(t_seq_stack *t_stack,element_type value)
{
//在进栈之前,看看栈空间够不够
if(t_stack != NULL && t_stack->top_index + 1 <= MAX) {
t_stack->arr[++t_stack->top_index] = value;
}
}
//拿到栈顶元素
element_type top_stack(t_seq_stack *t_stack)
{
if(t_stack != NULL) {
return t_stack->arr[t_stack->top_index];//拿到栈顶的数据
}
}
然后看测试程序
main.c
#include <stdio.h>
#include <stdlib.h>
#include "seqstack1.0.h"
int main()
{
//创建一个栈
t_seq_stack* t_stack = create_stack();
if(t_stack != NULL) {
printf("栈创建成功\n");
}
int num1 = 1;
int num2 = 2;
int num3 = 3;
//开始入栈
push_stack(t_stack,&num1);
push_stack(t_stack,&num2);
push_stack(t_stack,&num3);
printf("进栈顺序:1\n2\n3\n");
printf("栈里面的元素个数:%d\n",t_stack->top_index + 1);
//这里做一个循环操作
//不停的出栈,拿到栈顶打印
int i = 0;
for(;i < 3;i++) {
int* res = (int*)top_stack(t_stack);
printf("%d出栈了\n",*res);
pop_stack(t_stack);
}
return 0;
}
运行结果:

然后我们把这个顺序栈做成一个动态库
然后把这个库移到相应的位置就ok
边栏推荐
- 托管服务与SASE,纵享网络与安全融合 | 一期一会回顾
- 【CANN文档速递06期】初识TBE DSL算子开发
- Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
- Behind Tiantian Jianbao storm: tens of millions in arrears, APP shutdown, and the founder's premeditated plan to run away?
- “拯救”直播带货,一个董宇辉还不够
- It is said that Tencent officially announced the establishment of "XR" department to bet on yuanuniverse; Former CEO of Google: the United States is about to lose the chip competition. We should let T
- Jd.com: how does redis implement inventory deduction? How to prevent oversold?
- You can capture fingerprints with a mobile camera?! Accuracy comparable to signature and monogram, expert: you are aggravating discrimination
- Microsoft Office Excel 2013 2016 graphic tutorial on how to enable macro function
- What is showcase? What should showcase pay attention to?
猜你喜欢
思源笔记工具栏中的按钮名称变成了 undefined,有人遇到过吗?
![[go language questions] go from 0 to entry 4: advanced usage of slice, elementary review and introduction to map](/img/3a/db240deb4c66b219ef86f40d4c7b7d.png)
[go language questions] go from 0 to entry 4: advanced usage of slice, elementary review and introduction to map
![[go Language brossage] go from 0 to Getting started 4: Advanced use of slice, Primary Review and Map Getting started Learning](/img/3a/db240deb4c66b219ef86f40d4c7b7d.png)
[go Language brossage] go from 0 to Getting started 4: Advanced use of slice, Primary Review and Map Getting started Learning

Zadig + cave Iast: let safety dissolve in continuous delivery

【Go語言刷題篇】Go從0到入門4:切片的高級用法、初級複習與Map入門學習

Teach you how to cancel computer hibernation

1、 Downloading and installing appium

R for Data Science (note) -- data transformation (select basic use)

Steering gear control (stm32f103c8t6)

Capacitive inching touch switch module control (stm32f103c8t6)
随机推荐
Eureka source code shallow reading - automatic fault removal
Comparative analysis of arrayblockingqueue and linkedblockingqueue
Teach you how to cancel computer hibernation
gateway
What other data besides SHP data
Jd.com: how does redis implement inventory deduction? How to prevent oversold?
Capacitive inching touch switch module control (stm32f103c8t6)
Apple, Microsoft and Google will no longer fight each other. They will work together to do a big thing this year
Some ideas about chaos Engineering
Docker installing MySQL
To open the registry
Using dynamic time warping (DTW) to solve the similarity measurement of time series and the similarity identification analysis of pollution concentration in upstream and downstream rivers
It is said that Tencent officially announced the establishment of "XR" department to bet on yuanuniverse; Former CEO of Google: the United States is about to lose the chip competition. We should let T
Showcase是什么?Showcase需要注意什么?
Cooking business experience of young people: bloggers are busy selling classes and bringing goods, and the organization earns millions a month
Some small requirements for SQL Engine for domestic database manufacturers
16个优秀业务流程管理工具
Coinbase将推出首个针对个人投资者的加密衍生产品
R for Data Science (note) -- data transformation (select basic use)
Todesk remote control, detailed introduction and tutorial