gpt4 book ai didi

c - 学习链表

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:22 24 4
gpt4 key购买 nike

对于我正在进行的这个实验,当我打印列表时,顺序和年龄是正确的,但每个元素的名称都是“quit”。知道如何解决这个问题吗?我知道它很长,但其中一些可能对我的问题至关重要(不确定是哪个)。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct person {
char *name;
int age;
struct person *next;
} Person;

Person *addFront(Person *List, char *Name, int Age) {// add at front, for
people under 21

Person *ptrNew=malloc(sizeof(Person));
ptrNew->name=Name;
ptrNew->age=Age;
ptrNew->next=List;
return ptrNew;
}
Person *addRear(Person *List, char *Name, int Age) { // add at rear, for
people 21 or older

Person *ptrNew=malloc(sizeof(Person));
ptrNew->name=Name;
ptrNew->age=Age;
ptrNew->next=List;
if (List==NULL) return ptrNew;
Person *ptr;
for (ptr=List; ptr != NULL; ptr = ptr->next) {
if (ptr->next==NULL) break;
}
ptr->next=ptrNew;
return List;
}
void print(Person *List) { // print the list (name and age for
each item)
Person *ptr;
for (ptr=List; ptr != NULL; ptr = ptr->next) {
printf("Name:%s Age:%d\n", ptr->name, ptr->age);
}
}
void printLast(Person *List) { // print the last person (and
age) in the list
Person *ptr;
for (ptr=List; ptr != NULL; ptr = ptr->next)
if (ptr->next==NULL) printf("Name:%s Age:%d\n", ptr->name, ptr-
>age);
}
void printFirst(Person *List) { // print the first person (and
age) in the list
Person *ptr=List;
printf("Name:%s Age:%d\n", ptr->name, ptr->age);
}
int size(Person *List) { // return the length of the
list
Person *ptr;
int count=0;
for (ptr=List; ptr != NULL; ptr = ptr->next) count++;
return count;
}
int inList(Person *List, char *Name) { // returns 1 if the name is
in the list, else 0
Person *ptr;
for (ptr=List; ptr != NULL; ptr = ptr->next) {
if (strcmp(Name, ptr->name)==0) return 1;
else return 0;
}

}
int getAge(Person *List, char *Name) { // returns the age of the
person specified
Person *ptr;
for (ptr=List; ptr != NULL; ptr = ptr->next) {
if (strcmp(Name, ptr->name)==0) printf("%d", ptr->age);
else return -1;
}

}
// return -1 if the person is not in the list
int main(void) {
Person *myList = NULL;
int theAge;
char theName[128];
printf("Enter the name of a person and an age (an integer) : ");
scanf("%s %d", theName, &theAge);
while ( strcmp(theName, "quit") != 0) {
if (theAge < 21)
myList = addFront(myList, theName, theAge);
else
myList = addRear(myList, theName, theAge);
printf("Enter another name and age (or \"quit\" and any integer when done) : ");
scanf("%s %d", theName, &theAge);
}
printf("\n\n\nThe list is " ); print(myList);
printf("\n\nThe list has %d elements\n\n", size(myList) );
printf("\nThe first person in the list (and their age) is : "); printFirst(myList);
printf("\nThe last person in the list (and their age) is : "); printLast(myList);
printf("\n\n");
printf("Enter the name of a person (or \"exit\" to exit) : ");
scanf("%s", theName);
while ( strcmp(theName, "exit") != 0) {
if ( inList(myList, theName) )
printf("\tFound %s (age is %d)\n", theName, getAge(myList, theName) );
else
printf("\t%s was not found in the list\n", theName);
printf("\nEnter the name of a person (or \"exit\" to exit) : ");
scanf("%s", theName);
}
return 0;

最佳答案

addFrontaddRear 你正在使用

ptrNew->name=Name;

您不能只将列表中 name 的指针分配给您从 scanf 读取的指针。在这种情况下发生的情况是每个 List->name 都将指向相同的位置,并且如果 Name (来自 scanf)发生变化(在您的情况下为“退出”最后一个元素。每个 List->name 都会给出“quit”

您需要做的是为每个名称分别分配内存。

ptrNew->name=malloc(strlen(Name)+1);
strcpy(ptrNew->name, Name);

或者,您可以更改结构定义,以足够长度的名称存储数组

typedef struct person {
char name[20];
int age;
struct person *next;
} Person;

在这种情况下,您可以跳过上面的 malloc 步骤,但仍然需要 strcpy

关于c - 学习链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43197980/

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