[英]Issue with linked list in c
我正在尝试链接列表,由于某种原因,它没有执行应做的事情。 当我在选择1后输入数量时,一切都很好,直到将该节点添加到现有列表中,此后数量变成了一个奇怪的数字串。 而且,当我尝试将多个节点添加到捐赠列表中时,程序也会崩溃。
编辑:上面的问题已解决,但还有一个我忘记提及的问题。这是当我尝试将列表打印出来时,什么也没打印出来。 当我选择4时会发生这种情况。
EDIT2:打印功能仅在此之后不打印第一个节点。 请帮忙。
这是代码。
#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;
}
谢谢!
一个问题是您正在为捐赠指针分配空间。 您需要为结构本身分配空间。
donate* temp=(donate*)malloc(sizeof(donate*));
应该
donate* temp= malloc(sizeof(donate));
由于您正在执行malloc,因此在添加项目之前,我认为addItem仅需要:
donate* addItem(donate *mylist,donate *temp)
{
if (mylist != NULL)
temp->next = mylist;
return temp;
}
看来您不会打印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;
}
}
我认为应该是:
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);
}
尝试运行与efence或valgrind链接的程序 。 两者都会告诉您何时何地开始变坏。
我看到两个问题。 首先是Scooter指出的问题。 其次是在addItem()
的第一行中有内存泄漏。
编辑要回答第二个问题,您将需要修复构建错误; 您在main()
引用了reqList
,但从未声明过它。
这是代码的更正版本:
#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;
}
只是在这里进行校正donate *front=(donate*)malloc(sizeof(donate*))
至
donate *front=(donate*)malloc(sizeof(donate))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.