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

  • Home
  • SEARCH
  • 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 8586599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:20:51+00:00 2026-06-11T22:20:51+00:00

I was trying out linked lists and for some reason it isnt doing what

  • 0

I was trying out linked lists and for some reason it isnt doing what it is supposed to do. When I enter the quantity after choosing 1 it is all good until the node is add to the existing list, after which the quantity becomes a weird string of numbers. And also when ever i try adding more than one node to the donate list the program crashes.

EDIT: The above problem is solved but there is another problem which I forgot to mention
It is when I am trying to print the list out, nothing gets printed. This happens when I choose 4.

EDIT2: The print function is only printing out the first node nothing after that.
Please help.

Here’s the code.

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

typedef struct donation{
    char name[50];
    int quant;
    struct donation* next;
}donate;


donate* addItem(donate *mylist,donate *temp){
    donate *front=(donate*)malloc(sizeof(donate*));

    if(mylist==NULL)
    return temp;

    front=mylist;
    while(mylist->next!=NULL)
        mylist=mylist->next;
    mylist->next=temp;

    return front;
}    
void print(donate* donList){

    printf("Printing the Donations Table\n\n");
    if(donList!=NULL){
        while(donList->next!=NULL){
            printf("%s %d\n",donList->name,donList->quant);
            donList=donList->next;
        }
    }
}

main(){

    donate *list=NULL;

    while(1){
        int choice;
        printf("1. Add a donation\n);
        printf("Enter your choice: ");
        scanf("%d",&choice);

        if(choice==1){
            donate* temp=(donate*)malloc(sizeof(donate*));
            printf("\nEnter inventory type: ");
            scanf("%s",temp->name);
            printf("Enter the amount: ");
            scanf("%d",&temp->quant);
            temp->next=NULL;
            list=addItem(list,temp);
            printf("\nDonation Added!\n");
            printf("%s %d\n",list->name,list->quant);
        }
    else if(choice==4){
        print(list);
    }
}

    system("pause");
    return 0;
}

Thanks!

  • 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-11T22:20:52+00:00Added an answer on June 11, 2026 at 10:20 pm

    One problem is that you are mallocing space for a donate pointer. You need to allocate space for the struct itself.

    donate* temp=(donate*)malloc(sizeof(donate*));
    

    should be

    donate* temp= malloc(sizeof(donate));
    

    Since you are doing a malloc, prior to adding an item, I think addItem just needs to be:

    donate* addItem(donate *mylist,donate *temp)
    {
        if (mylist != NULL)
           temp->next = mylist;
    
        return temp;
    }
    

    It looks like you would not print a 1 item list:

       printf("Printing the Donations Table\n\n");
        if(donList!=NULL){
            printf("Not NULL!!!!\n");
            while(donList->next!=NULL){
                printf("%s %d\n",donList->name,donList->quant);
                donList=donList->next;
            }
        }
    

    I think it should be:

    printf("Printing the Donations Table\n\n");
    if (donList!=NULL)
    {
        printf("Not NULL!!!!\n");
        do 
        {
            printf("%s %d\n",donList->name,donList->quant)
            donList=donList->next;
        }
        while(donList != NULL);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find out my problem. For some reason when I try
After trying out VS2010b2 also my VS2008 installation changes the versions of solution and
I've been trying to figure out how to reverse the order of a doubly-linked
I am doing some examples out of an older C book [A First Book
I am relatively new to Java and while trying out some code came across
Trying to figure out how to sort my doubly linked list. I get a
I am trying to use a Generic Linked List to hold some WorkFlow steps
I have several tables that I am trying to get some data out of,
I'm trying to find out if friends of a user have liked a page
Trying out the example usage of codebrew rails-backbone , everything works fine but the

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.