当前位置:网站首页>C language simulates entering and leaving the stack, first in first out, first in first out, shared memory
C language simulates entering and leaving the stack, first in first out, first in first out, shared memory
2022-04-23 18:22:00 【Things will turn when they reach the extreme 1024】
This program can be used for serial communication cache, etc , The cause is UCOS Your notice didn't achieve the expected effect .
Routines include basic digital type and practical structure type .
The main body of this program consists of a structure and three functions :
A structure
Memory and current stack data length
typedef struct Stack
{
int Data[Data_Len];
int P_Active; // Current stack length
} My_Stack_type;
Three functions
The first function : Into the stack
int Stack_Push(My_Stack_type *Stack_Active,int Data) // Into the stack
From memory array 0 The element begins to save , Cumulative storage .
Data Num:0 Data:
Stack successful 0=0
Data Num:0 Data:
Stack successful 1=10
Data Num:1 Data:0
Stack successful 2=20
Data Num:2 Data:0 10
Stack successful 3=30
Data Num:3 Data:0 10 20
Stack successful 4=40
Data Num:4 Data:0 10 20 30
Stack successful 5=50
Data Num:5 Data:0 10 20 30 40
Stack successful 6=60
Data Num:6 Data:0 10 20 30 40 50
Stack successful 7=70
Data Num:7 Data:0 10 20 30 40 50 60
Stack successful 8=80
Data Num:8 Data:0 10 20 30 40 50 60 70
Stack successful 9=90
Data Num:9 Data:0 10 20 30 40 50 60 70 80
Stack failed , Excessive data
Stack failed , Excessive data
The second function : First in and then out of the stack
int Stack_FILO_Pop(My_Stack_type *Stack_Active,int *Data) // First in and then out of the stack
Start with the last number in the memory array
Data Num:10 Data:0 10 20 30 40 50 60 70 80 90
Stack out successfully 9=90
Data Num:9 Data:0 10 20 30 40 50 60 70 80
Stack out successfully 8=80
Data Num:8 Data:0 10 20 30 40 50 60 70
Stack out successfully 7=70
The third function : First in, first out
int Stack_FIFO_Pop(My_Stack_type *Stack_Active,int *Data) // First in, first out
Start with the first data you put in , And move the data put in later forward
Data Num:7 Data:0 10 20 30 40 50 60
Stack out successfully 0
Data Num:6 Data:10 20 30 40 50 60
Stack out successfully 10
Data Num:5 Data:20 30 40 50 60
Stack out successfully 20
Data Num:4 Data:30 40 50 60
Complete routine
#include <stdio.h>
#define Debug_En 0
#define Data_Len 10
typedef struct Stack
{
int Data[Data_Len];
int P_Active; // Current stack length
} My_Stack_type;
My_Stack_type My_Stack;
int Stack_Push(My_Stack_type *Stack_Active,int Data) // Into the stack
{
if(Stack_Active->P_Active<Data_Len)
{
Stack_Active->Data[Stack_Active->P_Active]=Data;
#if Debug_En
printf(" Stack successful %d=%d\r\n",Stack_Active->P_Active,Stack_Active->Data[Stack_Active->P_Active]);
Stack_Show(Stack_Active);
#endif
Stack_Active->P_Active++;
return 0;
}else{
#if Debug_En
printf(" Stack failed , Excessive data \r\n");
#endif
return 1; // Excessive data
}
}
int Stack_FILO_Pop(My_Stack_type *Stack_Active,int *Data) // First in and then out of the stack
{
if(Stack_Active->P_Active)
{
Stack_Active->P_Active--;
*Data=Stack_Active->Data[Stack_Active->P_Active];
#if Debug_En
printf(" Stack out successfully %d=%d\r\n",Stack_Active->P_Active,*Data);
Stack_Show(Stack_Active);
#endif
return 0;
}else{
printf(" Stack out failed , Data is empty \r\n");
return 1; // Excessive data
}
}
int Stack_FIFO_Pop(My_Stack_type *Stack_Active,int *Data) // First in, first out
{
int i=0;
if(Stack_Active->P_Active)
{
*Data=Stack_Active->Data[0];
#if Debug_En
printf(" Stack out successfully %d\r\n",*Data);
#endif
for(i=0;i<Stack_Active->P_Active;i++)
{
Stack_Active->Data[i]=Stack_Active->Data[i+1];
}
Stack_Active->P_Active--;
#if Debug_En
Stack_Show(Stack_Active);
#endif
return 0;
}else{
#if Debug_En
printf(" Stack out failed , Data is empty \r\n");
#endif
return 1; // Excessive data
}
}
int Stack_Show(My_Stack_type *Stack_Active)// Print memory
{
int i=0;
printf("Data Num:%d Data:",Stack_Active->P_Active);
for(i=0;i<Stack_Active->P_Active;i++)
{
printf("%d ",Stack_Active->Data[i]);
}
printf("\r\n");
}
int main(int argc, char *argv[])
{
int i=0;
int Data;
Stack_Show(&My_Stack);
for(i=0;i<12;i++)
{
Stack_Push(&My_Stack,i*10);
}
Stack_Show(&My_Stack);
for(i=0;i<3;i++)
{
Stack_FILO_Pop(&My_Stack,&Data);
}
Stack_Show(&My_Stack);
for(i=0;i<3;i++)
{
Stack_FIFO_Pop(&My_Stack,&Data);
}
Stack_Show(&My_Stack);
for(i=0;i<12;i++)
{
Stack_Push(&My_Stack,i*10);
}
Stack_Show(&My_Stack);
for(i=0;i<3;i++)
{
Stack_FILO_Pop(&My_Stack,&Data);
}
Stack_Show(&My_Stack);
for(i=0;i<20;i++)
{
Stack_FIFO_Pop(&My_Stack,&Data);
}
Stack_Show(&My_Stack);
return 0;
}
Screenshot of actual operation
Data Num:0 Data:
Stack successful 0=0
Data Num:0 Data:
Stack successful 1=10
Data Num:1 Data:0
Stack successful 2=20
Data Num:2 Data:0 10
Stack successful 3=30
Data Num:3 Data:0 10 20
Stack successful 4=40
Data Num:4 Data:0 10 20 30
Stack successful 5=50
Data Num:5 Data:0 10 20 30 40
Stack successful 6=60
Data Num:6 Data:0 10 20 30 40 50
Stack successful 7=70
Data Num:7 Data:0 10 20 30 40 50 60
Stack successful 8=80
Data Num:8 Data:0 10 20 30 40 50 60 70
Stack successful 9=90
Data Num:9 Data:0 10 20 30 40 50 60 70 80
Stack failed , Excessive data
Stack failed , Excessive data
Data Num:10 Data:0 10 20 30 40 50 60 70 80 90
Stack out successfully 9=90
Data Num:9 Data:0 10 20 30 40 50 60 70 80
Stack out successfully 8=80
Data Num:8 Data:0 10 20 30 40 50 60 70
Stack out successfully 7=70
Data Num:7 Data:0 10 20 30 40 50 60
Data Num:7 Data:0 10 20 30 40 50 60
Stack out successfully 0
Data Num:6 Data:10 20 30 40 50 60
Stack out successfully 10
Data Num:5 Data:20 30 40 50 60
Stack out successfully 20
Data Num:4 Data:30 40 50 60
Data Num:4 Data:30 40 50 60
Stack successful 4=0
Data Num:4 Data:30 40 50 60
Stack successful 5=10
Data Num:5 Data:30 40 50 60 0
Stack successful 6=20
Data Num:6 Data:30 40 50 60 0 10
Stack successful 7=30
Data Num:7 Data:30 40 50 60 0 10 20
Stack successful 8=40
Data Num:8 Data:30 40 50 60 0 10 20 30
Stack successful 9=50
Data Num:9 Data:30 40 50 60 0 10 20 30 40
Stack failed , Excessive data
Stack failed , Excessive data
Stack failed , Excessive data
Stack failed , Excessive data
Stack failed , Excessive data
Stack failed , Excessive data
Data Num:10 Data:30 40 50 60 0 10 20 30 40 50
Stack out successfully 9=50
Data Num:9 Data:30 40 50 60 0 10 20 30 40
Stack out successfully 8=40
Data Num:8 Data:30 40 50 60 0 10 20 30
Stack out successfully 7=30
Data Num:7 Data:30 40 50 60 0 10 20
Data Num:7 Data:30 40 50 60 0 10 20
Stack out successfully 30
Data Num:6 Data:40 50 60 0 10 20
Stack out successfully 40
Data Num:5 Data:50 60 0 10 20
Stack out successfully 50
Data Num:4 Data:60 0 10 20
Stack out successfully 60
Data Num:3 Data:0 10 20
Stack out successfully 0
Data Num:2 Data:10 20
Stack out successfully 10
Data Num:1 Data:20
Stack out successfully 20
Data Num:0 Data:
Stack out failed , Data is empty
Stack out failed , Data is empty
Stack out failed , Data is empty
Stack out failed , Data is empty
Stack out failed , Data is empty
Stack out failed , Data is empty
Stack out failed , Data is empty
Stack out failed , Data is empty
Stack out failed , Data is empty
Stack out failed , Data is empty
Stack out failed , Data is empty
Stack out failed , Data is empty
Stack out failed , Data is empty
Data Num:0 Data:
Practical structure and shape :
#include <stdio.h>
#define Debug_En 1
#define Data_num 100
#define Data_Len 50
typedef struct
{
int RealData[Data_num];
int Sta;
}Stack_Data;
typedef struct Stack
{
Stack_Data Data[Data_Len];
int P_Active; // Current stack length
} My_Stack_type;
My_Stack_type My_Stack;
int Stack_Push(My_Stack_type *Stack_Active,Stack_Data *Data) /// Into the stack
{
if(Stack_Active->P_Active<Data_Len)
{
Stack_Active->Data[Stack_Active->P_Active]=*Data;
#if Debug_En
printf(" Stack successful %d=%d %d \r\n",Stack_Active->P_Active,
Stack_Active->Data[Stack_Active->P_Active].Sta,
Stack_Active->Data[Stack_Active->P_Active].RealData[0]);
#endif
Stack_Active->P_Active++;
#if Debug_En
Stack_Show(Stack_Active);
#endif
return 0;
}else{
#if Debug_En
printf(" Stack failed , Excessive data \r\n");
#endif
return 1;
}
}
int Stack_FILO_Pop(My_Stack_type *Stack_Active,Stack_Data *Data) // First in and then out of the stack
{
if(Stack_Active->P_Active)
{
Stack_Active->P_Active--;
*Data=Stack_Active->Data[Stack_Active->P_Active];
#if Debug_En
printf(" Stack out successfully %d=%d %d\r\n",Stack_Active->P_Active,Data->Sta,Data->RealData[0]);
Stack_Show(Stack_Active);
#endif
return 0;
}else{
printf(" Stack out failed , Data is empty \r\n");
return 1;
}
}
int Stack_FIFO_Pop(My_Stack_type *Stack_Active,Stack_Data *Data) // First in, first out
{
int i=0;
if(Stack_Active->P_Active)
{
*Data=Stack_Active->Data[0];
#if Debug_En
printf(" Stack out successfully %d=%d %d\r\n",Stack_Active->P_Active,Data->Sta,Data->RealData[0]);
#endif
for(i=0;i<Stack_Active->P_Active;i++)
{
Stack_Active->Data[i]=Stack_Active->Data[i+1];
}
Stack_Active->P_Active--;
#if Debug_En
Stack_Show(Stack_Active);
#endif
return 0;
}else{
#if Debug_En
printf(" Stack out failed , Data is empty \r\n");
#endif
return 1;
}
}
int Stack_Show(My_Stack_type *Stack_Active)// Print memory
{
int i=0;
printf("Data Num:%d Data:",Stack_Active->P_Active);
for(i=0;i<Stack_Active->P_Active;i++)
{
printf("%d %d|",Stack_Active->Data[i].Sta,Stack_Active->Data[i].RealData[0]);
}
printf("\r\n");
}
int main(int argc, char *argv[])
{
int i=0;
int Data;
Stack_Data Stack_Data_DIs;
for(i=0;i<12;i++)
{
Stack_Data_DIs.Sta=i*10;
Stack_Data_DIs.RealData[0]=10+i;
Stack_Push(&My_Stack,&Stack_Data_DIs);
}
Stack_Show(&My_Stack);
for(i=0;i<3;i++)
{
Stack_FILO_Pop(&My_Stack,&Stack_Data_DIs);
}
Stack_Show(&My_Stack);
for(i=0;i<3;i++)
{
Stack_FIFO_Pop(&My_Stack,&Stack_Data_DIs);
}
Stack_Show(&My_Stack);
for(i=0;i<12;i++)
{
Stack_Data_DIs.Sta=i*10;
Stack_Data_DIs.RealData[0]=10+i;
Stack_Push(&My_Stack,&Stack_Data_DIs);
}
Stack_Show(&My_Stack);
for(i=0;i<3;i++)
{
Stack_FILO_Pop(&My_Stack,&Stack_Data_DIs);
}
Stack_Show(&My_Stack);
for(i=0;i<20;i++)
{
Stack_FIFO_Pop(&My_Stack,&Stack_Data_DIs);
}
Stack_Show(&My_Stack);
return 0;
}
版权声明
本文为[Things will turn when they reach the extreme 1024]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210610056982.html
边栏推荐
- Imx6 debugging LVDS screen technical notes
- QT reading and writing XML files (including source code + comments)
- A few lines of code teach you to crawl lol skin pictures
- Log4j2 cross thread print traceid
- Ucosiii transplantation and use, reference punctual atom
- Spark performance optimization guide
- Jeecg boot microservice architecture
- Gson fastjason Jackson of object to JSON difference modifies the field name
- Hard core parsing promise object (do you know these seven common APIs and seven key questions?)
- CISSP certified daily knowledge points (April 14, 2022)
猜你喜欢
C medium? This form of
硬核解析Promise对象(这七个必会的常用API和七个关键问题你都了解吗?)
Solution to Chinese garbled code after reg file is imported into the registry
From introduction to mastery of MATLAB (2)
Analysez l'objet promise avec le noyau dur (Connaissez - vous les sept API communes obligatoires et les sept questions clés?)
Installation du docker redis
kettle庖丁解牛第17篇之文本文件输出
logstash 7. There is a time problem in X. the difference between @ timestamp and local time is 8 hours
Robocode tutorial 3 - Robo machine analysis
ArcGIS license error -15 solution
随机推荐
Rust: how to match a string?
解决允许在postman中写入注释请求接口方法
Ionic 从创建到打包指令集顺序
Creation and use of QT dynamic link library
C language to achieve 2048 small game direction merging logic
Solution to Chinese garbled code after reg file is imported into the registry
Daily CISSP certification common mistakes (April 12, 2022)
Feign requests the log to be printed uniformly
Software test summary
Pointers in rust: box, RC, cell, refcell
多功能工具箱微信小程序源码
QT add external font ttf
C medium? This form of
Function recursion and solving interesting problems
JD freefuck Jingdong HaoMao control panel background Command Execution Vulnerability
According to the result set queried by SQL statement, it is encapsulated as JSON
Robocode tutorial 3 - Robo machine analysis
Const keyword, variable and function are decorated with const
Nodejs安装
14 py games source code share the second bullet