当前位置:网站首页>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