C语言实现链式队列

#include "link_queue.h"

/********************************
创建
入列
出列
显示所有数据
是否为空
释放队列
********************************/

//创建1
void create_link_queue_1(pLinkQueue *h)
{
    (*h) = (pLinkQueue)malloc(sizeof(tLinkQueue));
    if(NULL == (*header))
    {
        printf("create_link_queue malloc header err\r\n");
        return ;
    }

    (*h)->len = 0;
    (*h)->header = NULL;
    (*h)->tail = NULL;
}

//创建2
void create_link_queue_2(pLinkQueue h)
{
    if(NULL == h)
    {
        return ;
    }

    h->len = 0;
    h->header = NULL;
    h->tail = NULL;
}

//创建3
pLinkQueue create_link_queue_3(void)
{
    pLinkQueue new_h;

    new_h = (pLinkQueue)malloc(sizeof(tLinkQueue));
    if(NULL == new_h)
    {
        printf("create_link_queue3 malloc header err\r\n");
        return;
    }

    new_h->len = 0;
    new_h->header = NULL;
    new_h->tail = NULL;

    return new_h;
}

//是否为空
int is_empty_link_queue(pLinkQueue h)
{
    if((NULL == h) || (0 == (h->len)) || (NULL == h->header))
    {
        return 1;
    }

    return 0;
}

//入列:注意 队列只有尾插
int into_link_queue(pLinkQueue h,QUEUE_DATA_TYPE data)
{
    pQueueNode new_node;

    if(NULL == h)
    {
        return 1;
    }

    new_node = (pQueueNode)malloc(sizeof(tQueueNode));
    if(NULL == new_node)
    {
        printf("into_link_queue malloc err\r\n");
        return 1;
    }

    new_node->data = data;
    new_node->next = NULL;

    h->tail->next = new_node;
    h->tail = new_node;
    if(NULL == h->header)
    {
        h->header = new_node;
    }

    h->len++;

    return 0;
}

//出列
int out_link_queue(pLinkQueue h,QUEUE_DATA_TYPE *data)
{
    pQueueNode outNode;

    if(is_empty_link_queue(h))
    {
        printf("队列为空\r\n");
        return 1;
    }

    outNode = h->header;

    *data = outNode->data;
    h->header = outNode->next;
    h->len--;

    if(outNode == h->tail)
    {
        h->tail = NULL;
    }

    free(outNode);

    return 0;
}

//显示所有数据
int display_all_link_queue_node(pLinkQueue h)
{
    pQueueNode tmpNode;

    if(is_empty_link_queue(h))
    {
        printf("队列为空\r\n");
        return 1;
    }

    for((tmpNode = h->header); (NULL != tmpNode); (tmpNode = tmpNode->next))
    {
        printf("%d ",tmpNode->data);
    }

    return 0;
}

//释放链表
int free_link_queue(pLinkQueue h)
{
    pQueueNode freeNode;

    if(is_empty_link_queue(h))
    {
        printf("队列为空\r\n");
        return 1;
    }

    while(NULL != (h->header))
    {
        freeNode = h->header;
        h->header = h->header->next;
        free(freeNode);
    }

    free(h);

    return 0;
}
#ifndef LINK_QUEUE_H_INCLUDED
#define LINK_QUEUE_H_INCLUDED

#ifndef NULL
#define NULL ((void *)0)
#endif // NULL

#define QUEUE_DATA_TYPE int

typedef struct _QUEUE_NODE
{
    QUEUE_DATA_TYPE data;
    struct _QUEUE_NODE *next;
}tQueueNode,*pQueueNode;

typedef struct _LINK_QUEUE
{
    int len;
    pQueueNode header;
    pQueueNode tail;
}tLinkQueue,*pLinkQueue;

extern void create_link_queue_1(pLinkQueue *h);
extern void create_link_queue_2(pLinkQueue h);
extern pLinkQueue create_link_queue_3(void);
extern int is_empty_link_queue(pLinkQueue h);
extern int into_link_queue(pLinkQueue h,QUEUE_DATA_TYPE data);
extern int out_link_queue(pLinkQueue h,QUEUE_DATA_TYPE *data);
extern int display_all_link_queue_node(pLinkQueue h);
extern int free_link_queue(pLinkQueue h);

#endif // LINK_QUEUE_H_INCLUDED

如有错误,欢迎指正

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值