对于我正在进行的实验,当我打印列表时,顺序和年龄是正确的,但每个元素的名称都是“退出”。知道如何解决这个问题吗?我知道它很长,但有些可能对我的问题很重要(不确定是哪个)。
#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;
}
答案 0 :(得分:1)
在addFront
和addRear
您正在使用
ptrNew->name=Name;
您不能只将列表中name
的指针指定给您从scanf读取的指针。在这种情况下会发生的是每List->name
个指向相同的位置,如果Name
(来自scanf)发生变化(在您的情况下为#34;退出&#34;作为最后一个元素。 List->name
会退出&#34;退出&#34;
您需要做的是分别为每个名称分配内存。
ptrNew->name=malloc(strlen(Name)+1);
strcpy(ptrNew->name, Name);
或者,您可以更改结构定义以存储名称足够长的数组
typedef struct person {
char name[20];
int age;
struct person *next;
} Person;
在这种情况下,您可以跳过上面的malloc步骤,但仍然需要strcpy
。
答案 1 :(得分:1)
您使用ptrNew->name
作为字符指针,它始终指向theName
的{{1}}变量。因此链表中的所有节点都指向同一个名称。
相反,您应该将main()
和addFront()
中的代码更改为
addRear()
而不是
ptrNew->name= strdup(Name);
完成节点后,请确保ptrNew->name=Name;
内存正确。