链表中的字符

时间:2011-09-11 21:03:19

标签: c linked-list

我正在尝试创建一个链接列表,该列表将用户nameagessn作为输入,并以列表格式打印输出。我收到了一些错误,所以无法获得[输入?]。

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

struct person
{
    char *name;
    int age;
    char *ssn;
};

struct node
{
    struct person * person;
    struct node * next;
} *head, *element;

void insert (struct person *new_person)
{
    element->person = new_person;
    element->next = head;
    head = element;
}

void display (struct node *ll)
{
    if(ll == NULL)
        printf("empty list");

    while(ll != NULL)
    {
        printf("%s %d  %s ", ll->person->name, ll->person->age, ll->person->ssn);
        ll = ll->next;

        if(ll != NULL)
            printf("->");
    }
}

main()
{
    int total_no_person, i, page;

    printf("enter the total number of person \t");
    scanf("%d", &total_no_person);

    struct node * temp = (struct node *) malloc(sizeof(struct node));
    struct person * new_person;

    char *pname = NULL;
    char *pssn = NULL;

    head = NULL;

    for(i = 0; i < total_no_person; i++)
    {
        pname = (char *) malloc(100);
        pssn = (char *) malloc(100);

        struct person * newly;

        printf("enter the %dth person's name \t", i + 1);
        scanf("%s", &pname);
        newly[i].name = pname;

        printf("enter %dth person's age \t", i + 1);
        scanf("%d", &page);
        newly[i].age = page;

        printf("enter %dth person's ssn \t", i + 1);
        scanf("%s", &pssn);
        newly[i].ssn = pssn;

        new_person = newly;
        insert(new_person);
    }
    temp = head;
    display(temp);

}

3 个答案:

答案 0 :(得分:3)

这里有很多错误。

跳出来的第一件事:

struct person *newly;
...
newly[i].name=pname;

newly是一个人指针。你永远不会分配一个person,然后尝试访问它,就像它是一个本地结构(多次)作为一个数组?

struct person *newly = malloc(sizeof(struct person));

是您正在寻找的。然后,您将其传递给insert函数:

insert(newly);

new_person是多余的,不会做任何事情。与您的node

相同

你也从未分配过列表的头部。你的insert假设有一个头......那不存在。您应该将element设置为NULL并检查,因为如果它是NULL ...这是您第一次插入列表。 (编辑:嗯,嗯,实际上是head并且......再次阅读我不确定你要用element做什么

老实说 - 我会建议谷歌搜索,或初学者的C书。我们可以指出代码中的所有问题,但如果您不了解实际使用的内容,那么您将不会受益。

编辑:话虽如此,我想发布一个有效的示例,尽可能多地挽救原始代码是合理的。

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

struct person
{
    char *name;
    int age;
    char *ssn;
};

/* Note: because head and tail are global they
   are initialized to NULL automatically */
struct node
{
    struct person *person;
    struct node *next;
} *head, *tail;


void insert(struct person *new_person)
{
    /* allocate a new node */
    struct node *node = malloc(sizeof(struct node));

    /* assign the person to the node */
    node->person = new_person;
    node->next = NULL;

    if (head == NULL)
    {
         /* Since head is NULL, we are inserting for the first time.
            Set the head and tail to point at our new node */

        head = node;
        tail = node;
    }
    else
    {
        /* the tail is the last node in our list. We attach the new
           node to its next, then repoint the tail to our new node */
        tail->next = node;
        tail = node;
    }
}

void display()
{
    if(head == NULL)
    {
        printf("empty list\n");
    }
    else
    {
        struct node *current = head;
        while(current != NULL)
        {
            printf("%s %d  %s ", current->person->name, 
                                 current->person->age, 
                                 current->person->ssn);
            current = current->next;
            if(current != NULL)
                printf("->");
        }
        printf("\n");
    }
}


main()
{

    int total_no_person,i;

    printf("enter the total number of person \t");
    scanf("%d",&total_no_person);

    for(i=0;i<total_no_person;i++)
    {
        /* allocate a new person, then allocate its members */
        struct person *newly = malloc(sizeof(struct person));
        newly->name = malloc(100);
        newly->ssn = malloc(100);

        printf("enter the %dth person's name \t",i+1);
        scanf("%s", newly->name);

        printf("enter %dth person's age \t",i+1);
        scanf("%d", &newly->age);

        printf("enter %dth person's ssn \t",i+1);
        scanf("%s", newly->ssn);

        insert(newly);
    }

    display();
}

我遗漏的一个额外位是你可以使用scanf溢出输入缓冲区的部分 - http://www.crasseux.com/books/ctutorial/String-overflows-with-scanf.html

答案 1 :(得分:2)

您的element节点总是相同的,您永远不会分配新节点,有效地一次又一次地覆盖同一个节点。

当然它是一个全局变量,因此指针初始化为NULL,因此它会在第一次写入时崩溃。

答案 2 :(得分:2)

scanf采用char指针,而不是char**

scanf("%s", pname);

当你写pname时,你错误地将地址指向了&pname