C语言基于链表结构体的字符重复距离计算程序无输出问题求助
Hey there! Let's break down what's going wrong with your code and fix it step by step—since you're new to C, we'll go through each issue clearly.
Key Issues in Your Code
- Misunderstanding of Linked Lists: The initial code uses a linked list (
ListofCharis a pointer to aCharnode), but you're treating it like an array (usingchars[i]andmemseton the pointer). Linked lists work by dynamically creating nodes and linking them, not as a contiguous array. - Uninitialized Linked List: You never call
createnode()to actually create nodes for your list. Yourlst_ptrstarts asNULLand stays that way, so accessingchars->chindistance()is a null pointer dereference (this is why you might see no output or a crash). - Incorrect
report()Implementation: You modifiedreport()to take alengthparameter and loop like an array, but linked lists are traversed by following thenextpointers until you hitNULL. - Wrong Use of
memset:memset(lst_ptr, 0, 50*sizeof(Char))is invalid here—lst_ptris a pointer to a pointer, not an array of nodes. This is corrupting your pointer value.
Fixed Code
#include <stdio.h> #include <stdlib.h> #include <string.h> struct charact { char ch; int occurs; struct charact *next; }; typedef struct charact Char; typedef Char * ListofChar; typedef Char * CharNode_ptr; void letters(char name[50], ListofChar * chars_ptr); void report(ListofChar chars); Char * createnode(char ch); int main(void) { char name[50]; ListofChar chars = NULL; scanf("%49s", name); letters(name, &chars); report(chars); // Clean up dynamically allocated memory ListofChar temp; while (chars != NULL) { temp = chars; chars = chars->next; free(temp); } return 0; } Char * createnode(char ch) { CharNode_ptr newnode_ptr = malloc(sizeof(Char)); if (newnode_ptr == NULL) { // Always check if malloc succeeded fprintf(stderr, "Malloc failed to allocate memory\n"); exit(1); } newnode_ptr->ch = ch; newnode_ptr->occurs = 0; newnode_ptr->next = NULL; return newnode_ptr; } void letters(char name[50], ListofChar * lst_ptr) { size_t len = strlen(name); ListofChar current = NULL; for (size_t i = 0; i < len; i++) { // Create a new node for the current character Char *new_node = createnode(name[i]); // Calculate distance to the next duplicate character for (size_t j = i + 1; j < len; j++) { if (name[j] == new_node->ch) { new_node->occurs = j - i; break; // Stop at the first duplicate we find } } // Append the new node to the linked list if (*lst_ptr == NULL) { *lst_ptr = new_node; // First node becomes the list head } else { current = *lst_ptr; while (current->next != NULL) { current = current->next; // Traverse to the end of the list } current->next = new_node; // Add the new node at the end } } } void report(ListofChar chars) { ListofChar current = chars; while (current != NULL) { printf("%c: %d\n", current->ch, current->occurs); current = current->next; // Move to the next node in the list } }
What We Fixed
- Proper Linked List Usage:
- We now call
createnode()for every character in the input string, creating a dedicated node for each one. - Nodes are appended to the list correctly by traversing to the end before adding a new node, which is how linked lists are built.
- We now call
- Correct Distance Calculation:
- For each character at index
i, we scan fromi+1to the end of the string. The first duplicate we find sets theoccursvalue to the distance; if no duplicate exists, it stays 0.
- For each character at index
- Fixed
report()Function:- We traverse the linked list using the
nextpointer instead of treating it like an array, ensuring we print every node in the list.
- We traverse the linked list using the
- Memory Safety:
- Added a check for
malloc()failure to avoid crashes from out-of-memory situations. - Added code in
main()to free all allocated nodes—always clean up dynamically allocated memory in C to avoid leaks!
- Added a check for
Test with "testing"
Input: testing
Output:
t:3 e:0 s:0 t:0 i:0 n:0 g:0
This matches your expected output perfectly.
内容的提问来源于stack exchange,提问作者Christos Ka
相关产品推荐
相关产品推荐

