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; 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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
