数据结构 链式队列

一、链队列的结构

struct node // 结点的信息
{
    int data;
    struct node *next;
};
typedef struct node Node;

struct queue // 队列的信息
{
    Node *front;
    Node *rear;
};
typedef struct queue Queue;

二、链队列的源代码

1. LinkQueue.h

#ifndef _LINKQUEUE_H
#define _LINKQUEUE_H
#define SUCCESS 10000
#define FAILURE 10001
#define TRUE    10002
#define FALSE   10003

struct node 
{
    int data;
    struct node *next;
};
typedef struct node Node;

struct queue
{
    Node *front;
    Node *rear;
};
typedef struct queue Queue;

int InitQueue(Queue **q);
int queueinsert(Queue *queue, int e);
int DeleteQueue(Queue *q);
int GetFirst(Queue *q);
int QueueEmpty(Queue *q);
int ClearQueue(Queue *q);
int QueueDestroy(Queue **q);


#endif

2. LinkQueue.c

#include "LinkQueue.h"
#include <stdio.h>
#include <stdlib.h>

/*队列初始化*/
int InitQueue(Queue **q)
{
    (*q) = (Queue *)malloc(sizeof(Queue));
    if((*q) == NULL)
    {
        return FAILURE;
    }
    Node *p = (Node *)malloc(sizeof(Node));
    if(p == NULL)
    {
        return FAILURE;
    }

    p->next = NULL;
    (*q)->front = (*q)->rear = p;

    return SUCCESS;
}

/*入队列*/
int queueinsert(Queue *queue, int e)
{
    if(NULL == queue)
    {
        return FAILURE;
    }

    Node *q = (Node *)malloc(sizeof(Node));
    if(NULL == q)
    {
        return FAILURE;
    }

    q->data = e;
    q->next = NULL;
    queue->rear->next = q;
    queue->rear = q;

    return SUCCESS;
}

/*出队列*/
int DeleteQueue(Queue *q)
{
    if(NULL == q || q->rear == q->front)
    {
        return FAILURE;
    }

    int e;
    Node *p = q->front->next;
    e = p->data;
    q->front->next = p->next;
    if(!p->next)
    {
        q->rear = q->front;
    }
    free(p);

    return e;
}

/*取队头元素*/
int GetFirst(Queue *q)
{
    if(NULL == q || q->front == q->rear)
    {
        return FAILURE;
    }

    return q->front->next->data;
}

/*判断是否为空*/
int QueueEmpty(Queue *q)
{
    if(q == NULL)
    {
        return 1;
    }

    return (q->front == q->rear) ? TRUE : FALSE;
}

/*清空*/
int ClearQueue(Queue *q)
{
    if(NULL == q)
    {
        return FAILURE;
    }

    Node *p = q->front->next;
    while(p)
    {
        q->front->next = p->next;
        free(p);
        p = q->front->next;
    }
    q->rear = q->front;
    return SUCCESS;
}

/*销毁*/
int QueueDestroy(Queue **q)
{
    if(q == NULL || (*q) == NULL)
    {
        return FAILURE;
    }

    free((*q)->front);
    free(*q);
    *q = NULL;

    return SUCCESS;
}

3. TestLinkQueue.c

#include "LinkQueue.h"
#include <stdio.h>

int main()
{
    Queue *queue;
    int ret, i;

    /*初始化队列*/
    ret = InitQueue(&queue);
    if(ret == SUCCESS)
    {
        printf("Init Success!\n");
    }
    else
    {
        printf("Init Failure!\n");
    }

    /*入队列*/
    for(i = 0; i < 6; i++)
    {
        ret = queueinsert(queue, i);
        if(FAILURE == ret)
        {
            printf("Insert Failure!\n");
        }
        else
        {
            printf("Insert %d is Success!\n", i);
        }
    }

    /*出队列*/
    for(i = 0; i < 3; i++)
    {
        ret = DeleteQueue(queue);
        if(ret == FAILURE)
        {
            printf("Delete Failure!\n");
        }
        else
        {
            printf("Delete %d Success!\n", ret);
        }
    }

    /*取队头元素*/
    ret = GetFirst(queue);
    if(ret == FAILURE)
    {
        printf("Get First Failure!\n");
    }
    else
    {
        printf("First is %d\n", ret);
    }

    /*判断是否为空*/
    ret = QueueEmpty(queue);
    if(ret == TRUE)
    {
        printf("Queue is Empty!\n");
    }
    else
    {
        printf("Queue is not Empty!\n");
    }

    /*清空*/
    ret = ClearQueue(queue);
    if(ret == SUCCESS)
    {
        printf("Clear Success!\n");
    }
    else
    {
        printf("Clear Failure!\n");
    }

    /*销毁*/
    ret = QueueDestroy(&queue);
    if(ret == SUCCESS)
    {
        printf("Destroy Success!\n");
    }
    else
    {
        printf("Destroy Failure!\n");
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41998576/article/details/81569695