You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

链表尾插函数问题求助:尾节点指针未指向NULL致遍历报错

Fixing Your Linked List Tail Insertion & Traversal Crash

Hey there, let's break down why your code is triggering that system error during traversal and get your tail-insertion function working correctly.

The Core Issues in Your Current Code

  • Uninitialized Pointer in main: You declared struct node *node; but never set it to NULL. When you first call AddToList(node, 30), you're passing a wild pointer (points to random, invalid memory) as the initial list head. This corrupts your linked list right from the start.
  • Incorrect Insertion Logic: Your AddToList function is doing head insertion, not the tail insertion you intended. Even if it worked, it would build the list in reverse order (10, 20, 30 instead of 30, 20, 10). Worse, you never set any node's next to NULL, so the showlist traversal will run off the end of the list and access invalid memory, causing the crash.

Corrected Code with Proper Tail Insertion

Let's fix the AddToList function, the uninitialized pointer, and add a working showlist function to test everything:

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

struct node{ 
    int value; 
    struct node *next; 
};

struct node *AddToList (struct node *list, int n);
void showlist(struct node *list);

int main() {
    // Start with an empty, valid list (initialize to NULL)
    struct node *node = NULL;
    node = AddToList(node, 30);
    node = AddToList(node, 20);
    node = AddToList(node, 10);
    showlist(node);
    
    // Optional: Clean up memory to avoid leaks
    struct node *temp;
    while (node != NULL) {
        temp = node;
        node = node->next;
        free(temp);
    }
    
    return 0;
}

struct node *AddToList (struct node *list, int n){
    struct node *new_node = malloc(sizeof(struct node));
    if (new_node == NULL) { // Always check if malloc succeeded!
        fprintf(stderr, "Failed to allocate memory\n");
        exit(1);
    }
    new_node->value = n;
    new_node->next = NULL; // Critical: New tail node must point to NULL
    
    // If list is empty, new node becomes the head
    if (list == NULL) {
        return new_node;
    }
    
    // Traverse to the last node in the existing list
    struct node *current = list;
    while (current->next != NULL) {
        current = current->next;
    }
    
    // Link the last node to the new tail node
    current->next = new_node;
    return list; // Return original head (tail insertion doesn't change the head)
}

void showlist(struct node *list) {
    struct node *current = list;
    while (current != NULL) {
        printf("%d -> ", current->value);
        current = current->next;
    }
    printf("NULL\n");
}

What Changed & Why

  1. Valid Initial List: struct node *node = NULL; ensures we start with an empty, properly defined list instead of a wild pointer.
  2. Proper Tail Insertion:
    • We set the new node's next to NULL immediately, guaranteeing the list always terminates correctly.
    • If the list is empty, the new node becomes the head.
    • For non-empty lists, we traverse to the last node (where current->next == NULL) and link it to the new node.
    • We return the original head (unlike head insertion, tail insertion never modifies the list's starting pointer).
  3. Malloc Safety: Added a check for malloc failure to avoid unexpected crashes if memory can't be allocated.
  4. Memory Cleanup: Added code to free all nodes at the end to prevent memory leaks.
  5. Safe Traversal: The showlist function stops when it hits NULL, so it never accesses invalid memory.

When you run this code, it will output:

30 -> 20 -> 10 -> NULL

No system errors, since the list properly terminates with a NULL pointer.

内容的提问来源于stack exchange,提问作者Walter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:42:40