【问题标题】:Reading in words into a linked list将单词读入链表
【发布时间】:2015-08-25 04:23:22
【问题描述】:

我正在尝试编写一个程序来读取用户输入的每个单词,然后将该单词粘贴到链接列表中。这是我迄今为止尝试过的,但出现了段错误,但不太确定我在 mallocing/pointers 上哪里出错了。 (还没有实现 printList)。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LEN 20

typedef struct node{
    char *word;
    struct node *next;
}node_t;

node_t *read(node_t *node);
void printList(node_t *node);
node_t *insertNode(char *word, node_t *node, int size);

int main(int argc, char *argv[]) {
    node_t *start = NULL;
    printf("Enter a sentence:\n");
    read(start);



    return 0;
}

void *read(node_t *node){
    int i, size = MAX_LEN;
    char c, *word;
    if(!(word=malloc(size))){
        printf("Out of memory!\n");
        exit(EXIT_FAILURE);
    }
    while((c=getchar())!='\n'){
        for(i=0;c!=' ';i++){
            word[i]=c;
            if(i>size){
                size=size*2;
                if(!realloc(word, size)){
                    printf("Out of memory\n");
                    exit(EXIT_FAILURE);
                }
            }
        }
        node = insertNode(word,node,size);  
    }
    return node;
}

node_t *insertNode(char *word, node_t *node, int size){
    node_t *new_node, *current;
    new_node = (node_t*)malloc(sizeof(node_t));
    new_node->next = NULL;
    if(!(new_node->word = malloc(size))){
        printf("Out of memory\n");
        exit(EXIT_FAILURE);
    }
    strcpy(new_node->word,word);

    if (node == NULL){
        node = new_node;
        current = new_node;
    }

    else{
        current->next = new_node;
        current = new_node;
    }
    return node;
}

【问题讨论】:

    标签: c struct linked-list malloc nodes


    【解决方案1】:

    有几个问题:

    • 您的原型和read 的实现不匹配;使两者都返回node_t *
    • 您有两个用于输入的嵌套循环,一个从 stdin 读取,另一个在字符之间循环。内循环从未更新其条件,因为c 只能由外循环更改。应该只有一个循环,负责从流中读取并写入字符串。
    • 您不保留realloc 的结果,这意味着当分配的内存的句柄发生更改时,您不会反映更新。在这些情况下,您将访问已失效的旧句柄。
    • 不要用空字符终止字符串。
    • 您应该在访问超出范围的内存之前重新分配。这通常意味着在写入之前检查是否扩大数组。请注意,对于长度为 n 的数组,n 本身已经是非法索引。
    • getchar 的结果应该是 int,而不是 char,以便所有有效输入都不同于 EOF,您无需检查。

    可能还有更多问题,列出的是与read 相关的问题。我还没有研究过链表插入。

    为了正确地用零终止字符串,我建议编写一个无限循环并在可能的重新分配之后推迟break 条件。敌人的例子:

    node_t *read(node_t *node)
    {
        int size = MAX_LEN;
        int i = 0; 
        char *word = malloc(size);    
    
        if(word == NULL) {
            printf("Out of memory!\n");
            exit(EXIT_FAILURE);
        }
    
        while (1) {
            int c = getchar();
    
            if(i >= size) {
                size = size*2;
                word = realloc(word, size);
    
                if (word == NULL) {
                    printf("Out of memory\n");
                    exit(EXIT_FAILURE);
                }
            }
    
            if (c == '\n' || c == EOF) {
                word[i] = '\0';
                break;
            }
    
            word[i++] = c;
        }
    
        node = insertNode(word, node, size);
        return node;
    }
    

    【讨论】:

      【解决方案2】:

      我认为错误是由线路引起的

      return node;
      

      insertNode。那应该是

      return new_node;
      

      【讨论】:

        猜你喜欢
        • 2012-06-25
        • 2015-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-19
        • 1970-01-01
        相关资源
        最近更新 更多