Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This lab maintains a linked list of people, where each node contains a name (one-word names only) and the age of that person. For example,

This lab maintains a linked list of people, where each node contains a name (one-word names only) and the age of that person. For example, a node might contain BigAl and 21. The program operates as follows

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 (if they are in the list) The code below is on Blackboard. 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 it adds in alphabetical order. Do not add a name if it already exists in the list. That is, before adding, traverse the list to make sure the name is not already there. Print an error message if it exists.

Two sample inputs for the program are shown at the bottom right of the code

image text in transcribed

#include #include #include typedef struct person ( char name; int age struct person "next Personi void print (Person Person add (Person*,char int) int getAge (Person, char ) /I prints the entire 1ist /I adds a new node to the list // returns the age of the person or -1 if not found int main (void) ( char inputl [100] int input2; Person *myList = NULL ; printf("Enter a person's name (one word) and age scanf("%s %d", input!, &input2); while (input2!- 0) myList -add (myList, inputi, input2) printf ("nnThe 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(nnThe final list is Print (myList) printf("In nEnter the name of a person to look up their age scanf ("%s", input1); while strcmp (inputl, "xxx)0) Fred 30 Wilma 29 Barney 26 Betty 25 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(n" return; { printf ("[is-ld] ", ptr->Mane, ptr->age); ptr } = ptr->next ; Fred 30 Fred 29 Fred 28 Wilma 29 Wilma 30 Person add (Person ptr, char *n, int a) Person newNodemalloc( sizeof (Person) newNode->namemalloc strlen (n) +1 strcpy (newNode->name, n) newNode->agea; newNode->next = ptr ; return newNode

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

1. Explain why strategic planning is important to all managers.

Answered: 1 week ago