C语言设计模式(应用)

#ifndef QUEUE_H
#define QUEUE_H

#define QUEUE_SIZE 10

typedef struct queue
{
    int buffer[QUEUE_SIZE];
    int head;
    int size;
    int tail;

    int (*isFull)(struct queue* const me);
    int (*isEmpty)(struct queue* const me);
    int (*getSize)(struct queue* const me);
    void (*insert)(struct queue* const me, int k);
    int (*remove)(struct queue* const me);
}QUEUE;

void Queue_Init(QUEUE* 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));
void Queue_Cleanup(QUEUE* const me);

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);
int Queue_remove(QUEUE* const me);

QUEUE* Queue_Create(void);
void Queue_Destroy(QUEUE* const me);

#endif

 

#include 
#include 
#include "queue.h"

void Queue_Init(QUEUE* 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->tail=0;
    me->size=0;

    me->isFull=isFullFunction;
    me->isEmpty=isEmptyFunction;
    me->getSize=getSizeFunction;
    me->insert=insertFunction;
    me->remove=removeFunction;
}

void Queue_Cleanup(QUEUE* const me){


}

int Queue_isFull(QUEUE* const me){

    return (me->head+1)%QUEUE_SIZE==me->tail;
}

int Queue_isEmpty(QUEUE* const me){

    return me->head==me->tail;
}

int Queue_getSize(QUEUE* const me){

    return me->size;
}

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;
    }
}

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* 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;
}

void Queue_Destroy(QUEUE* const me){

    if (me!=NULL)
    {
        Queue_Cleanup(me);
    }

    free(me);
}

 

#include "queue.h"
#include 
#include 

int main(void)
{
    int j,k,h,t;

    QUEUE* myQ;
    myQ=Queue_Create();
    k=1000;

    for (j = 0; j)
    {
        h=myQ->head;
        myQ->insert(myQ, k);
        printf("inserting %d at position %d, size=%d\n", k--,h,myQ->getSize(myQ));
    }
    printf("Iserted %d elements\n", myQ->getSize(myQ));

    for (j = 0; j)
    {
        t=myQ->tail;
        k=myQ->remove(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 0;
}

 

转载于:https://www.cnblogs.com/whylinux/p/10494607.html

你可能感兴趣的:(C语言设计模式(应用))