#ifndef _MY_QUEUE_H_
#define _MY_QUEUE_H_
typedef enum
{
QUEUE_EMPTY = 0,
QUEUE_FULL = 1,
QUEUE_OK = 2,
} queue_status_t;
#define QUEUE_SIZE 1024
typedef struct
{
short front;
short rear;
short size;
short data[QUEUE_SIZE];
} queue_t;
extern queue_t g_Queue1;
void QueueInit(queue_t *q);
short QueuePush(queue_t *q, short data);
short QueuePop(queue_t *q, short *data);
#endif /* _MY_QUEUE_H_ */
#include "myqueue.h"
queue_t g_Queue1;
void QueueInit(queue_t *q)
{
q->size = 0;
q->front = 0;
q->rear = 0;
}
short QueuePush(queue_t *q, short data)
{
if(((q->rear % QUEUE_SIZE) == q->front) && (q->size == QUEUE_SIZE))
{
return QUEUE_FULL;
}
q->data[q->rear] = data;
q->rear = (q->rear + 1) % QUEUE_SIZE;
q->size++;
return QUEUE_OK;
}
short QueuePop(queue_t *q, short *data)
{
if((q->front == q->rear) && (q->size == 0))
{
return QUEUE_EMPTY;
}
*data = q->data[q->front];
q->front = (q->front + 1) % QUEUE_SIZE;
q->size--;
return QUEUE_OK;
}
queue 先进先出的原则。