链表式队列(C语言)——(2)

本节:使用C语言实现链表式队列的创建、入队、出队。启用头结点不存储数据,以第二个节点作为开始节点。

main.c

//
//  main.c
//  learn
//
//  Created by wang shang on 2021/7/10.
//

#include <stdio.h>
#include <string.h>
#include "link.h"
#include "quene.h"

int main(int argc, const char * argv[]) {
#if 1   //链表式队列
    int flag = 0;
    DataType val = 0;
    
    T_Qnode *s = InitQuene();
    if(s==NULL)
    {
        return false;
    }
    T_Qnode *rear, *head;
    rear=head=s;
    rear = Qpush(rear, 100);
    rear = Qpush(rear, 200);
    rear = Qpush(rear, 300);
    
    rear = Qpop(head, rear, &val);
    printf("Qpop flag is %d, val is %d\n", flag, val);
    rear = Qpop(head, rear, &val);
    printf("Qpop flag is %d, val is %d\n", flag, val);
    rear = Qpop(head, rear, &val);
    printf("Qpop flag is %d, val is %d\n", flag, val);
#endif
    return true;
}

config.h

//
//  config.h
//  learn
//
//  Created by wang shang on 2021/7/17.
//

#ifndef config_h
#define config_h

typedef enum{
    false,
    true,
}Status;

#endif /* config_h */

quene.h

//
//  quene.h
//  learn
//
//  Created by wang shang on 2021/7/17.
//

#ifndef quene_h
#define quene_h

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

typedef int DataType;

typedef struct Qnode{
    DataType data;
    struct Qnode *next;
}T_Qnode, *P_Qnode;

//初始化队列
T_Qnode *InitQuene();
//入队
T_Qnode *Qpush(T_Qnode *rear, DataType val);
//出队
T_Qnode *Qpop(T_Qnode *head, T_Qnode *rear, DataType *val);

#endif /* quene_h */

quene.c

//
//  quene.c
//  learn
//
//  Created by wang shang on 2021/7/17.
//

#include <stdlib.h>
#include "quene.h"

//链表式队列 判断是否为空 入队 出队 遍历
//初始化队列
T_Qnode *InitQuene()
{
    //创建节点头,节点头不保存数据,数据从下一个节点的开始节点头存储
    T_Qnode *rear = (T_Qnode *)malloc(sizeof(T_Qnode));
    if(rear==NULL)
    {
        printf("InitQuene rear is null\n");
        return NULL;
    }
    rear->next=NULL;
    return rear;
}

//入队
T_Qnode *Qpush(T_Qnode *rear, DataType val)
{
    printf("rear address is %d *rear is %d &rear is %d\n", rear, *rear, &rear);
    //创建新的节点空间
    T_Qnode *NewNode = (T_Qnode *)malloc(sizeof(T_Qnode));
    if(NewNode==NULL)
    {
        printf("push NewNode is null\n");
        return NULL;
    }
    //新节点赋值
    NewNode->data = val;
    //新节点下节点地址赋值
    NewNode->next = NULL;
    //将新节点地址赋值给上一个节点的next
    rear->next = NewNode;
    //将入队指针rear指向最后一个节点地址
    rear = NewNode;
    //返回入列地址,参数*rear,使用rear改变值,退出函数不生效,return rear才可以保存修改值。
    //rear = NewNode操作是将rear指针变量指向新的地址,并不是
    return rear;
}

//出队
T_Qnode *Qpop(T_Qnode *head, T_Qnode *rear, DataType *val)
{
    //判断队列是否为空
    if(head->next == NULL)
    {
        printf("pop head is null,quene is empty\n");
        return rear;
    }
    //摘除待出队节点
    T_Qnode *temp = head->next;
    //出队数据
    *val = temp->data;
    printf("%d\n", (*val));
    //将出队指针head指向下一个节点地址,即将节点头的next地址重新赋值
    head->next = temp->next;
    //判断出队数量达到入队数量时,表示队列已空,需要将入队指针rear返回初始与出队一样的地址
    if(rear==temp)
    {
        rear = head;
    }
    //释放摘除的节点
    free(temp);
    return rear;
}

运行结果:

rear address is 5450480 *rear is 0 &rear is 0
rear address is 5448240 *rear is 100 &rear is 0
rear address is 5448304 *rear is 200 &rear is 0
100
Qpop flag is 0, val is 100
200
Qpop flag is 0, val is 200
300
Qpop flag is 0, val is 300
Program ended with exit code: 1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坏一点

您的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值