复习数据结构之队列

直接上code

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

#define OK 1
#define PARAMETER "%d\n"

typedef int dataType;
typedef int status;

typedef struct QNode
{
    dataType data;
    struct QNode * next;
}QNode, *QNodePtr;

typedef struct LinkQueue
{
    QNodePtr front;
    QNodePtr rear;
}LinkQueue, *LinkQueuePtr;

/***************initial a link queue******************/

status initialQueue(LinkQueuePtr queue)
{
    if(NULL == queue)
    {
        printf("the queue is null\n");
        exit(-1);
    }
    queue->front = queue->rear = (QNodePtr)malloc(sizeof(QNode));
    if(NULL == queue->front)
    {
        printf("initial queue error.\n");
        exit(-1);
    }
    queue->front->next = NULL;
    return OK;
}

/***************destory a link queue******************/

status destoryQueue(LinkQueuePtr queue)
{
    if(NULL == queue)
    {
        printf("the queue is null\n");
        exit(-1);
    }
    while(queue->front)
    {
        queue->rear = queue->front->next;
        free(queue->front);
        queue->front = queue->rear;
    }
    return OK;
}

/***************insert******************/

status insertQueue(LinkQueuePtr queue, dataType dataInput)
{
    QNodePtr tmpNode;
    if(NULL == queue)
    {
        printf("the queue is null\n");
        exit(-1);
    }
    tmpNode = (QNodePtr)malloc(sizeof(QNode));
    if(NULL == tmpNode)
    {
        printf("malloc memory error in insertQueue.\n");
        exit(-1);
    }
    tmpNode->data = dataInput;
    tmpNode->next = NULL;
    queue->rear->next = tmpNode;
    queue->rear = tmpNode;
    return OK;
}

/***************delete front element******************/

status deleteQueue(LinkQueuePtr queue, dataType * dataOutput)
{
    QNodePtr tmpNode;
    if(NULL == queue || NULL == dataOutput)
    {
        printf("invalid parameter in deleteQueue\n");
        exit(-1);
    }
    if(queue->front == queue->rear)
    {
        printf("the queue is already null\n");
        exit(-1);
    }
    tmpNode = queue->front->next;
    *dataOutput = tmpNode->data;
    queue->front->next= tmpNode->next;
    if(queue->rear == tmpNode)
    {
        queue->front = queue->rear;
    }
    free(tmpNode);
	tmpNode = NULL;
    return OK;
}

/***************traversal *****************/

status traversalQueue(LinkQueuePtr queue)
{
    QNodePtr tmpNode;
    if(NULL == queue)
    {
        printf("queue is null is traversalQueue\n");
        exit(-1);
    }
    tmpNode = queue->front;
    while(tmpNode->next)
    {
        printf("the data is %d\n", tmpNode->next->data);
        tmpNode = tmpNode->next;
    }
    return OK;
}
int main(void)
{
    LinkQueue q;
    dataType element;
    (void)memset((void*)&q, 0, sizeof(LinkQueue));
    if(OK != initialQueue(&q))
    {
        printf("error for initial in function main\n");
        return -1;
    }
    insertQueue(&q, 10);
    insertQueue(&q, 12);
    insertQueue(&q, 8);
    insertQueue(&q, 9);
    traversalQueue(&q);
    deleteQueue(&q, &element);
    printf(PARAMETER, element);
    deleteQueue(&q, &element);
    printf(PARAMETER, element);
    traversalQueue(&q);
    destoryQueue(&q);
    //insertQueue(&q, 10);
    //traversalQueue(&q);
    return OK;
}
点赞