CFXLWT 2022-02-21 09: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:引发了未经处理的异常:读取访问权限冲突。 last 是 0xCDCDCDCD。
            last = last->next;//问题2:左边的last会得到右边last->next的什么?是地址吗?是last->next所指向的地址吗?
            printf("hellow");
           }
          last->next = p;
         }
         else {
             head = p;
         }
        }
    } while (number != -1);
}
  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-02-21 10: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:引发了未经处理的异常:读取访问权限冲突。 last0xCDCDCDCDlast = last->next;//问题2:左边的last会得到右边last->next的什么?是地址吗?是last->next所指向的地址吗?
                        printf("hellow\n");//问题2:理解正确。
                    }
                    last->next = p;
                }
                else {
                    head = p;
                }
            }
        } while (number != -1);
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月1日
  • 已采纳回答 2月21日
  • 创建了问题 2月21日

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题