c中的链表问题

c中的链表问题,c,linked-list,C,Linked List,我在尝试链接列表,但出于某种原因,它没有做它应该做的事情。当我在选择1之后输入数量时,在将节点添加到现有列表之前,这一切都是好的,之后数量变成一个奇怪的数字字符串。而且,每当我尝试向捐赠列表添加多个节点时,程序就会崩溃 编辑:上面的问题已经解决了,但还有一个问题我忘了提 当我试图打印列表时,什么都不会打印出来。当我选择4时就会发生这种情况 EDIT2:print函数只打印第一个节点,之后什么也不打印。 请帮忙 这是密码 #include<stdio.h> #include<st

我在尝试链接列表,但出于某种原因,它没有做它应该做的事情。当我在选择1之后输入数量时,在将节点添加到现有列表之前,这一切都是好的,之后数量变成一个奇怪的数字字符串。而且,每当我尝试向捐赠列表添加多个节点时,程序就会崩溃

编辑:上面的问题已经解决了,但还有一个问题我忘了提 当我试图打印列表时,什么都不会打印出来。当我选择4时就会发生这种情况

EDIT2:print函数只打印第一个节点,之后什么也不打印。 请帮忙

这是密码

#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;
}
#包括
#包括
#包括
typedef结构捐赠{
字符名[50];
整数;
结构捐赠*下一步;
}捐赠;
捐赠*附加项(捐赠*我的列表,捐赠*临时){
捐赠额=(捐赠额*)malloc(捐赠额*);
if(mylist==NULL)
返回温度;
前=mylist;
while(mylist->next!=NULL)
mylist=mylist->next;
mylist->next=temp;
返回前线;
}    
作废打印(捐赠*捐赠名单){
printf(“打印捐款表\n\n”);
if(donList!=NULL){
while(donList->next!=NULL){
printf(“%s%d\n”,donList->name,donList->quant);
donList=donList->next;
}
}
}
main(){
捐赠*list=NULL;
而(1){
智力选择;
printf(“1.添加捐赠\n);
printf(“输入您的选择:”);
scanf(“%d”,选择(&C);
如果(选项==1){
捐赠*临时=(捐赠*)malloc(捐赠*);
printf(“\n输入存货类型:”);
扫描(“%s”,临时->名称);
printf(“输入金额:”);
scanf(“%d”,&temp->quant);
temp->next=NULL;
列表=附加项(列表,临时);
printf(“\n添加国家!\n”);
printf(“%s%d\n”,列表->名称,列表->数量);
}
else if(选项==4){
打印(列表);
}
}
系统(“暂停”);
返回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);
}

一个问题是,您正在为指针分配空间。您需要为结构本身分配空间

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

我看到两个问题。第一个是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;
}
#包括
#包括
#包括
typedef结构捐赠{
字符名[50];
整数;
结构捐赠*下一步;
}捐赠;
捐赠*附加项(捐赠*我的列表,捐赠*临时){
if(mylist==NULL)
返回温度;
捐赠*front=mylist;
while(mylist->next!=NULL)
mylist=mylist->next;
mylist->next=temp;
返回前线;
}    
main(){
捐赠*list=NULL;
而(1){
智力选择;
printf(“1.添加捐赠\n);
printf(“输入您的选择:”);
scanf(“%d”,选择(&C);
如果(选项==1){
捐赠*临时=(捐赠*)malloc(sizeof(捐赠));
printf(“\n输入存货类型:”);
扫描(“%s”,临时->名称);
printf(“输入金额:”);
scanf(“%d”,&temp->quant);
temp->next=NULL;
列表=附加项(列表,临时);
printf(“\n添加国家!\n”);
printf(“%s%d\n”,列表->名称,列表->数量);
}
}
系统(“暂停”);
返回0;
}

我看到两个问题。首先是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;
}
#包括
#包括
#包括
typedef结构捐赠{
字符名[50];
整数;
结构捐赠*下一步;
}捐赠;
捐赠*附加项(捐赠*我的列表,捐赠*临时){
if(mylist==NULL)
返回温度;
捐赠*front=mylist;
while(mylist->next!=NULL)
mylist=mylist->next;
mylist->next=temp;
返回前线;
}    
main(){
捐赠*list=NULL;
而(1){
智力选择;
printf(“1.添加捐赠\n);
printf(“输入您的选择:”);
scanf(“%d”,选择(&C);
如果(选项==1){
捐赠*临时=(捐赠*)malloc(sizeof(捐赠));
printf(“\n输入存货类型:”);
扫描(“%s”,临时->名称);
printf(“输入金额:”);
scanf(“%d”,&temp->quant);
temp->next=NULL;
列表=附加项(列表,临时);
printf(“\n添加国家!\n”);
printf(“%s%d\n”,列表->名称,列表->数量);
}
}
系统(“暂停”);
返回0;
}
请在此处更正
捐赠*front=(捐赠*)malloc(sizeof(捐赠*)


在这里更正一下
捐赠*front=(捐赠*)malloc(sizeof(捐赠*)



您应该考虑将问题中的代码减少到演示问题所需的最小值。例如,您可能会丢失打印和命令行UI。