My Queue ADT 链表实现

来源:互联网 发布:洛阳平安数据科技待遇 编辑:程序博客网 时间:2024/05/16 10:59

        紧接着 stack ADT ,又完成了个简单的链表,没什么好说的。

主要是 默认QueueElement 使用 void*,若要自定义,请于引用queue.h前 插入一些代码:

#define QUEUE_ELEMENT_DEF

typedef TypeNullable QueueElement;

类型最好是指针之类的可以 赋值为NULL的,不然就自己改函数去吧。<^_^>

(Win7, VIm + GCC)

queue.h

/* * * * * * * * * * * * * * * * * * * * * * * * * * A ADT Queue. * Notice:  This queue only save pointer that point *          to data. *          Because the elements are void pointers,  *          you should convert into the correct *          type that you need. * * * * * * * * * * * * * * * * * * * * * * * * */#ifndef QUEUE_ADT#define QUEUE_ADT/* * * * * * * * * * * * * * * * * * * * * * * * *  * The default type of the queue's element is void*. * If you want to use your type(must be a kind of  * pointer(or that value can be NULL), or you have * to change the code of the functions), to do like * this: *      #define QUEUE_ELEMENT_DEF *      typedef TypeNullable QueueElement; * And these codes must be written before inlclude * queue.h. * * * * * * * * * * * * * * * * * * * * * * * * */#ifndef QUEUE_ELEMENT_DEF#define QUEUE_ELEMENT_DEFtypedef void* QueueElement;#endifstruct QueueNode;typedef struct QueueNode QueueNode;typedef QueueNode* NodePointer;struct QueueNode{    QueueElement data;    NodePointer next;};typedef struct{    int nodeCount;    int maxSize;    NodePointer front;    NodePointer rear;} Queue;typedef Queue* QueuePointer;#define MIN_QUEUE_SIZE (8)QueuePointer NewQueue(int maxSize);int IsQueueFull(QueuePointer queue);int IsQueueEmpty(QueuePointer queue);int EnQueue(QueuePointer queue, QueueElement element);QueueElement GetFrontElement(QueuePointer queue);QueueElement DeQueue(QueuePointer queue);int EmptyQueue(QueuePointer queue);int DisposeQueue(QueuePointer queue);int DoubleQueueSize(QueuePointer queue);#endif

queue.c

