2017-04-04 85 views
0

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

#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; 

}

回答

1

addFrontaddRear您使用

ptrNew->name=Name; 

你不能只分配了name指針在列表中,你是從scanf函數讀指針。會發生什麼事在這種情況下是每List->name將指向相同的位置,如果Name(從scanf函數)的變化(在你的情況下,以「跳槽」作爲最後一個元素。每List->name會給「跳槽」

你需要什麼做的,是提供一種用於分開每個名稱分配內存。

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

您正在使用ptrNew->name作爲字符指針,並始終指向theName變量從main()變量。因此鏈表中的所有節點都指向相同的名稱。

相反,你應該在addFront()addRear()更改代碼

ptrNew->name= strdup(Name); 

而不是

ptrNew->name=Name; 

確保您free內存適當的時候,你與節點完成。