运行C语言链表操作程序出现undefined reference error,求错误解析
Hey there! Let's clear up those confusing undefined reference errors you're hitting—they're a common pitfall in C, and once you understand what's happening, fixing them is straightforward.
What Do These Errors Mean?
When you see undefined reference to create_list or similar, this is a linker error, not a compiler error. Here's the breakdown:
- Your code has function declarations (like
struct node * create_list(struct node * start);), which tell the compiler "this function exists somewhere, trust me." - But the linker (the tool that puts all your code together into an executable) can't find the actual implementation (the body of the function where the work happens) for these declared functions. It needs that code to build a working program.
Looking at your code, you've declared all the linked list functions you want to use, but you haven't written any of their actual code—you only have the main() function! That's why the linker is complaining.
How to Fix This
You need to add concrete implementations for every function you've declared. Let's walk through examples for a couple of your functions, and you can follow the pattern for the rest:
Example 1: Implementing display()
This function will print out the linked list contents:
void display(struct node * start) { struct node *current = start; if (start == NULL) { printf("The list is empty!\n"); return; } printf("Linked List: "); while (current != NULL) { printf("%d ", current->info); current = current->link; } printf("\n"); }
Example 2: Implementing create_list()
This function will let the user input nodes to build a new list:
struct node * create_list(struct node * start) { int num_nodes, i, data; printf("Enter number of nodes to create: "); scanf("%d", &num_nodes); if (num_nodes == 0) { printf("Created an empty list.\n"); return NULL; } // Create first node printf("Enter data for node 1: "); scanf("%d", &data); start = (struct node *)malloc(sizeof(struct node)); start->info = data; start->link = NULL; // Add remaining nodes struct node *last_node = start; for (i = 2; i <= num_nodes; i++) { printf("Enter data for node %d: ", i); scanf("%d", &data); struct node *new_node = (struct node *)malloc(sizeof(struct node)); new_node->info = data; new_node->link = NULL; last_node->link = new_node; last_node = new_node; } printf("List created successfully!\n"); return start; }
Repeat for All Declared Functions
You'll need to write similar implementations for:
count(): Count the number of nodes in the listsearch(): Check if a value exists in the listaddatbeg(): Add a node to the start of the listaddatend(): Add a node to the end of the listaddafter(): Add a node after a specific valueaddbefore(): Add a node before a specific valueaddatpos(): Add a node at a specific positiondel(): Delete a node with a specific valuereverse(): Reverse the order of the list
Quick Bug Note in Your Existing Code
While fixing the linker errors, keep an eye out for a couple small mistakes in your main() function:
- In
case 8, you're usingitembut reading input intopos(mismatched variable name) - In
case 9, you're reading both the element and position intodata(you'll overwrite the element value—useposfor the second input)
Once you add all the function implementations, recompile your code, and those undefined reference errors should disappear!
内容的提问来源于stack exchange,提问作者Bikrant Koirala

