• 静态循环队列


    静态循环队列


    去实习之后发现静态循环队列是超级超级常见的存储方式。队列就是先进先出存储方式的数组(静态前提),我们在尾部插入,头部取出,我的理解是因为其结构稳定,易操作所以使用超级超级多。假如用它储存串口接收的数据,那么因为是数组吗,地址连续,可能某些操作要比动态方便的很多。
    下面简单的头部定义:

    #ifndef  __FIFO_H
    #define __FIFO_H
    
    #define DATESIZE   10
    
    typedef struct node{
    	
    	int date[DATESIZE];
    	int front;
    	int rear;	
    }FIFO,*FI;
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在公司里呢肯定不会这么简单,因为这种储存结构我们定义一次肯定要多次使用,在宏观上可能会同时接收多个地方的数据,例如同时接收串口1或者串口2的数据,因此我们必须要做一个兼容性。也就是结构体里套一个结构体,假如存在三个串口

    #ifndef  __FIFO_H
    #define __FIFO_H
    
    #define DATESIZE   10
    #define UARTNUM    3
    
    typedef struct node{
    	
    	int date[DATESIZE];
    	int front;
    	int rear;	
    }FIFO,*FI;
    
    typedef struct Fifo{
    	FIFO FIFO_RE[UARTNUM];
    }FIFO_RE_DATE; 
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    按照该方式即可。
    在我们最开始学习C语言的时候,要让一个数据在一个范围内循环赋值通常使用对总长度求余,那么循环队列也是使用该方法,让他在一定长度的数组内不停的循环,并保持先进先出的顺序。

    #include 
    #include 
    #include "FIFO.h"
    #include 
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    void Init_Fifo(FI queue)
    {
    	queue->front = 0;
    	queue->rear  = 0;
    	
    	printf("队列初始化成功......\r\n");
    	printf("队列总长度为:%d\r\n",DATESIZE);
    	printf("队头:%d\r\n",queue->front);
    	printf("队尾:%d\r\n",queue->rear);
    }
    
    bool Fifo_Isnull(FI queue)
    {
    	if(queue->front == queue->rear)
    	{
    		return true;
    	}else
    	{
    		return false;
    	}
    }
    bool Fifo_Isfull(FI queue)
    {
    	if(((queue->rear + 1) % DATESIZE) == queue->front)
    	{  
    		return true;
    	}else
    	{
    		return false;
    	}
    }
    
    void input_data(FI queue,int val)
    {
    	if(Fifo_Isfull(&queue))
    	{
    		printf("队列已满,入队失败\r\n");
    		return;
    	}
    	queue->date[queue->rear] = val;
    	queue->rear = (queue->rear+1) % DATESIZE;
    	printf("入队成功:%d 队头:%d 队尾:%d \r\n",val,queue->front,queue->rear);
    }
    int out_put(FI queue)
    {
    	int out = 0;
    	if(Fifo_Isnull(&queue))
    	{
    		printf("队列为空,出队失败\r\n");
    		return;		
    	}
    	out = queue->date[queue->front];
    	queue->front = (queue->front+1) % DATESIZE;
    	
    	printf("出队成功:%d 队头:%d 队尾:%d \r\n",out,queue->front,queue->rear);
    	
    	return out;
    	
    }
    void showfifo(FI queue)
    {
    	int cur = 0;
    	if(Fifo_Isnull(&queue))
    	{
    		printf("队列为空,出队失败\r\n");
    		return;		
    	}
    	cur = queue->front;
    	while(cur != queue->rear)
    	{
    		printf("%d ",queue->date[cur]);
    		cur = (cur+1) % DATESIZE;
    	}
    	printf("\r\n");
    }
    
    
    int main(int argc, char *argv[]) {
    	FIFO fifo;
    	Init_Fifo(&fifo);
    	input_data(&fifo,10);
    	input_data(&fifo,20);
    	input_data(&fifo,30);
    	input_data(&fifo,40);
    	input_data(&fifo,50);
    	input_data(&fifo,60);
    	showfifo(&fifo);
    	out_put(&fifo);
    	out_put(&fifo);
    	out_put(&fifo);
    	out_put(&fifo);
    	showfifo(&fifo);	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
  • 相关阅读:
    防御保护--入侵防御系统IPS
    【数据结构】八大经典排序(两万字大总结)
    学习二十大奋进新征程线上知识答题小程序登录技术点分析与实现
    Defaultdict:Python中的高效字典类
    java-net-php-python-jsp宠物寄养系统计算机毕业设计程序
    C语言 开关灯实验
    Pspice simulation with Op Amp AC circuits
    【毕业设计】基于云平台的火灾报警器 - stm32 物联网 单片机 OneNET云平台
    在 Android 10 中访问/proc/net/route权限被拒绝
    史上最全的Java进阶书籍推荐
  • 原文地址:https://blog.csdn.net/weixin_52297353/article/details/133497841