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

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 (ListofChar is a pointer to a Char node), but you're treating it like an array (using chars[i] and memset on 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. Your lst_ptr starts as NULL and stays that way, so accessing chars->ch in distance() is a null pointer dereference (this is why you might see no output or a crash).
  • Incorrect report() Implementation: You modified report() to take a length parameter and loop like an array, but linked lists are traversed by following the next pointers until you hit NULL.
  • Wrong Use of memset: memset(lst_ptr, 0, 50*sizeof(Char)) is invalid here—lst_ptr is 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

  1. 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.
  2. Correct Distance Calculation:
    • For each character at index i, we scan from i+1 to the end of the string. The first duplicate we find sets the occurs value to the distance; if no duplicate exists, it stays 0.
  3. Fixed report() Function:
    • We traverse the linked list using the next pointer instead of treating it like an array, ensuring we print every node in the list.
  4. 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!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 21:28:16