本文摘抄于《C嵌入式编程设计模式》
- 头文件部分:Queue.h
2. #ifndef __QUEUE_H_
#define __QUEUE_H_
#define QUEUE_SIZE 10
/* class queue */
typedef struct Queue queue;
struct Queue
{
int buffer[QUEUE_SIZE];
int head;
int size;
int tail;
int (*isFull)(Queue *const me);
int (*isEmpty)(Queue *const me);
int (*getSize)(Queue *const me);
void (*insert)(Queue *const me, int k);
int (*remove)(Queue *const me);
};
/* Constructors and destructors */
void Queue_init(Queue *const me, int (*isFull)(Queue *const me),
int (*isEmpty)(Queue *const me),
int (*getSize)(Queue *const me),
void (*insert)(Queue *const me, int k),
int (*remove)(Queue *const me));
void Queue_Cleanup(Queue *const me);
/* Operaters */
int Queue_isFull(Queue *const me);
int Queue_isEmpty(Queue *const me);
int Queue_getSize(Queue *const me);
void Queue_insert(Queue *const me, int k);s
int Queue_remove(Queue *const);
Queue *Queue_Create(void);
void Queue_Destroy(Queue *const me);
#endif
- 源文件实现部分:queue.c
#include <stdio.h>
#include <stdlib.h>
#include "Queue.h"
/* init struct */
void Queue_init(Queue *const me, int (*isFullFunction)(Queue *const me),
int (*isEmptyFunction)(Queue *const me),
int (*getSizeFunction)(Queue *const me),
void (*insertFunction)(Queue *const me, int k),
int (*removeFunction)(Queue *const me))
{
me->head = 0;
me->size = 0;
me->tail = 0;
me->isFull = isFullFunction;
me->isEmpty = isEmptyFunction;
me->getSize = getSizeFunction;
me->insert = insertFunction;
me->remove = removeFunction;
}
/* operation clean up */
void Queue_Cleanup(Queue * const me)
{
//TODO:
}
/* operation is Full */
int Queue_isFull(Queue * const me)
{
return (me->head+1)%QUEUE_SIZE == me->tail;
}
/* operation isEmpty */
int Queue_isEmpty(Queue * const me)
{
return (me->head == me->tail);
}
/* operation getSize */
int Queue_getSize(Queue * const me)
{
return me->size;
}
/* operation insert */
void Queue_insert(Queue * const me,int k)
{
if(!me->isFull(me))
{
me->buffer[me->head] = k;
me->head = (me->head+1)%QUEUE_SIZE;
++me->size;
}
}
/* operation remove */
int Queue_remove(Queue *const me)
{
int value = -9999;
if(!me->isEmpty(me))
{
value = me->buffer[me->tail];
me->tail = (me->tail+1)%QUEUE_SIZE;
--me->size;
}
return value;
}
/* queue create */
Queue *Queue_Create(void)
{
Queue *me = (Queue*)malloc(sizeof(Queue));
if(me!=NULL)
{
Queue_init(me,Queue_isFull,Queue_isEmpty,Queue_getSize,Queue_insert,Queue_remove);
}
return me;
}
/* queue remove */
void Queue_remove(Queue *const me)
{
if(me!= NULL)
{
Queue_Cleanup(me);
}
free(me);
}
- 队列测试代码:TestQueue.c
#include <stdio.h>
#include <stdlib.h>
#include "Queue.h"
int main(void)
{
int j,k,h,t;
/* test normal queue */
Queue *myQ;
myQ = Queue_Create();
k = 1000;
for(j = 0; j < QUEUE_SIZE; j++)
{
h = myQ->head;
myQ->insert(myQ, k);
printf("inserting %d at position %d, size=%d\n", k--, h, myQ->getSize(myQ));
}
printf("Inserting %d elements\n", myQ->getSize(myQ));
for(j = 0;j < QUEUE_SIZE; j++)
{
t = myQ->tail;
k = myQ->getSize(myQ);
printf("Removing %d at position %d, size=%d\n", k, t, myQ->getSize(myQ));
}
printf("last item removed %d\n", k);
printf("Current queue size %d\n", myQ->getSize(myQ));
puts("Queue test program");
return EXIT_SUCCESS;
}