NEKO212 2021-12-16 16:58 采纳率: 0%
浏览 4
已结题

c语言单链表关于新加入一个tail指向链表末尾的问题

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

typedef struct node {
    int value;
    struct node* next;    //这里定义了一个struct node类新的指针命名为next,
        struct node* pre;    //为什么他会把节点指向下一个,不清楚他的运作逻辑
}Node;                            //关于pre他在此结构的作用是什么呢  

typedef struct _list {
    Node* head;
    Node* tail;
}List;

void add(List* pList, int number);
void print(List* pList);
void reprint(List* pList);

int main(int argc, char const* argv[])
{
    List list;
    list.head = NULL;
    list.tail = NULL;
    int number = 0;

    do {
        scanf_s("%d", &number);
        if (number != -1) 
        {
            add(&list, number);
        }
    } while (number != -1);
    reprint(&list);

    return 0;
}

void add(List* pList, int number)
{
    Node* p = (Node*)malloc( sizeof(Node) );
    p->value = number;
    p->next = NULL;

    if (pList->head)
    {
        p->pre = pList->tail;
        pList->tail->next = p;               //此时这个tail的作用是什么,他的运作逻辑又是怎样的呢
        pList->tail = p;
    }
    else 
    {
        pList->head = p;
        pList->tail = p;
        pList->tail->pre = NULL;
    }
}

void print(List* pList) {
    Node* p;
    for (p = pList->head; p; p = p->next) {
        printf("%d ", p->value);
    }
    printf("\n");
}
void reprint(List* pList) {
    Node* p;
    for (p = pList->tail; p; p = p->pre) {
        printf("%d ", p->value);
    }
    printf("\n");
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 12月24日
    • 创建了问题 12月16日

    悬赏问题

    • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
    • ¥50 comsol温度场仿真无法模拟微米级激光光斑
    • ¥15 上传图片时提交的存储类型
    • ¥15 Ubuntu开机显示器只显示kernel,是没操作系统(相关搜索:显卡驱动)
    • ¥15 VB.NET如何绘制倾斜的椭圆
    • ¥15 arbotix没有/cmd_vel话题
    • ¥15 odoo17的分包重新供应路线如何设置?可从销售订单中实时直接触发采购订单或相关单据
    • ¥15 用C语言怎么判断字符串的输入是否符合设定?
    • ¥15 通信专业本科生论文选这两个哪个方向好研究呀
    • ¥50 我在一个购物网站的排队系统排队,这个排队到号后重新定向到目标网站进行购物,但是有技术牛通过技术方法直接跳过排队系统进入目标网址购物,有没有什么软件或者脚本可以用