我试图创建一个链表,该链表将用户name
, age
和ssn
数字作为输入,并以列表格式打印输出。我得到一些错误,所以不能得到[input?].
#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);
}
有....这里有很多错误
第一个跳出来的东西:
struct person *newly;
...
newly[i].name=pname;
newly
是一个person指针。你从来没有分配一个person
,然后尝试访问它,就像它是一个本地结构体(多次)…一个数组?
struct person *newly = malloc(sizeof(struct person));
就是你要找的。然后将其传递给insert
函数:
insert(newly);
new_person
是多余的,不做任何事情。你的node
你也从来没有分配列表本身的头。你的insert
假设有一个头……这里没有。您应该将element
设置为NULL
,并检查,因为如果它是NULL
…这是第一次插入列表。(编辑:嗯,好吧,实际上head
和…再读一遍,我不知道你想用element
)
老实说,我建议你在谷歌上搜索一下,或者一本初学者的C书。我们可以指出代码中的所有问题,但是如果您不了解实际上使用的是什么,那么您将无法从中受益。
EDIT:话虽如此,我想发布一个工作示例是合理的,尽可能多地挽救原始代码。
#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 listn");
}
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溢出输入缓冲区的部分
您的element
节点总是相同的,您永远不会分配一个新的,有效地一次又一次地覆盖相同的事情。
当然这是一个全局变量,所以指针被初始化为NULL,所以它会在第一次写入时崩溃
scanf
接受char指针,而不是char**
:
scanf("%s", pname);
当你写&pname
时,你错误地取了字符指针pname
的地址