数据结构C语言实现链式队列模板

C语言实现链式队列模板

#include <stdio.h>
#include <stdlib.h>
/************************************************
 * C语言链式队列模板
 * 
 * 1.InitLinkQueue() 初始化链式队列:为头指针动态分配
 * 内存,为头结点的next赋NULL值,让队头指针和队尾指针指
 * 向头结点.
 * 
 * 2.EnLinkQueue(LinkQueue *queue, ElemType data) 
 * 入队:创建一个新结点,对新结点赋值,将新结点地址赋值给
 * 队尾指针所指向的结点的next,将队尾指针指向新结点.
 * 
 * 3.DeLinkQueue(LinkQueue *queue) 出队:判断队头指
 * 针指向结点的下一个结点是否为空,不为空则输出下一个结
 * 点的数据并释放下一个结点,如果出队结点是队尾指针指向
 * 的结点,将队尾指针赋值为队头指针.
 * 
 * 4.LinkQueueIsEmpty(LinkQueue *queue) 判断链式队列
 * 是否为空,为空返回1,不为空返回0.
 * 
 * 5.DisplayLinkQueue(LinkQueue * queue) 输出链式队
 * 列中的元素.
 * 
 * 6.DestoryLinkQueue(LinkQueue * queue) 销毁链式队
 * 列,释放链表所有结点,将头指针,队头指针,队尾指针赋NULL
 * 值.
 * 
 * 作者:西瓜小羽毛
 * 日期:2020/7/21
 ************************************************/
#define TRUE 1
#define FALSE 0
typedef bool BOOL;
typedef int ElemType;
typedef struct Node
{
    ElemType data; //数据域
    Node *next;    //下一个结点
} Node;
typedef struct LinkQueue
{
    Node *head; //链表头指针
    Node *top;  //队头指针
    Node *rear; //队尾指针
} LinkQueue;
/* 初始化链式队列 */
LinkQueue InitLinkQueue()
{
    LinkQueue queue;
    queue.head = (Node *)malloc(sizeof(Node));
    queue.head->next = NULL;
    queue.top = queue.head;
    queue.rear = queue.head;
    return queue;
}
/* 链式队列入队 */
void EnLinkQueue(LinkQueue *queue, ElemType data)
{
    if (queue->head == NULL)
    {
        printf("链式队列未初始化");
        return;
    }
    Node *node = (Node *)malloc(sizeof(Node));
    node->data = data;
    node->next = NULL;
    queue->rear->next = node;
    queue->rear = queue->rear->next;
}
/* 链式队列出队 */
ElemType DeLinkQueue(LinkQueue *queue)
{
    ElemType data = 0;
    if (queue->head == NULL)
    {
        printf("链式队列未初始化");
        return data;
    }
    if (queue->top->next == NULL)
    {
        printf("链式队列为空队列\n");
        return data;
    }
    Node *pNode = queue->top->next;
    queue->top->next = pNode->next;
    data = pNode->data;
    if (pNode == queue->rear)
    {
        queue->rear = queue->top;
    }
    free(pNode);
    return data;
}
/* 链式队列是否为空 */
BOOL LinkQueueIsEmpty(LinkQueue *queue)
{
    if (queue->head == NULL)
    {
        printf("链式队列未初始化");
        return FALSE;
    }
    if (queue->top->next == NULL)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}
/* 输出链式队列中的元素 */
void DisplayLinkQueue(LinkQueue *queue)
{
    if (queue->head == NULL)
    {
        printf("链式队列未初始化");
        return;
    }
    Node *pNode = queue->top->next;
    while (pNode != NULL)
    {
        printf("%d ", pNode->data);
        pNode = pNode->next;
    }
    printf("\n");
}
/* 销毁链式队列 */
void DestoryLinkQueue(LinkQueue *queue)
{
    if (queue->head == NULL)
    {
        printf("链式队列未初始化");
        return;
    }
    Node *pNode = queue->head;
    Node *qNode = NULL;
    while (pNode != NULL)
    {
        qNode = pNode;
        pNode = pNode->next;
        free(qNode);
    }
    queue->head = NULL;
    queue->top = NULL;
    queue->rear = NULL;
}

测试代码

#include "LinkQueue.h"
int main()
{
    /* 链式队列测试代码 */
    LinkQueue queue = InitLinkQueue();
    printf("链式队列是否为空:%d\n", LinkQueueIsEmpty(&queue));
    EnLinkQueue(&queue, 20);
    EnLinkQueue(&queue, 30);
    EnLinkQueue(&queue, 10);
    EnLinkQueue(&queue, 60);
    DisplayLinkQueue(&queue);
    printf("出队元素:%d\n", DeLinkQueue(&queue));
    printf("链式队列是否为空:%d\n", LinkQueueIsEmpty(&queue));
    printf("出队元素:%d\n", DeLinkQueue(&queue));
    printf("链式队列是否为空:%d\n", LinkQueueIsEmpty(&queue));
    DestoryLinkQueue(&queue);
}

输出结果

链式队列是否为空:1
20 30 10 60
出队元素:20
链式队列是否为空:0
出队元素:30
链式队列是否为空:0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值