#include <stdlib.h>/* * * * * * * * * * * * * * * * * * * * * * * * * * One implementation of ADT Queue. * Notice:  This queue only save pointer that point *          to data. *          Because the elements are void pointers,  *          you should convert into the correct *          type that you need. * * * * * * * * * * * * * * * * * * * * * * * * *//* * * * * * * * * * * * * * * * * * * * * * * * *  * The default type of the queue's element is void*. * If you want to use your type(must be a kind of  * pointer(or that value can be NULL), or you have * to change the code of the functions), to do like * this: *      #define QUEUE_ELEMENT_DEF *      typedef TypeNullable QueueElement; * And these codes must be written before inlclude * queue.h. * * * * * * * * * * * * * * * * * * * * * * * * *///Heder file of ADT queue.#include "queue.h"/* * * * * * * * * * * * * * * * * * * * * * * * * * NewQUeue: Create a new queue that size is  *           maxSize( >=8 ). * Return:  A pointer of queue, if create  *          successfully. *          NULL: Fail to create. * * * * * * * * * * * * * * * * * * * * * * * * */QueuePointer NewQueue(int maxSize){    if (maxSize < MIN_QUEUE_SIZE)    {        maxSize = MIN_QUEUE_SIZE;    }    QueuePointer newQueue = NULL;    if ( newQueue = (QueuePointer)malloc(maxSize * sizeof(Queue)) )    {        newQueue->nodeCount = 0;        newQueue->maxSize = maxSize;        newQueue->front = NULL;        newQueue->rear = NULL;        return newQueue;    }    else    {        return NULL;    }}/* * * * * * * * * * * * * * * * * * * * * * * * * * IsQueueFull: Decide whether a queue is full; * Return:  1: Is full. *          0: Not full. * * * * * * * * * * * * * * * * * * * * * * * * */int IsQueueFull(QueuePointer queue){    if (queue->nodeCount == queue->maxSize)    {        return 1;    }    else    {        return 0;    }}/* * * * * * * * * * * * * * * * * * * * * * * * * * IsQueueEmpty: Decide whether a queue is empty; * Return:  1: Is empty. *          0: Not empty. * * * * * * * * * * * * * * * * * * * * * * * * */int IsQueueEmpty(QueuePointer queue){    if (queue->nodeCount == 0)    {        return 1;    }    else    {        return 0;    }}/* * * * * * * * * * * * * * * * * * * * * * * * * * EnQueue: Add a new element to the rear of queue. * Return:  1: Successfully. *          0: Fail to add. *          -1: Queue is full. *          -2: Invalid element, can't be NULL.  * * * * * * * * * * * * * * * * * * * * * * * * */int EnQueue(QueuePointer queue, QueueElement element){    if (!element)    {        return -2;    }    if (queue)    {        if ( !IsQueueFull(queue))        {            NodePointer theNode = (NodePointer)malloc(sizeof(QueueNode));            if (!theNode)            {                return 0;            }            theNode->data = element;            theNode->next = NULL;            if ( queue->rear )            {                queue->rear->next = theNode;                queue->rear = queue->rear->next;            }            else            {                queue->rear = theNode;                queue->front = theNode;            }            queue->nodeCount++;            return 1;        }        else        {            return -1;        }    }    else    {        return 0;    }}/* * * * * * * * * * * * * * * * * * * * * * * * * * GetFrontElement: Just get the front element. * Return:  QueneElement: The front element of the *                        queue. *          NULL: Unsuccessfully or empty queue.   * * * * * * * * * * * * * * * * * * * * * * * * */QueueElement GetFrontElement(QueuePointer queue){    if (queue)    {        if (!IsQueueEmpty(queue))        {            return queue->front->data;        }        else        {            return NULL;        }    }    else    {        return NULL;    }}/* * * * * * * * * * * * * * * * * * * * * * * * * * DeQueue: Delete the front of queue and return it. * Return:  QueneElement: The front element of the *                        queue. *          NULL: Unsuccessfully or empty queue. * * * * * * * * * * * * * * * * * * * * * * * * */QueueElement DeQueue(QueuePointer queue){    if (queue)    {        if (!IsQueueEmpty(queue))        {            QueueElement element = queue->front->data;            NodePointer node = queue->front;            queue->front = queue->front->next;            queue->nodeCount--;            if (!queue->front)            {                queue->rear = queue->front;            }            free(node);            return element;        }        else        {            return NULL;        }    }    else    {        return NULL;    }}/* * * * * * * * * * * * * * * * * * * * * * * * * * EmptyQueue: Delete all elements from a queue. * Return:  1: All deleted. *          0: ERROR. * * * * * * * * * * * * * * * * * * * * * * * * */int EmptyQueue(QueuePointer queue){    if (queue)    {        if (queue->front)        {            NodePointer temp;            while (queue->rear != queue->front)            {                temp = queue->front;                queue->front = queue->front->next;                free(temp);                queue->nodeCount--;            }            free(queue->front);            queue->front = NULL;            queue->rear = NULL;            queue->nodeCount--;        }        return 1;    }    else    {        return 0;    }}/* * * * * * * * * * * * * * * * * * * * * * * * * * DisposeQueue: Delete a queue itself(and delete *               all elements). * Return:  1: Successfully. *          0: Unsuccessfully. * Warning: After disposing a queue, you must make *          all pointer pointing into the queue  *          be NULL! * * * * * * * * * * * * * * * * * * * * * * * * */int DisposeQueue(QueuePointer queue){    if ( EmptyQueue(queue) )    {        free(queue);        return 1;    }    else    {        return 0;    }}/* * * * * * * * * * * * * * * * * * * * * * * * * * DoubleQueueSize: Double the size of the queue. * Return:  1: Successfully. *          0: Unsuccessfully. * * * * * * * * * * * * * * * * * * * * * * * * */int DoubleQueueSize(QueuePointer queue){    if (queue)    {        queue->maxSize *= 2;        return 1;    }    else    {        return 0;    }}


