当前位置:网站首页>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
边栏推荐
- Mode of interprocess communication
- Daily CISSP certification common mistakes (April 13, 2022)
- RC smart pointer in rust
- Pyppeter crawler
- xlsxwriter. exceptions. Filecreateerror: [errno 13] permission denied
- Map basemap Library
- Docker 安裝 Redis
- Climbing watermelon video URL
- JD-FreeFuck 京東薅羊毛控制面板 後臺命令執行漏洞
- Reptile efficiency improvement method
猜你喜欢

【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))

WiFi ap6212 driver transplantation and debugging analysis technical notes

7-21 wrong questions involve knowledge points.

Robocode tutorial 7 - Radar locking

logstash 7. There is a time problem in X. the difference between @ timestamp and local time is 8 hours

Introduction to quantexa CDI syneo platform

Cygwin64 right click to add menu, and open cygwin64 here

SSD硬盘SATA接口和M.2接口区别(详细)总结

Spark performance optimization guide

【ACM】70. 爬楼梯
随机推荐
JD-FreeFuck 京東薅羊毛控制面板 後臺命令執行漏洞
Docker installation MySQL
How to install jsonpath package
According to the result set queried by SQL statement, it is encapsulated as JSON
Using transmittablethreadlocal to realize parameter cross thread transmission
Gson fastjason Jackson of object to JSON difference modifies the field name
14个py小游戏源代码分享第二弹
【ACM】376. Swing sequence
Analysez l'objet promise avec le noyau dur (Connaissez - vous les sept API communes obligatoires et les sept questions clés?)
Rust: a simple example of TCP server and client
【ACM】455. 分发饼干(1. 大饼干优先喂给大胃口;2. 遍历两个数组可以只用一个for循环(用下标索引--来遍历另一个数组))
Daily CISSP certification common mistakes (April 13, 2022)
Calculation of fishing net road density
Daily CISSP certification common mistakes (April 19, 2022)
C language to achieve 2048 small game direction merging logic
Hard core parsing promise object (do you know these seven common APIs and seven key questions?)
CISSP certified daily knowledge points (April 13, 2022)
Qt读写XML文件(含源码+注释)
Arcpy adds fields and loop assignments to vector data
Cells in rust share variable pointers