链表尾插函数问题求助:尾节点指针未指向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 declaredstruct node *node;but never set it toNULL. When you first callAddToList(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
AddToListfunction 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'snexttoNULL, so theshowlisttraversal 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
- Valid Initial List:
struct node *node = NULL;ensures we start with an empty, properly defined list instead of a wild pointer. - Proper Tail Insertion:
- We set the new node's
nexttoNULLimmediately, 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).
- We set the new node's
- Malloc Safety: Added a check for
mallocfailure to avoid unexpected crashes if memory can't be allocated. - Memory Cleanup: Added code to free all nodes at the end to prevent memory leaks.
- Safe Traversal: The
showlistfunction stops when it hitsNULL, 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
相关产品推荐
相关产品推荐

