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 625815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:21:03+00:00 2026-05-13T19:21:03+00:00

I’m basically trying to create a linked list from a text file and add

  • 0

I’m basically trying to create a linked list from a text file and add a new member every time the words are different, and increment the count if the words are the same (hw assignment). I thought I did it correctly, but it seems to add a member no matter what. I’m wondering if I’m traversing the list incorrectly while I search? Here is my code. Any thoughts? Thanks!

LIST *CreateList(FILE *fp) 
{
    char input[LINE_LEN];
    LIST *root= NULL;             /* contains root of list             */
    size_t strSize;    
    LIST *newList;          /* used to allocate new list members */
    int same;             /* if string is same */

    while (fscanf(fp, BUFFMT"s", input) != EOF) {

        strSize = strlen(input) + 1;

            if (root == NULL) {
                if ((newList = (LIST *)malloc(sizeof(LIST))) == NULL) {
                    printf("Out of memory...");
                    exit(EXIT_FAILURE);
                } 
                if ((newList->str = (char *)malloc(sizeof(strSize))) == NULL) {
                    printf("Not enough memory for %s", input);
                    exit(EXIT_FAILURE);
                }
                memcpy(newList->str, input, strSize);   /*copy string    */
                newList->count = START_COUNT;
                newList->next = NULL;
                root = newList;
            }
            /* if not root node, add node, or increment count */
            else {
                same = ListSame(newList, input);
                if (same == 1) {
                    root->count++;
                }
                else {
                    if ((newList = (LIST *)malloc(sizeof(LIST))) == NULL) {
                        printf("Out of memory...");
                        exit(EXIT_FAILURE);
                    } 
                    if ((newList->str = (char *)malloc(sizeof(strSize))) == NULL) {
                        printf("Not enough memory for %s", input);
                        exit(EXIT_FAILURE);
                    }
                    memcpy(newList->str, input, strSize);   /*copy string    */
                    newList->count = START_COUNT;
                    newList->next = root->next;
                    root->next = newList;
                }
            }
    }
        return root;
}

int ListSame(LIST *head, char *input) 
{
    LIST *start = head;
    for (; start != NULL; start = start->next) {
        if (strcmp(head->str, input) == 0) {
            return 1;   
        }
    }
    return 0;
}
  • 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-13T19:21:03+00:00Added an answer on May 13, 2026 at 7:21 pm

    You’re calling ListSame with newList, which will always only have the last created node. It looks like you want to pass root to ListSame.

    Also, in ListSame, it’s always checking head->str, not start->str, which is your looping variable.

    Also, if ListSame returns true (1), you increment root->count; I’m not sure if you want to increment the root node’s count (count how many duplicates total) or the count for the node that has the duplicate (count how many times each word shows up). If it’s the latter, either ListSame is going to need to return which node is the duplicate, or it’s going to need to increment the count itself.

    Also, the way you’re building the list is a little confusing. When you say:

    newList->next = root->next;
    root->next = newList;

    root->next before this will either be NULL, or the last created node. So when you do the insert here, you’re always inserting it as the second item in the list. That’s probably not what you want. If you want to append, you should keep track of the tail as well as the head; if you want to prepend, just set newList->next = root; root = newList;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think you will best solve this problem by calling… May 15, 2026 at 2:06 am
  • Editorial Team
    Editorial Team added an answer You can view a complete list of events in the… May 15, 2026 at 2:06 am
  • Editorial Team
    Editorial Team added an answer It seems to only work with the http[s] protocols (likely… May 15, 2026 at 2:06 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

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.