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

C语言中结合结构体实现数组字符串转指针及字符次数统计

Solution for Character Count Linked List Implementation

Got it, let's fill in the missing letters and report functions to make this code work as intended. Here's the breakdown and complete code:

Implementing the letters Function

This function needs to iterate through each character in the input string, check if it already exists in our linked list:

  • If the character is already present, increment its occurrence count.
  • If not, create a new node using the provided createnode function, set its occurrence to 1, and add it to the end of the list (to preserve the order of first appearance).
void letters(char name[50], ListofChar * lst_ptr) {
    int i;
    for(i=0; name[i]!='\0'; i++){
        char current_ch = name[i];
        CharNode_ptr temp = *lst_ptr;
        CharNode_ptr prev = NULL;
        
        // Traverse the list to check if the character exists
        while (temp != NULL) {
            if (temp->ch == current_ch) {
                temp->occurs++;
                break;
            }
            prev = temp;
            temp = temp->next;
        }
        
        // If character not found, create new node and add to list
        if (temp == NULL) {
            CharNode_ptr new_node = createnode(current_ch);
            new_node->occurs = 1; // Initialize count to 1 since it's the first occurrence
            if (*lst_ptr == NULL) {
                // List is empty, set new node as head
                *lst_ptr = new_node;
            } else {
                // Append to the end of the list
                prev->next = new_node;
            }
        }
    }
    return;
}

Implementing the report Function

This function simply traverses the linked list and prints each character along with its occurrence count in the specified format. We'll loop through each node until we reach the end of the list.

void report(ListofChar chars) {
    CharNode_ptr temp = chars;
    while (temp != NULL) {
        printf("%c : %d ", temp->ch, temp->occurs);
        temp = temp->next;
    }
    // Optional: Print a newline to clean up the output
    printf("\n");
    return;
}

Full Updated Code

Here's the complete code with both functions filled in, ready to compile and run:

#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() { 
    char name[50]; 
    ListofChar chars = NULL; 
    scanf("%s", name); 
    letters(name, &chars); 
    report(chars); 
    return 0; 
} 

Char * createnode(char ch) { 
    CharNode_ptr newnode_ptr ; 
    newnode_ptr = malloc(sizeof (Char)); 
    newnode_ptr -> ch = ch; 
    newnode_ptr -> occurs = 0; 
    newnode_ptr -> next = NULL; 
    return newnode_ptr; 
} 

void letters(char name[50], ListofChar * lst_ptr) {
    int i;
    for(i=0; name[i]!='\0'; i++){
        char current_ch = name[i];
        CharNode_ptr temp = *lst_ptr;
        CharNode_ptr prev = NULL;
        
        // Traverse the list to check if the character exists
        while (temp != NULL) {
            if (temp->ch == current_ch) {
                temp->occurs++;
                break;
            }
            prev = temp;
            temp = temp->next;
        }
        
        // If character not found, create new node and add to list
        if (temp == NULL) {
            CharNode_ptr new_node = createnode(current_ch);
            new_node->occurs = 1; // Initialize count to 1 since it's the first occurrence
            if (*lst_ptr == NULL) {
                // List is empty, set new node as head
                *lst_ptr = new_node;
            } else {
                // Append to the end of the list
                prev->next = new_node;
            }
        }
    }
    return;
} 

void report(ListofChar chars) {
    CharNode_ptr temp = chars;
    while (temp != NULL) {
        printf("%c : %d ", temp->ch, temp->occurs);
        temp = temp->next;
    }
    printf("\n");
    return;
} 

Testing the Code

If you input Hello, the output will be:

H : 1 e : 1 l : 2 o : 1 

(The trailing space is harmless, but if you want to avoid it, you could add a check to print the separator only after the first element, but that's an optional refinement.)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:37:22