无校验的简单队列(QUEUE)

#ifndef  QUEUE_H
#define  QUEUE_H
#include "main.h"

/*----------Custom----------*/
#define QUEUE_ValueType ushort
#define QUEUE_MaxSize 32
/*----------End----------*/

typedef struct
{
    QUEUE_ValueType Value[QUEUE_MaxSize];
    ushort Head;
    ushort Tail;
    ushort Size;
    ulong  Sum;
} QUEUE_TypeDef;

#ifdef   QUEUE_C

int QUEUE_SumValue(QUEUE_ValueType data)
{
/*----------Custom----------*/
    return data;
/*----------End----------*/
}

#endif

void QUEUE_Push(QUEUE_TypeDef* Que, QUEUE_ValueType Data);
void QUEUE_Pop (QUEUE_TypeDef* Que);

#endif
#define  QUEUE_C
#include "queue.h"

void QUEUE_Init(QUEUE_TypeDef* Que)
{
    Que->Head = 0;
    Que->Tail = 0;
    Que->Size = 0;
    Que->Sum  = 0;
}

void QUEUE_Push(QUEUE_TypeDef* Que, QUEUE_ValueType Data)
{
    Que->Value[Que->Tail] = Data;
    Que->Sum += QUEUE_SumValue(Data);
    Que->Tail = (Que->Tail + 1) % QUEUE_MaxSize;
    Que->Size ++;
}

void QUEUE_Pop(QUEUE_TypeDef* Que)
{
    Que->Sum -= QUEUE_SumValue(Que->Value[Que->Head]);
    Que->Head = (Que->Head + 1) % QUEUE_MaxSize;
    Que->Size --;
}
posted on 2020-11-06 10:33  棕色的北极熊  阅读(188)  评论(0)    收藏  举报