CFXLWT 2022-02-21 01:46 采纳率: 81.5%
浏览 32
已结题

翁恺C语言程序设计入门 链表课的两个问题(写在代码里)


typedef struct _node {
    int value;
    struct _node* next;
}Node;
 
int main(int argc, char* argv[])
{
    Node* head = (Node*)malloc(sizeof(Node));
    int number;
    do {
        scanf("%d", &number);
        if (number != -1) {
         Node* p = (Node*)malloc(sizeof(Node));
         p->value = number;
         p->next = NULL;
         Node* last = head;
         if( last ){
           while (last->next) {//问题1:引发了未经处理的异常:读取访问权限冲突。 last0xCDCDCDCDlast = last->next;//问题2:左边的last会得到右边last->next的什么?是地址吗?是last->next所指向的地址吗?
            printf("hellow");
           }
          last->next = p;
         }
         else {
             head = p;
         }
        }
    } while (number != -1);
}
  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-02-21 02:06
    关注

    修改如下,见注释,供参考:

    #include <stdio.h>
    #include <malloc.h>
    typedef struct _node {
        int   value;
        struct _node* next;
    }Node;
    
    int main(int argc, char* argv[])
    {
        Node* head = (Node*)malloc(sizeof(Node));
        head->next = NULL;   //问题1:头结点的 next 指针没有初始化,是个随机值,所以在输入第一结点值时 last->next != NULL 
        int number;          //条件满足,造成误判断。
        do {
            scanf("%d", &number);
            if (number != -1) {
                Node* p = (Node*)malloc(sizeof(Node));
                p->value = number;
                p->next = NULL;
                Node* last = head; 
                if (last) {
                    while (last->next) {//问题1:引发了未经处理的异常:读取访问权限冲突。 last0xCDCDCDCD
                        last = last->next;//问题2:左边的last会得到右边last->next的什么?是地址吗?是last->next所指向的地址吗?
                        printf("hellow\n");//问题2:理解正确。
                    }
                    last->next = p;
                }
                else {
                    head = p;
                }
            }
        } while (number != -1);
    }
    
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
    CFXLWT 2022-02-21 02:13

    原来如此!谢谢兄弟!!!讲的真好👍👍

    回复
查看更多回答(1条)
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 2月28日
  • 已采纳回答 2月21日
  • 创建了问题 2月21日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部