当前位置:网站首页>顺序栈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
边栏推荐
- Bytebase 加入阿裏雲 PolarDB 開源數據庫社區
- Win7 10 tips for installing Office2010 five solutions for installing MSXML components
- Drawing DEM with GEE gracefully
- unity之模糊背景(带你欣赏女人的朦胧美)
- Oracle create tablespaces and tables
- 两位湖南老乡,联手干出一个百亿IPO
- The largest DPU manufacturer in history (Part 1)
- Fundamentals of performance testing -- definitions of common terms
- 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
- 网络安全审查办公室对知网启动网络安全审查,称其“掌握大量重要数据及敏感信息”
猜你喜欢

网络安全审查办公室对知网启动网络安全审查,称其“掌握大量重要数据及敏感信息”

Capacitive inching touch switch module control (stm32f103c8t6)

Confirm whether the host is a large terminal or a small terminal

Byte and Tencent have also come to an end. How fragrant is this business of "making 30million yuan a month"?

Saltstack state state file configuration instance

基于SSM的物料管理系统(源码+文档+数据库)

What is CNN (convolutional neural network)

Microsoft Office Excel 2013 2016 graphic tutorial on how to enable macro function

LCD12864 (ST7565P) Chinese character display (STM32F103)

字节、腾讯也下场,这门「月赚3000万」的生意有多香?
随机推荐
Todesk remote control, detailed introduction and tutorial
[cann document express issue 06] first knowledge of tbe DSL operator development
基于SSM的物料管理系统(源码+文档+数据库)
SQL export CSV data, unlimited number of entries
Bytebase加入阿里云PolarDB开源数据库社区
Vxlan and MPLS: from data center to Metro Ethernet
消息称腾讯正式宣布成立“XR”部门,押注元宇宙;谷歌前 CEO:美国即将输掉芯片竞争,要让台积电、三星建更多工厂...
First understand redis' data structure - string
Power supply noise analysis
Methods for comparing float types in the kernel
Where are Xiaomi mobile phone's favorite SMS and how to delete them
【云驻共创】ModelBox隔空作画 绘制你的专属画作
Bat learning notes
Audio and video 2020 2021 2022 basic operation and parameter setting graphic tutorial
Download steps of STM32 firmware library
Eureka source code shallow reading - automatic fault removal
物联网?快来看 Arduino 上云啦
Introduction: continuously update the self-study version of the learning manual for junior test development engineers
Huawei cloud modelarts has ranked first in China's machine learning public cloud service market for the fourth time!
How does the video platform import the old database into the new database?