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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:05:25+00:00 2026-05-28T19:05:25+00:00

I’m trying to implement a linked list abstraction, however I am running into problems.

  • 0

I’m trying to implement a linked list abstraction, however I am running into problems. Once I create the linked list and add elements to it. When I print the list it only prints the first element in it in an infinite loop fashion, meaning that either the first element is linked to itself or the print function is incorrect. However, I can’t find the problem, could someone help?

The following is the list abstraction:

typedef struct _friend {
    char *firstname;
    char *lastname;
    char birthdate[9];
} friend;


typedef struct _node {
    friend *value;
    struct _node *next;
} node;

typedef struct _linkedlist {
    node *head;
} linkedlist;

The program must follow this abstraction, as it is part of something bigger.
The following are the functions that should print the list and add a node to the beginning of the list:

 /* addHead
  *
  * This function takes two parameters - a linked list and a friend.
  * This creates a node for the linked list and connects the friend to the 
  * node.  Then it adds the node to the head of the linked list.
  */

void addHead(linkedlist *llist, friend *f)
{

    // create a node and put the friend in it
    node *n = (node *)malloc(sizeof(node));
    n->value = f;
    n->next = NULL;

    // if the list is empty
    if (llist == NULL)
    {
        // this link is the entire list
        llist->head = n;
        printf("adding friend to null list\n");

    }
    // if the list is not empty
    else
    {
        // make the new link's next pointer point to
        // the first link in the list
        n->next = llist->head;
        printf("adding %s to head\n", n->value->firstname);

        //  make the head pointer point to the new link
        llist->head = n;


}

}

/*
 * printList
 *
 * This steps down through each of the nodes in a linked list and 
 * prints out the information stored in the friend to which the node points.
 * Instead of automatically printing to the screen, it prints to the 
 * file pointer passed in.  If the programmer wants to print to the screen,
 * he/she will pass in stdout.
 */

void printList(linkedlist *llist,FILE *fp)
{

    node *n;
    friend *f;
    // for each node, print out the friend attached to it

    for(n = llist->head; n != NULL ; n = llist->head->next)
    {
        // assign f to the friend of the right node
        f = n->value; 
        // print the friend out
        fprintf(fp,"%s %s: %s\n",
        f->firstname, f->lastname, f->birthdate);
    }

}

Thank You

  • 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-28T19:05:26+00:00Added an answer on May 28, 2026 at 7:05 pm

    The for loop in printList isn’t quite right:

    for(n = llist->head; n != NULL ; n = llist->head->next)
    

    This should read:

    for(n = llist->head; n != NULL ; n = n->next)
    

    Otherwise from the second iteration onwards, n gets set to the same value every single time.

    The following isn’t related to the problem you’re having, but I thought I’d mention it anyway. In the following code:

    if (llist == NULL)
    {
        // this link is the entire list
        llist->head = n;
        printf("adding friend to null list\n");
    
    }
    

    if llist == NULL, the llist->head = n will segfault.

    With the current signature of addHead(), there’s not a lot you can do if llist is NULL (other than printing an error message and bailing out).

    If instead you meant to check whether llist->head is NULL, you don’t need to do that since the else block already handles that correctly.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.