如何从标准输入中读取多行并将其存储在链表中

4

我正试图编写一个程序,从用户(从STDIN)获取行并将它们存储在一个链表中。

现在我只能获取一行并终止程序。如何更改代码以不断从stdin获取行?

另外,如果有人告诉我是否正在按照应该分配和释放内存的方式进行操作,将非常有帮助。

谢谢。

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

int BUFF_SIZE = 128;

struct Node {
    char* data;
    struct Node* next;
};

struct Node* head = NULL;
struct Node* tail = NULL;

void free_list(struct Node* head)
{
    if (head != NULL)
    {
        free_list(head->next);
        free(head);
    }
}

int main()
{
    int curr_size = 0;

    char* pStr = malloc(BUFF_SIZE);
    curr_size = BUFF_SIZE;

    printf("%s", "please print multiple lines\n");
    if (pStr != NULL)
    {
        char c;
        int i = 0;

        while ((c = getchar()) != '\n' && c != EOF)
        {
            pStr[i++] = c;

            if (i == curr_size)
            {
                curr_size = i + BUFF_SIZE;
                pStr = realloc(pStr, curr_size);
                if (pStr == NULL) return;
            }
        }
        pStr[i] = '\0';

        struct Node* new_node = malloc(sizeof(struct Node*));
        char* new_data = malloc(sizeof(pStr));
        new_data = pStr;
        new_node->data = new_data;

        if (head == NULL)
        {
            head = new_node;
            tail = new_node;
        }

        else
        {
            tail->next = new_node;
        }
    }

    free_list(head);
}
1个回答

6
一些问题:
  1. As of now you are terminating the reading upon reacieving the \n.

    if (pStr == NULL) return; //error
    
    int c;
    int i = 0;
    
    while ((c = getchar()) != EOF)
    {
       /*New word, insert into linked list*/
       if (c == '\n'){
           pStr[i] = '\0';
    
           struct Node* new_node = malloc(sizeof(*new_node));
           char* new_data = malloc(i+1);
           strcpy(new_data, pStr);
           new_node->data = new_data;
    
           if (head == NULL)
           {
                head = new_node;
                tail = new_node;
           }
           else
           {
                tail->next = new_node;
                tail = new_node;
           }
           i = 0; //Reset the index
       }
       else {
    
           pStr[i++] = c;
           if (i == curr_size)
           {
               curr_size = i + BUFF_SIZE;
               pStr = realloc(pStr, curr_size);
               if (pStr == NULL) return;
           }
       }
    }
    
  2. Memory leaks and node data will be always pointing to latest content of pStr.

    char* new_data = malloc(sizeof(pStr)); 
    new_data = pStr;   //Memory leak here
    new_node->data = new_data;
    

    change it to

    char* new_data = malloc(i+1);
    strcpy(new_data, pStr);
    new_node->data = new_data;
    

    sizeof(pStr) is size of pointer not the string length.

  3. You need to update tail after each node is inserted to list.

     else
     {
         tail->next = new_node;
     }
    

    to

     else
    {
        tail->next = new_node;
        tail = new_node;
     }
    

抱歉,我是新来的,我该怎么做? - beginnerCoder
1
在答案旁边的点赞计数器下面,有一个复选标记。点击它,它应该变成绿色。另外,需要注意的是,这段代码仍然存在问题。c 应该是 int,而不是 char - WhozCraig
@WhozCraig 谢谢,为什么它应该是 int 类型? - beginnerCoder
3
因为这就是 getchar() 返回的内容,而这非常重要,尤其是在与 EOF 进行比较时(正如您所做的那样)。 - WhozCraig
1
struct Node* new_node = malloc(sizeof(struct Node*)); (copied from the original source) is wrong. Must be sizeof(struct Node) or sizeof *new_node - Ingo Leonhardt
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接