[英]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.