当前位置:网站首页>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
边栏推荐
- Use of regular expressions in QT
- Permission management with binary
- Docker installation MySQL
- Refcell in rust
- Imx6 debugging LVDS screen technical notes
- Cygwin64 right click to add menu, and open cygwin64 here
- Introduction to quantexa CDI syneo platform
- The difference between deep copy and shallow copy
- 登录和发布文章功能测试
- Daily network security certification test questions (April 12, 2022)
猜你喜欢

Nodejs安装

Hard core parsing promise object (do you know these seven common APIs and seven key questions?)

Spark performance optimization guide

【ACM】455. 分发饼干(1. 大饼干优先喂给大胃口;2. 遍历两个数组可以只用一个for循环(用下标索引--来遍历另一个数组))

Calculation of fishing net road density

C medium? This form of

ArcGIS license error -15 solution

Qt读写XML文件(含源码+注释)
![解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]](/img/5f/a80951777a0473fcaa685cd6a8e5dd.png)
解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

Notepad + + replaces tabs with spaces
随机推荐
Refcell in rust
Daily network security certification test questions (April 15, 2022)
According to the result set queried by SQL statement, it is encapsulated as JSON
Multifunctional toolbox wechat applet source code
14个py小游戏源代码分享第二弹
WiFi ap6212 driver transplantation and debugging analysis technical notes
mysql自动启动设置用Systemctl start mysqld启动
idea中安装YapiUpload 插件将api接口上传到yapi文档上
Rewrite four functions such as StrCmp in C language
Log4j2 cross thread print traceid
CISSP certified daily knowledge points (April 18, 2022)
MATLAB从入门到精通(二)
Queue solving Joseph problem
Daily network security certification test questions (April 14, 2022)
Quantexa CDI(场景决策智能)Syneo平台介绍
Introduction to quantexa CDI syneo platform
Ionic 从创建到打包指令集顺序
Rust: how to match a string?
C language to achieve 2048 small game direction merging logic
使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA