C语言:数据结构-队列(无头结点链队列)

目录

一、结构体

1.头尾指针节点

2.队列元素节点

二、创建

1.头尾指针节点

2.创建队列元素节点

三、判空

四、入队(尾插)

五、出队(头删)

六、打印

七、清空

八、销毁

一、结构体

1.头尾指针节点

typedef struct
{
    node front; //头指针
    node rear;  //尾指针
}link_que, *linkque;

2.队列元素节点

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

二、创建

1.头尾指针节点

linkque create_link_node()
{
    linkque Q = (linkque)malloc(sizeof(link_que));
    if (Q == NULL)
    {
        printf("指针节点失败\n");
        return NULL;
    }
    Q->front = NULL;
    Q->rear = NULL;
    return Q;
}

2.创建队列元素节点

node create_node(int data)
{
    node Q = (node)malloc(sizeof(Node));
    if (Q == NULL)
    {
        printf("入参为空\n");
        return NULL;
    }
    Q->next = NULL;
    Q->data = data;
    return Q;
}

三、判空

int empty_linkque(linkque Q)
{
    if (Q == NULL)
    {
        printf("入参为空\n");
        return -1;
    }
    return Q->rear == NULL ? 1 : 0;
}

四、入队(尾插)

void push_linkque(linkque Q, int data)
{
    if (Q == NULL)
    {
        printf("入参为空\n");
        return;
    }
    node newnode = create_node(data);
    if (empty_linkque(Q))
    {
        Q->front = newnode;
        Q->rear = newnode;
        return;
    }
    Q->rear->next = newnode;
    Q->rear = newnode;
}

五、出队(头删)

int pop_linkque(linkque Q)
{
    if (Q == NULL)
    {
        printf("入参为空\n");
        return -1;
    }
    if (empty_linkque(Q))
    {
        printf("链队为空,无法出队\n");
        return -1;
    }
    node del = Q->front;
    if (Q->front == Q->rear)
    {
        int num = del->data;
        free(del);
        del = NULL;
        Q->front = NULL;
        Q->rear = NULL;
        return num;
    }
    Q->front = del->next;
    int num = del->data;
    free(del);
    del = NULL;
    return num;
}

六、打印

void print_linkque(linkque Q)
{
    if (Q == NULL)
    {
        printf("入参为空\n");
        return;
    }
    if (empty_linkque(Q))
    {
        printf("链队为空\n");
        return;
    }
    node S = Q->front;
    while (S != NULL)
    {
        printf("Q->data = %d\n", S->data);
        S = S->next;
    }
}

七、清空

void clean_linkque(linkque Q)
{
    if (Q == NULL)
    {
        printf("入参为空\n");
        return;
    }
    if (empty_linkque(Q))
    {
        printf("链队为空\n");
        return;
    }
    while (!empty_linkque(Q))
    {
        pop_linkque(Q);
    }
    printf("链队已清空\n");
}

八、销毁

void destory_linkque(linkque *Q)
{
    if (*Q == NULL)
    {
        printf("入参为空\n");
        return;
    }
    clean_linkque(*Q);
    free(*Q);
    *Q = NULL;
    printf("链队已销毁\n");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值