gpt4 book ai didi

链表中的字符

转载 作者:太空宇宙 更新时间:2023-11-04 00:41:13 25 4
gpt4 key购买 nike

我正在尝试创建一个链表,它将用户 nameagessn 号码作为输入并打印输出列表格式。我收到一些错误,因此无法获得 [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,然后尝试访问它,就像它是一个本地结构(多次)一样...一个数组?

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

是你要找的。然后,您将把它传递给您的 insert 函数:

insert(newly);

new_person 是多余的,什么都不做。与您的节点

相同

您也从未分配过列表本身的头部。您的 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

关于链表中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7381235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com