0

我正在尝试链接列表,但由于某种原因它没有做它应该做的事情。当我在选择 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;
}

谢谢!

4

4 回答 4

2

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

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);
}
于 2012-09-24T01:27:19.757 回答
1

尝试运行链接到efence或使用valgrind的程序。两者都会告诉你事情开始变坏的时间和地点。

于 2012-09-24T01:22:42.870 回答
1

我看到了两个问题。首先是Scooter指出的问题。其次是你的第一行有内存泄漏addItem()

编辑要回答您的第二个问题,您需要修复构建错误;您引用reqList但从main()不声明它。

这是代码的更正版本:

#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;
}
于 2012-09-24T01:36:45.597 回答
0

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

donate *front=(donate*)malloc(sizeof(donate))
于 2012-09-24T02:40:25.610 回答