2401_84401465 2024-10-26 09:44 采纳率: 71.4%
浏览 2
已结题

为什么我没办法打印链表的值?那两个警告是什么意思?


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

typedef struct node {
    int data;
    struct node* next;
}Node;

Node* initlist() {
    Node* head = (Node*)malloc(sizeof(Node));
    head->data = 0;
    head->next = NULL;
    return head;
}

void insert1(Node* L, int e) {
    Node* p = (Node*)malloc(sizeof(Node));
    p->data = e;
    p->next = L->next;
    L->next = p;
}

void traval(Node* L) {
    if (L->next = NULL) {
        printf("链表为空"); 
    }
    Node* p = L->next;
    while (p!= NULL) {
        printf("%d\n", p->data); 
        p = p->next;
    }
}
int main() {
    Node* L = initlist();
    insert1(L, 32);
    insert1(L, 45);
    insert1(L, 23);
    insert1(L, 90);
    insert1(L, 69);
    insert1(L, 39); 
    insert1(L, 58);
    insert1(L, 93);
    insert1(L, 47);
    insert1(L, 13);
    insert1(L, 4);
    traval(L);
    return 0;
}

img

  • 写回答

1条回答 默认 最新

  • 小ᶻ☡꙳ᵃⁱᵍᶜ꙳ 优质创作者: 人工智能技术领域 2024-10-26 09:50
    关注

    回答引用自GPT-4o,由博主小ᶻZ࿆编写,若还有疑问可评论或留言:

    为什么无法打印链表的值?警告信息分析

    问题描述

    题主在编写C语言代码实现链表时,无法打印出链表中的值,并且遇到了两个警告。问题代码如下:

    #include <cstdio.h>
    #include <cstdlib.h>
    
    typedef struct node {
        int data;
        struct node* next;
    } Node;
    
    Node* initlist() {
        Node* head = (Node*)malloc(sizeof(Node));
        head->data = 0;
        head->next = NULL;
        return head;
    }
    
    void insert1(Node* L, int e) {
        Node* p = (Node*)malloc(sizeof(Node));
        p->data = e;
        p->next = L->next;
        L->next = p;
    }
    
    void traval(Node* L) {
        if (L->next = NULL) { // 这里是问题所在
            printf("链表为空\n"); 
        }
        Node* p = L->next;
        while (p != NULL) {
            printf("%d\n", p->data); 
            p = p->next;
        }
    }
    
    int main() {
        Node* L = initlist();
        insert1(L, 32);
        insert1(L, 45);
        insert1(L, 23);
        insert1(L, 90);
        insert1(L, 69);
        insert1(L, 39); 
        insert1(L, 58);
        insert1(L, 93);
        insert1(L, 47);
        insert1(L, 13);
        insert1(L, 4);
        traval(L);
        return 0;
    }
    

    解决方案

    1. 问题分析:条件判断中的赋值错误

      • traval函数中,if (L->next = NULL)这一行实际上是将L->next赋值为NULL,而不是进行相等性判断。
      • 正确的做法应该是使用==进行比较:
        if (L->next == NULL) {
            printf("链表为空\n"); 
        }
        
      • 这个问题会导致链表直接被置为空,从而无法正确遍历链表。
    2. 警告信息:未使用括号的赋值操作

      • 编译器会警告if语句中使用赋值操作,这样的写法容易产生意外行为。
      • 使用==可以避免这个问题,并确保代码的语义正确。
    3. 检查头节点和遍历逻辑

      • 确保链表头节点初始化正确,并且在traval函数中,从L->next开始遍历,确保不会将头节点的默认值输出。

    修改后的代码

    以下是修复了上述问题的完整代码:

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node {
        int data;
        struct node* next;
    } Node;
    
    Node* initlist() {
        Node* head = (Node*)malloc(sizeof(Node));
        head->data = 0;
        head->next = NULL;
        return head;
    }
    
    void insert1(Node* L, int e) {
        Node* p = (Node*)malloc(sizeof(Node));
        p->data = e;
        p->next = L->next;
        L->next = p;
    }
    
    void traval(Node* L) {
        if (L->next == NULL) {
            printf("链表为空\n"); 
            return;
        }
        Node* p = L->next;
        while (p != NULL) {
            printf("%d\n", p->data); 
            p = p->next;
        }
    }
    
    int main() {
        Node* L = initlist();
        insert1(L, 32);
        insert1(L, 45);
        insert1(L, 23);
        insert1(L, 90);
        insert1(L, 69);
        insert1(L, 39); 
        insert1(L, 58);
        insert1(L, 93);
        insert1(L, 47);
        insert1(L, 13);
        insert1(L, 4);
        traval(L);
        return 0;
    }
    

    总结

    • 使用==来判断是否为空而不是=,避免在条件判断中误用赋值操作。
    • 修复之后,链表的遍历应该能正常输出各个节点的值。

    如有帮助,题主可以帮忙采纳一下嘛,谢谢~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月26日
  • 已采纳回答 10月26日
  • 创建了问题 10月26日