Reputation: 45
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!
Upvotes: 0
Views: 142
Reputation: 17655
just make correction here
donate *front=(donate*)malloc(sizeof(donate*))
to
donate *front=(donate*)malloc(sizeof(donate))
Upvotes: 0
Reputation: 7061
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);
}
Upvotes: 2
Reputation: 192
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;
}
Upvotes: 1