The language is C. A copy- paste of the program above is below. Thanks
#include #include #include typedef struct person { char *name; int age; struct person *next; } Person; void print(Person *); // prints the entire list Person *add(Person *, char *, int); // adds a new node to the list int getAge(Person *, char *); // returns the age of the person or -1 if not found int main(void) { char input1[100]; int input2; Person *myList = NULL; printf("Enter a person's name (one word) and age : "); scanf("%s %d", input1, &input2); while (input2 != 0) { myList = add (myList, input1, input2); printf(" The list is now : "); print(myList); printf("Enter a name (one word) and age, enter 'xxx' and 0 to exit : "); scanf("%s %d", input1, &input2); } printf(" The final list is "); print(myList); printf(" Enter the name of a person to look up their age : "); scanf("%s", input1); while ( strcmp(input1, "xxx") != 0 ) { printf("\t%s is %d years old ", input1, getAge(myList, input1) ); printf("Enter a name to look up their age or 'xxx' to exit : "); scanf("%s", input1); } return 0; } void print(Person *ptr) { while (ptr) { printf("[%s-%d] ", ptr->name, ptr->age); ptr = ptr->next; } printf(" "); return; } Person *add(Person *ptr, char *n, int a) { Person *newNode = malloc( sizeof(Person) ); newNode->name = malloc( strlen(n) + 1 ); strcpy(newNode->name, n); newNode->age = a; newNode->next = ptr; return newNode; }
CS 100 Lab 10 - Spring 2018 -A Take-home Lab This hh maintains a linked list ot pople, whereeach node contins a name (one-wor nses only and the age ot . PrULLIpLs Lhe user Lur tuulus aud ans, building Luc lisL wuh calls lo add. priruni lluc lis1 aller cach a id .Promprs the user for a name, and then prinrs the age of tat pesnf they are in the list The code hekw is on larkh. You mmust do the to lowing: Write the finction getage thar rerurns the age of the name passed to ir, or-1/f the person does not exist ite-wrire add so rhar ir?cks in alphaber col order. I)o not add a nene ir ft already eristsin rhelist 'I har s, hefore adding, traverse the st tomake sure the rame is nor alreaty t. rint aneor message if it exr * * wo sample inputs tor the program ? re shown at the bottom right of the code . typodet atruct peroon printiPer returna the age o the rexaon or l it not Cound ahar input1 1100) int inpat2 Peraon yLiat-NULL whil input2- 01 print2(ayn.clbe list io now . "); priat ?xyList); Fred 30 Wilma 29 Barney 26 Betty 25 printi.. 'n?nThe final list ic print layLiath printf[?n?nRnter the lane of per00D to look up their age : "); printtc Enter a nane to look up tbeir age orxto exit : "1 return D void printIPerooo ptr) Fred 30 Fred 29 Fred 29 Wilma 2g Wilma 30 return nentiode-ext- lirst, on ur loca machine, compress your lah0 diectory in a single compressel tile