当前位置:网站首页>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
边栏推荐
- Win1远程出现“这可能是由于credssp加密oracle修正”解决办法
- MATLAB小技巧(6)七种滤波方法比较
- Crawling mobile game website game details and comments (MQ + multithreading)
- JD-FreeFuck 京東薅羊毛控制面板 後臺命令執行漏洞
- Daily CISSP certification common mistakes (April 13, 2022)
- 【ACM】376. 摆动序列
- Custom prompt box MessageBox in QT
- Permission management with binary
- Mode of interprocess communication
- 【ACM】455. 分发饼干(1. 大饼干优先喂给大胃口;2. 遍历两个数组可以只用一个for循环(用下标索引--来遍历另一个数组))
猜你喜欢

使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA

The vivado project corresponding to the board is generated by TCL script

Calculation of fishing net road density

Differences between SSD hard disk SATA interface and m.2 interface (detailed summary)

Robocode tutorial 3 - Robo machine analysis

Promote QT default control to custom control

Robocode tutorial 7 - Radar locking

深度学习经典网络解析目标检测篇(一):R-CNN

【ACM】376. Swing sequence

Halo 开源项目学习(七):缓存机制
随机推荐
Nodejs安装
Ucosiii transplantation and use, reference punctual atom
Matlab tips (6) comparison of seven filtering methods
【ACM】455. Distribute Biscuits (1. Give priority to big biscuits to big appetite; 2. Traverse two arrays with only one for loop (use subscript index -- to traverse another array))
Solution to Chinese garbled code after reg file is imported into the registry
CISSP certified daily knowledge points (April 12, 2022)
Serial port debugging tools cutecom and minicom
Arcpy adds fields and loop assignments to vector data
Daily CISSP certification common mistakes (April 18, 2022)
Dynamically add default fusing rules to feign client based on sentinel + Nacos
Pyppeter crawler
Rust: how to match a string?
软件测试总结
Const keyword, variable and function are decorated with const
idea中安装YapiUpload 插件将api接口上传到yapi文档上
Introduction to quantexa CDI syneo platform
线上怎么确定期货账户安全的?
QT add external font ttf
【ACM】70. 爬楼梯
Using transmittablethreadlocal to realize parameter cross thread transmission