Oilfacer 2021-10-21 18:41 采纳率: 0%
浏览 12
已结题

感觉还是指针没学清楚,搞不清楚这边last和pList->head->next的区别了



```c
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
typedef struct _node{
    int value;
    struct _node *next;
}Node;

typedef struct _list{
    Node* head;
    //Node* tail;
}List;
void print(List* pList);
void add(List* pList,int number);

int main(int arge,char const *argv[])
{
        List* list;
        list=(List*)malloc(sizeof(struct _list)); 
        int number;
        list->head=(Node*)malloc(sizeof(struct _node));
        list->head->next=NULL; 
        do{
            scanf("%d",&number);
            if(number!=-1){
                /*list->head=*/add(list,number);
            }
        }while(number!=-1);
        print(list);
        return 0;
}

void add(List* pList,int number)
{
       // add to linked-list
       Node*p=new Node;
       p->value=number;
       p->next=NULL;
       // find the last
       Node *last=pList->head->next;
       if(last){
           while(last->next){
               last=last->next;
           }
           //  attach
           last->next=p;
       }else{
               pList->head->next=p; 
               //这边改last就直接错了,不知道为啥??? 
       }
}
void print(List* pList)
{
    Node* p;
    p=pList->head->next;
    if(p){
        while(p)
        {
            printf("%d",p->value);
            p=p->next;
        }
    }else{
        printf("只有一个结点%d喔!\n",p->value);
    }
}


```

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 10月29日
    • 创建了问题 10月21日

    悬赏问题

    • ¥15 fesafe材料库问题
    • ¥35 beats蓝牙耳机怎么查看日志
    • ¥15 Fluent齿轮搅油
    • ¥15 八爪鱼爬数据为什么自己停了
    • ¥15 交替优化波束形成和ris反射角使保密速率最大化
    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统