链表中的错误

时间:2014-09-11 16:59:14

标签: c linked-list

我有这个编写的测试代码来理解链表。问题是它没有正确遍历显示所有元素。请帮我理解我出错的地方和原因。

#include <stdio.h>
#include <stdlib.h>


//Define structure

struct node {
    char *name;
    int age;
    struct node *pointer;
};

struct node * buildlist()
{
    //initiate the number of structures.
    struct node *dad = NULL;
    struct node *mom = NULL;
    struct node *me = NULL;


    //initate the structures to add data from keyboard

    dad = malloc (sizeof(struct node));
    dad->name = malloc (100*(sizeof(char)));
    printf ("enter the name of the father");
    scanf("%s",dad->name);
    printf ("enter the age of the father");
    scanf("%d",&dad->age);
    dad->pointer = mom;

    mom = malloc (sizeof(struct node));
    mom->name = malloc (100*(sizeof(char)));
    printf ("enter the name of the mother");
    scanf("%s",mom->name);
    printf ("enter the age of the mother");
    scanf("%d",&mom->age);
    mom->pointer = me;

    me = malloc (sizeof(struct node));
    me->name = malloc (100*(sizeof(char)));
    printf ("enter the name of the me");
    scanf("%s",me->name);
    printf ("enter the age of the me");
    scanf("%d",&me->age);
    me->pointer = NULL;

    return dad;

}

int main()
{
    struct node *node1;
    //error i think is in below line

    node1 = buildlist();

    while (node1 != NULL)
        {
            printf("The name is %s and the age is %d\n",node1->name,node1->age);
            node1 = node1->pointer;
        }
}

2 个答案:

答案 0 :(得分:0)

dad->pointer = mom;
mom->pointer = me;

在这两项作业中,momme的值仍为NULL。你还没有初始化它们。尝试:

struct node * buildlist()
{
    //initiate the number of structures.
    struct node *dad = NULL;
    struct node *mom = NULL;
    struct node *me = NULL;


    //initate the structures to add data from keyboard

    dad = malloc (sizeof(struct node));
    dad->name = malloc (100*(sizeof(char)));
    printf ("enter the name of the father");
    scanf("%s",dad->name);
    printf ("enter the age of the father");
    scanf("%d",&dad->age);

    mom = malloc (sizeof(struct node));
    mom->name = malloc (100*(sizeof(char)));
    printf ("enter the name of the mother");
    scanf("%s",mom->name);
    printf ("enter the age of the mother");
    scanf("%d",&mom->age);

    me = malloc (sizeof(struct node));
    me->name = malloc (100*(sizeof(char)));
    printf ("enter the name of the me");
    scanf("%s",me->name);
    printf ("enter the age of the me");
    scanf("%d",&me->age);

    dad->pointer = mom;
    mom->pointer = me;
    me->pointer = NULL;

    return dad;

}

答案 1 :(得分:0)

按以下方式更改功能buildlist

//...

dad->pointer = NULL;

mom = malloc (sizeof(struct node));
mom->name = malloc (100*(sizeof(char)));
printf ("enter the name of the mother");
scanf("%s",mom->name);
printf ("enter the age of the mother");
scanf("%d",&mom->age);
mom->pointer = NULL;

dad->pointer = mom;


me = malloc (sizeof(struct node));
me->name = malloc (100*(sizeof(char)));
printf ("enter the name of the me");
scanf("%s",me->name);
printf ("enter the age of the me");
scanf("%d",&me->age);
me->pointer = NULL;

mom->pointer = me;

//...