對於本實驗,我正在研究打印列表時,訂單和年齡是正確的,但每個元素的名稱均爲「退出」。任何想法如何解決這個問題?我知道這是相當長的,但其中一些可能對我的問題至關重要(不知道是哪一個)。學習鏈接列表
#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;
}