//做个测试。

program.c

#include <stdio.h>#define QUEUE_ELEMENT_DEFtypedef char QueueElement;#include "queue.h"void PrintInf(QueuePointer queue);int main(void){    int size = 0;    do    {        printf("Enter the size of queue:");        scanf("%d", &size);    } while (size < 1);    //Create a new queue.    QueuePointer theQ = NewQueue(size);    if (!theQ)    {        return -1;    }    PrintInf(theQ);    //EnQueue test.    EnQueue(theQ, 'a');    EnQueue(theQ, 'b');    EnQueue(theQ, 'c');    PrintInf(theQ);    EnQueue(theQ, 'd');    PrintInf(theQ);    //GetFrontElement test.    char ch = GetFrontElement(theQ);    printf("   %c\n", ch);    PrintInf(theQ);    //DeQueue test.    ch = DeQueue(theQ);    printf("   %c\n", ch);    PrintInf(theQ);    //FullQueue test.    EnQueue(theQ, 'c');    PrintInf(theQ);        EnQueue(theQ, 'c');    PrintInf(theQ);    EnQueue(theQ, 'c');    PrintInf(theQ);    EnQueue(theQ, 'c');    PrintInf(theQ);    EnQueue(theQ, 'c');    PrintInf(theQ);    EnQueue(theQ, 'c');    PrintInf(theQ);    EnQueue(theQ, 'c');    PrintInf(theQ);    EnQueue(theQ, 'c');    PrintInf(theQ);    puts("\n");    DoubleQueueSize(theQ);    PrintInf(theQ);    EnQueue(theQ, 'x');    PrintInf(theQ);    EnQueue(theQ, 'x');    PrintInf(theQ);    EnQueue(theQ, 'x');    PrintInf(theQ);    EnQueue(theQ, 'x');    PrintInf(theQ);    EnQueue(theQ, 'x');    PrintInf(theQ);    EnQueue(theQ, 'x');    PrintInf(theQ);    /*    //DeQueue all.    ch = DeQueue(theQ);    printf("   %c\n", ch);    PrintInf(theQ);    ch = DeQueue(theQ);    printf("   %c\n", ch);    PrintInf(theQ);    ch = DeQueue(theQ);    printf("   %c\n", ch);    PrintInf(theQ);    ch = DeQueue(theQ);    printf("   %c\n", ch);    PrintInf(theQ);    ch = DeQueue(theQ);    printf("   %c\n", ch);    PrintInf(theQ);    ch = DeQueue(theQ);    printf("   %c\n", ch);    PrintInf(theQ);    ch = DeQueue(theQ);    printf("   %c\n", ch);    PrintInf(theQ);    ch = DeQueue(theQ);    printf("   %c\n", ch);    PrintInf(theQ);    ch = DeQueue(theQ);    printf("   %c\n", ch);    PrintInf(theQ);    ch = DeQueue(theQ);    printf("   %c\n", ch);    PrintInf(theQ);    */    //EmptyQueue test    EmptyQueue(theQ);    PrintInf(theQ);    if (DisposeQueue(theQ))    {        theQ = NULL;        puts("    Dised!");    }    getch();    return 0;}void PrintInf(QueuePointer queue){    printf("  maxSize = %d, node count = %d\n", queue->maxSize, queue->nodeCount);    printf("    Front = %d, Rear = %d\n", queue->front, queue->rear);}

简单测试





原创粉丝点击