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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:56:41+00:00 2026-06-01T01:56:41+00:00

I am attempting to add a node to the end of a linked list.

  • 0

I am attempting to add a node to the end of a linked list. I use a void function and pass my struct into it, but once it has ran through the add function, my struct is still empty. here is the code.

struct part {
    char* name;
    float price;
    int quantity;
    struct part *next;
};

typedef struct part partType;

void addEnd(partType *item) {  
    partType *temp1=NULL, *temp2=NULL;
    char temp[100];  

    temp1 = (struct part *)malloc(sizeof(partType));  

    if (!temp1)
        printf("malloc failed\n");

    temp1->name = malloc(sizeof(char)*100);

    printf("Please enter item name: \n");
    fgets(temp, 100, stdin);     
    strcpy(temp1->name, temp);      

    printf("Please enter item price: \n");
    fgets(temp, 100, stdin);
    sscanf(temp, "%f", &temp1->price);      

    printf("Please enter item quantity: \n");
    fgets(temp, 100, stdin);
    sscanf(temp, "%d", &temp1->quantity);  

    // Copying the Head location into another node.  
    temp2 = item;  

    if (item == NULL) {  
        // If List is empty we create First Node.  
        item = temp1;  
        item->next = NULL;
        printf("%s%.2f\n%d\n", item->name, item->price, item->quantity);  
    } else {  
       // Traverse down to end of the list.  
       while (temp2->next != NULL)  
           temp2 = temp2->next;  

       // Append at the end of the list.  
       temp1->next = NULL;  
       temp2->next = temp1;
       printf("%s%.2f\n%d\n", item->name, item->price, item->quantity);
    }
} 

item is null when it is initially passed into the function, but for some reason comes out null even though i have the if statement that sets item equal to temp1.

  • 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-06-01T01:56:42+00:00Added an answer on June 1, 2026 at 1:56 am

    You need to modify the value of the pointer, so you need an extra level of indirection:

    void addEnd(partType **item)
    {
       ...
       temp2 = *item;
       ...  
       if (*item == NULL)
       {
         *item = temp1;
         (*item)->next = NULL;
         printf("%s%.2f\n%d\n", (*item)->name, (*item)->price, (*item)->quantity);
         ...
    }
    

    and you would call this as

    partType *newItem;
    ...
    addEnd(&newItem);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to insert a new node into a sorted linked list of
I am attempting to read a text file into a linear linked list of
I am attempting to add the jQuery Print Preview Plugin into my website but
I am attempting to add a function that will create a csv for download
I am attempting to add a List component to a view in Sencha Touch.
I am attempting to add a No Preference choice to my check box list
Attempting to add parameters to an xsl template, for use in a navigation menu.
I'm attempting to craft my own basic singly linked list in C++ as a
I'm attempting to add a UIImageView to the subview of a UILabel, but for
I'm attempting to add a drop down to a page that already has a

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.