1

I'm teaching myself about linked lists and have proposed a basic problem to solve. I want to line by line read a text file that will have names and add each name to my linked list.

An example of the text file would be:

John
Jacob
Jingleheimer
Smith

I am having trouble figuring out how to dynamically add to my proposed linked list. Here is what I have so far.

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



int main(void)
{
    struct node {
        char *name;
        struct node* next;
    };


    static const char* fileName = "test.txt";
    FILE *fp = fopen(fileName,"r");
    char *line = NULL;
    size_t len = 0;
    ssize_t read;

    struct node* head = NULL; // first node

    if (fp == NULL)
        exit(EXIT_FAILURE);

    while ((read = getline(&line, &len, fp)) != -1)
    {
        //add line of text to linked list
    }
    if (line)
        free(line);

       exit(EXIT_SUCCESS);

}

Any pointers into the right direction would be helpful.

0

2 Answers 2

3

Ok, to do this you need to first allocate a node entry, then copy the line you just read into it. Then add it to the list. (I've left out error handling, such as malloc returning NULL).

/* This will store the last read line first */
while ((read = getline(&line, &len, fp)) != -1)
{
    struct node *n = malloc(sizeof(*n));
    n->name = strdup(line); /* copy the line since it can get reused by getline */
    n->next = head;
    head = n;
}
Sign up to request clarification or add additional context in comments.

5 Comments

I need help with the list insert part.
Is this compatible with my existing linked list structure? I am getting errors
You probably need to add #include <string.h> but other than that it works for me with no warnings or errors.
I got it working. Do you have any tips for linked where items will have "child" nodes and I will have to link those as well
you mean how to splice two lists together? Search for the end of one of the lists, and add the other there. Something like this (the semi colons are important!): for(n = head2; n->next != NULL; n=n->next); n->next = head; head = head2;
0

Allocate memory to hold the string, set that memory = to the string. Then

node *next = malloc(sizeof(node));
next->name = name;
next->next = NULL;
head->next = next;
head = next;

4 Comments

So I put that into my loop and that is all?
No problem, but the code will segfault too. head is NULL the first time through the loop.
I'm assuming he realizes that and will make it not NULL before entering the loop.
If you're assuming it, you should say so, because the OP won't assume what you're assuming. Unstated assumptions wreck many projects; ask the people at NASA who crashed the Mars Climate Orbiter because they didn't check the units, or the people in the ESA who crashed an Ariane 5 rocket after take-off because the code needed before take-off ran into problems with overflows after take-off.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.