当前位置:网站首页>Sequence stack version 1.0
Sequence stack version 1.0
2022-06-24 20:28:00 【Wukong stopped buying vegetables】
I made a sequence stack and chain stack before , Why do we say to upgrade the sequence stack , When I was writing to traverse a binary tree with a sequence stack , There's a problem , Look at the data storage in the sequence stack I wrote before

What's in it is int Element of type , But for binary trees , We need to iterate over complex data structure types

That is, the address of this node is passed into the stack , So we have to change the data type in the stack , So that you can store any data type
Don't talk much , Go straight to the code :
seqstack1.0.h
#ifndef _SEQSTACK1_H_
#define _SEQSTACK1_H_
#define EMPTY_INDEX -1// On behalf of the stack, Li Amin has no elements
#define MAX 100// Define stack space size
typedef void* element_type;
// Define the header space of the stack
typedef struct _t_seq_stack {
element_type arr[MAX];
int top_index;
}t_seq_stack;
// Create a stack
t_seq_stack* create_stack();
// Judge whether the stack is empty
int is_empty(t_seq_stack *stack);
// Destroy the stack
void destroy_stack(t_seq_stack *stack);
// Empty stack
void make_empty(t_seq_stack *stack);
// Out of the stack
void pop_stack(t_seq_stack *stack);
// Push
void push_stack(t_seq_stack *stack,element_type value);
// Get to the top of the stack
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"
// Create a stack
t_seq_stack* create_stack()
{
t_seq_stack* t_stack = (t_seq_stack*)malloc(sizeof(t_seq_stack));
if(t_stack != NULL) {
// The initial array space
memset(t_stack->arr,0,sizeof(element_type));
// Initialize the subscript
t_stack->top_index = EMPTY_INDEX;
}
return t_stack;
}
// Judge whether the stack is empty
int is_empty(t_seq_stack *t_stack)
{
if(t_stack != NULL) {
return t_stack->top_index == -1 ? 1 : 0;
}
}
// Destroy the stack
void destroy_stack(t_seq_stack *t_stack)
{
if(t_stack != NULL) {
free(t_stack);
}
}
// Empty stack
void make_empty(t_seq_stack *t_stack)
{
if(t_stack != NULL) {
t_stack->top_index = -1;
}
}
// Out of the stack
void pop_stack(t_seq_stack *t_stack)
{
// Whether the stack is empty must be determined when the stack is out
if(t_stack != NULL && (t_stack->top_index != -1)) {
t_stack->top_index--;
}
}
// Push
void push_stack(t_seq_stack *t_stack,element_type value)
{
// Before entering the stack , See if the stack space is enough
if(t_stack != NULL && t_stack->top_index + 1 <= MAX) {
t_stack->arr[++t_stack->top_index] = value;
}
}
// Get to the top of the stack
element_type top_stack(t_seq_stack *t_stack)
{
if(t_stack != NULL) {
return t_stack->arr[t_stack->top_index];// Get the data at the top of the stack
}
}
Then look at the test program
main.c
#include <stdio.h>
#include <stdlib.h>
#include "seqstack1.0.h"
int main()
{
// Create a stack
t_seq_stack* t_stack = create_stack();
if(t_stack != NULL) {
printf(" Stack created successfully \n");
}
int num1 = 1;
int num2 = 2;
int num3 = 3;
// Start stacking
push_stack(t_stack,&num1);
push_stack(t_stack,&num2);
push_stack(t_stack,&num3);
printf(" Stack order :1\n2\n3\n");
printf(" The number of elements in the stack :%d\n",t_stack->top_index + 1);
// Here is a loop operation
// Keep coming out of the stack , Get to the top of the stack and print
int i = 0;
for(;i < 3;i++) {
int* res = (int*)top_stack(t_stack);
printf("%d Out of the stack \n",*res);
pop_stack(t_stack);
}
return 0;
}
Running results :

Then we make this sequence stack into a dynamic library
Then move the library to the corresponding location ok
边栏推荐
- Information theory of popular science Shannon
- To open the registry
- Popupwindow touch event transparent transmission scheme
- Fuzzy background of unity (take you to appreciate the hazy beauty of women)
- Apache+php+mysql environment construction is super detailed!!!
- Unit actual combat lol skill release range
- Huawei cloud modelarts has ranked first in China's machine learning public cloud service market for the fourth time!
- gateway
- 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
- redis数据结构之压缩列表
猜你喜欢

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

使用gorm查询数据库时reflect: reflect.flag.mustBeAssignable using unaddressable value

两位湖南老乡,联手干出一个百亿IPO

顺序栈1.0版本

字节、腾讯也下场,这门「月赚3000万」的生意有多香?

Map跟object 的区别

To open the registry

云计算发展的 4 个阶段,终于有人讲明白了

Redis installation of CentOS system under Linux, adding, querying, deleting, and querying all keys

Where are Xiaomi mobile phone's favorite SMS and how to delete them
随机推荐
unity实战之lol技能释放范围
Bytebase joins Alibaba cloud polardb open source database community
【CANN文档速递06期】初识TBE DSL算子开发
What is CNN (convolutional neural network)
Kubernetes cluster deployment
Otaku can't save yuan universe
托管服务与SASE,纵享网络与安全融合 | 一期一会回顾
IP address to integer
Nodered has no return value after successfully inserting into the database (the request cannot be ended)
Apple, Microsoft and Google will no longer fight each other. They will work together to do a big thing this year
云计算发展的 4 个阶段,终于有人讲明白了
Compressed list of redis data structures
Microsoft Office Excel 2013 2016 graphic tutorial on how to enable macro function
得物多活架构设计之路由服务设计
Introduction: continuously update the self-study version of the learning manual for junior test development engineers
[cloud resident co creation] ModelBox draws your own painting across the air
Confirm whether the host is a large terminal or a small terminal
Dongyuhui is not enough to bring goods to "rescue" live broadcast
Open programmable infrastructure (OPI) project, redefining dpu/ipu
What is showcase? What should showcase pay attention to?