[英]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.
当我在选择1后输入数量时,一切都很好,直到将该节点添加到现有列表中,此后数量变成了一个奇怪的数字串。 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.
当我选择4时会发生这种情况。
EDIT2: The print function is only printing out the first node nothing after that. EDIT2:打印功能仅在此之后不打印第一个节点。 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: 由于您正在执行malloc,因此在添加项目之前,我认为addItem仅需要:
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: 看来您不会打印1个项目列表:
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 . 尝试运行与efence或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.
首先是Scooter指出的问题。 Second is you have a memory leak in the first line of
addItem()
. 其次是在
addItem()
的第一行中有内存泄漏。
Edit To answer your second question, you will need to fix the build error; 编辑要回答第二个问题,您将需要修复构建错误; you reference
reqList
in main()
but never declare it. 您在
main()
引用了reqList
,但从未声明过它。
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*))
只是在这里进行校正
donate *front=(donate*)malloc(sizeof(donate*))
to 至
donate *front=(donate*)malloc(sizeof(donate))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.