Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1069369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:24:49+00:00 2026-05-16T20:24:49+00:00

I am having some trouble running this linked list implementation (containing words as data).

  • 0

I am having some trouble running this linked list implementation (containing words as data). Problem is when I try to print the words (that I inserted) in the Linked List,I get nothing. What am I doing wrong, breaking my head over this? I hope it’s not something silly. Anyway here’s the code –

typedef struct node
{
    void *data;
    struct node *next;
} NODE;

NODE *new_node(void *data)
{
    NODE *new = malloc(sizeof(NODE));
    if(new)
    {
        new->data = data;
        new->next = NULL;
        return new;
    }
    else
    {
        return NULL;
    }
}

void print_list(NODE *head, void (print_fn) (void*))
{
    if(head && head->next)
    {
        while(head->next)
        {
            if(print_fn)
                print_fn(head->data);
            else
                printf("Word: %s\n", (char *)head->data);
            head = head->next;
        }
    }
    return;
}

void append(NODE **head, NODE *node)                                                                                  
{
    NODE *tmp = *head;
    if(tmp && node)
    {
        while(tmp->next)
            tmp = tmp->next; 
        (*head)->next = node; /*add as last node*/
    }
    return;
}


NODE *create_list()
{
    FILE *dict_file = fopen("trial.txt", "r");

    if(dict_file)
    {
        NODE *head = new_node(NULL);
        if(!head) return NULL;

        char word[20];
        int first  = TRUE;
        memset(word, '\0', 20);

        while(fgets(word, sizeof(word), dict_file) != NULL )
        {
            if(first)
            {
                head->data = (void*)word;
                first = FALSE;
            }
            else
            {
                append(&head, new_node((void*)word));
            }
        }
        fclose(dict_file);
        return head;
    }
    else
    {
        printf("ERROR: File not found");
        return NULL;
    }
}

int main(int argc, char *argv[])
{
    NODE *head = create_list();

    if(!head)
    {
        printf("ERROR: Either malloc() failed or data not found\n");
        return FALSE;
    }
    else
    {
        print_list(head, NULL);
        return TRUE;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:24:49+00:00Added an answer on May 16, 2026 at 8:24 pm

    This has become quite a long answer. Don’t take this personally, but you have made quite a few rookie mistakes. I have met with many people at university whom I helped to learn the C language and programming in general, so I’ve got used to notice these kinds of things.

    The important issues I could find

    • You assign a pointer to a stack variable to words
      This is completely bad, because that value will be overwritten once the execution gets out of the function that creates it. Solution: copy the content of that variable into a heap variable.

    • Your append function is faulty
      It adds the appended element to the second place instead of the last place. Note that you don’t need the return at the end either. There is also no point in requiring a double-pointer as input to the append method. Also, after assigning head to tmp, it is futile to check tmp against NULL as well, since it won’t be NULL if head isn’t NULL. Also, I recommend checking the new node against NULL as well. If it is NULL, this saves you from iterating over the entire collection.

    • The create_list function is sub-optimal
      Fist, the distinction between the first and the other cases are futile. Introducing another pointer (called current in my code) will eliminate the need to check whether it is the first or not. Next, you always call the append function on the head, thus you always need to iterate over the entire collection. This can be also optimized by introducing the current variable. (Which, at start, should be assigned the value of head.)

    • The print_list function is errornous
      It doesn’t print anything if there is only one node. It also redundantly checks the pointers for null. (The beginning of the loop checks it, too.) The return statement at the end of this void function is also unnecessary.

    • You should free up memory when you don’t use it
      @Baltasarq wrote a nice clear function in his answer, you should use it. 🙂

    Not serious errors, but you should be aware of them

    • You should not use void* instead of char*
      If you know that the data member of the NODE structure is going to store characters, why do you use void*? It is bad practice! (Unless you have a good reason, of course.)

    • Using the new word as a variable name makes your code not compliant with C++. Thus, I recommend against it.

    • Adopt a better coding style, please – it will make your code so much easier to read

    • Inconsistency: if in print_list you don’t allocate a new variable to go through the collection (like you do with the tmp variable in append) then it is misguiding to name the parameter as head. (I renamed it to node in my code.)

    Here is the fixed code

    (Note that there may be small syntax errors because I typed the code into the browser without actually testing it.)

    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    typedef struct node
    {
        void *data;
        struct node *next;
    } NODE;
    
    NODE *new_node(void *data)
    {
        NODE *newNode = (NODE*)malloc(sizeof(NODE));
        if (newNode)
        {
            newNode->data = data;
            newNode->next = NULL;
            return newNode;
        }
        return NULL;
    }
    
    void append(NODE *head, NODE *node)
    {
        if (head && node)
        {
            NODE *tmp = head;
            while (tmp->next)
                tmp = tmp->next; 
            tmp->next = node; /* add as last node */
        }
    }
    
    void print_list(NODE *node, void (print_fn) (void*))
    {
        while (node)
        {
            if (print_fn)
                print_fn(node->data);
            else
                printf("Word: %s\n", (char *)node->data);
    
            node = node->next;
        }
    }
    
    NODE *create_list()
    {
        FILE *dict_file = fopen("trial.txt", "r");
    
        if (dict_file)
        {
            NODE *head = NULL;
            NODE *current = head;
    
            char word[20];
            memset(word, '\0', 20);
    
            while (fgets(word, sizeof(word), dict_file))
            {
                // Creating a variable on the heap
                char *data = calloc(sizeof(word) + 1, sizeof(char));
                // Copying the contents of words to it
                strcpy(data, word);
    
                append(current, new_node((void*)data));
                if (current->next)
                    current = current->next
            }
            fclose(dict_file);
            return head;
        }
        else
        {
            printf("ERROR: File not found");
        }
        return NULL;
    }
    
    int main(int argc, char *argv[])
    {
        NODE *head = create_list();
    
        if (!head)
        {
            printf("ERROR: Either malloc() failed or data not found\n");
        }
        else
        {
            print_list(head, NULL);
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 537k
  • Answers 537k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Someone already has written this down for you - there… May 17, 2026 at 1:49 am
  • Editorial Team
    Editorial Team added an answer Well, the problem is that you can't pickle the function… May 17, 2026 at 1:49 am
  • Editorial Team
    Editorial Team added an answer If you don't have to deal with multiple threads accessing… May 17, 2026 at 1:49 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

heya, i am running linux (ubuntu), I am having some trouble with this. i've
Having some trouble getting this query to work correctly. mysql_query(DELETE FROM `table` WHERE `id`
I've been having some trouble with vs2008 SP1 running in debug mode when I
I've recently started to learn Fluent NH, and I'm having some trouble with this
Guys im having some trouble with some inconsistency of running a piece of code
I’m having some trouble with understanding how IIS is handling static variables on its
I've moved my project over to .NET 4.0 recently, and am having some trouble
I'm trying to deploy my rails application with capistrano, but I'm having some trouble
Having some trouble sending properly formatted HTML e-mail from a PHP script. I am
I'm developing a Flex application and am having some trouble working with asynchronous calls.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.