简体   繁体   English

如何创建链表使用从 C 文件中读取的数据

[英]How to create a linked list use data read from file in C

I'm new to C, I want to read a data file that contains integers one at a line and add them into a linked list (I'm being lazy here so did not put the typedef into a .h file, but just put it at the top of my .c file).我是 C 新手,我想读取一个包含一个整数的数据文件,并将它们添加到一个链表中(我在这里很懒,所以没有将 typedef 放入 .h 文件中,而是将其放入它在我的 .c 文件的顶部)。 The structure of linked list is like:链表的结构如下:

typedef struct Node{
    int num;
    struct Node *next;
    
}Node;

Now I wrote some code like following, the .c file compiled but cannot display linked list properly.现在我写了一些像下面这样的代码,.c 文件编译但不能正确显示链表。 I don't know why this is happending but I'm guessing it's related to the while loop condition and fscanf()?我不知道为什么会发生这种情况,但我猜这与 while 循环条件和 fscanf() 有关?

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

typedef struct Node{
    int num;
    struct Node *next;
    
}Node;

Node* createLinkedlist(FILE* input_file, char* filename1);
void displayList(Node *head);

int main() {
    char *filename1 = "data";
    int number;
    FILE *input_file;

    Node *head = NULL;
    head = createLinkedlist(input_file, filename1);
    displayList(head);
    return(0);
    

}


Node* createLinkedlist(FILE* input_file, char* filename1) {
    Node* head = NULL;
    Node* newNodePtr = NULL;
    Node* p = NULL;


    input_file = fopen(filename1, "r"); /*open input file, read data*/
    if(input_file == NULL) {
        printf("Cannot open %s", filename1);
        exit(EXIT_FAILURE);
    } 
    while(fscanf(input_file, "r") != EOF) { 
        /*Create individual Node*/
        newNodePtr = (Node*)malloc(sizeof(Node));
        if(newNodePtr == NULL){
            printf("The storage was NOT allocated.");
            exit(EXIT_FAILURE);
        } else {
            /*Create signle Node data*/
            fscanf(input_file, "%d\n", &(newNodePtr->num));
        }

        if(head == NULL){
            head = newNodePtr;
        }else {
            p = head;
            while(p->next != NULL) {
                p = p->next;
            }
            p->next = newNodePtr;
        }
        
    }
    return head;
    
}

void displayList(Node* head) {
    Node *p = head;

    while(p != NULL){
        printf("\t%d->", p->num);
        p = p->next;
    }
}

Can someone help me to figure out my problem?有人可以帮我找出我的问题吗? Thank you so much, I'm really struggled...非常感谢,我真的很挣扎...

The main problem, you didn't assign NULL value to the proper value.主要问题是,您没有将 NULL 值分配给正确的值。 After get the entry,you must write it:拿到入口后,你必须写:

newNodePtr->next = NULL;

if you don't do that, this doesn't work:如果你不这样做,这将不起作用:

else {
        p = head;
        while(p->next != NULL) {
            p = p->next;
        }
        p->next = newNodePtr;
    }

Because if you don't assign NULL, you won't be able to find NULL value.因为如果您不分配 NULL,您将无法找到 NULL 值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM