简体   繁体   中英

Issue with linked list in c

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!

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);
}

Try running your program linked to efence or with valgrind . Both will tell you when and where things start to go bad.

There are two issue that I see. First is the issue pointed out by Scooter. Second is you have a memory leak in the first line of addItem() .

Edit To answer your second question, you will need to fix the build error; you reference reqList in main() but never declare it.

Here is a corrected version of 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){
    if(mylist==NULL)
    return temp;

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

    return front;
}    

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);
        }
    }
    system("pause");
    return 0;
}

just make correction here donate *front=(donate*)malloc(sizeof(donate*))
to

donate *front=(donate*)malloc(sizeof(donate))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM