Question: starting code #include #include #include typedef struct person { char *name; int age; struct person *next; } Person; Person *createPerson(char name[], int age, Person *next)

 starting code #include #include #include typedef struct person { char *name;

starting code #include  #include  #include  typedef struct person { char *name; int age; struct person *next; } Person; Person *createPerson(char name[], int age, Person *next) { Person *pNew = malloc( sizeof(Person) ); pNew->name = malloc( strlen(name) + 1 ); strcpy(pNew->name, name); pNew->age = age; pNew->next = next; return pNew; } int comparePerson(Person *ptr1, Person *ptr2) { return -1; } Person *add(Person *head, char name[], int age) { return createPerson(name, age, head); } void print(char prefix[], Person *ptr) { printf("%s", prefix); while (ptr) { printf("[%s-%d] ", ptr->name, ptr->age); ptr = ptr->next; } printf(" "); } int getAge(Person *head, char name[]) { return -1; } int main(void) { Person *head = NULL; char name[100]; int age; while (1) { // build the list printf(" Enter a name (one word) and age, or xxx and 0 to exit: "); scanf("%s%d", name, &age); if (strcmp(name, "xxx")==0) break; head = add(head, name, age); print(" \tNow the list is: ", head); } while (1) { // perform search printf(" Enter a name to look up their age or xxx to exit : "); scanf("%s", name); if (strcmp(name, "xxx")==0) break; printf(" \t%s is %d years old ", name, getAge(head, name) ); } return 0; }

Create a directory named lab9 and download lab9.c as shown below from Blackboard to that directory. This lab maintains a linked list of persons, where each person contains a nam operates as follows. e (one-word only) and an age. The program Prompts the user for names and ages, building the list with calls to add, printing the list after each add Prompts the user for a name, and then prints the age of that person (-1 if the name is not in the list) . You must do the following Write the function getage that returns the age of the name passed to it, or -1 if the person does not exist. . Re-write add so that all the persons will be ordered by ascending age. For those with the same age, the will be ordered alphabetically. Do not add a name if it already exists in the list and print an error message. It is recommended to write a helper function comparePerson that compares two persons and returns -1, 1 or 0, depending on the positions of the two persons in the linked list. Two sample inputs for the program are shown at the right of the code include #include include Fred 20 Barney 19 Betty 25 Wilma 20 typadet struct person t char *name; int age : struct Pn Person next; t Person Person *createPerson (char name [1, int age, Person "next) Person pNew-malloc sizeof (Person) PNew->name malloc strlen (name) 1 stropy (pNew->nane, name): pNew-age- age pNew-nextnext return PNew: Fred Betty int comparePerson (Person 'ptri, Person ptr2) return -1: Person *add (Person "head, char name[], int age) return createPerson (name, age, head) Betty 28 Fred 30 Fred 29 Wilma 29 Wilma 30 void print (char prefixi], Person ptr) printf("%s", prefix) ; while (ptr) i printf ("[ts-td] ", ptr->name, ptr->age) ptr - ptr->next; printf(n"): int getAge (Person *head, char name[]) t return -1 int main (void) Pebbles Person *head = NULL; char name [1001: int while (1) // build the list ago nnter a nane (one word) and age, or xxx and o to exit: ) scanf("%s%d", name, &age); if (strcmp (name , "xxx")=-o) break ; head add (head, name, age): print("n\tNow the list is: ",head); while (1)// perform search printf ("nenter a name to look up their age or xxx to exit:"): scanf("%s", name); if (stramp (name , "xxx ")=0) break printf (" \ta is %d years olan", name , getage (head, name) ); return o; Submit your lab First, on your machine, compress your lab9 directory into a single (compressed) file, i.e. lab9.zip. Second, once you have a compressed file named lab9.zip, submit that file to Blackboard

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!