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 addFront and addRear

- prints entire list, and then prints only the people in the list who are 21 or older

- prompts the user for a name and then prints the age of that person (if they are in the lsit)

Write the three functions addRear, printLegal, and getAge

image text in transcribed

#include #include #include typedef struct person { char *name; int age; struct person *next; } Person; void print (Person *): Person *addFront (Person *, char *, int) : // prints the entire list Person *addRear (Person *, char *, int) : 77 adds a new node to the front of the list void printLegal (Person *); // adds a new node to the end of the list 77 prints the names who are 21 or older int getAge (Person *, char *); 77 returns age of the name specified (or -1) int main(void) { char input1 [100]; int input2; Person *myList = NULL; printf("Enter a person's name (one word) and age : "); scanf("%s %d", inputi, &input2); while (input2 != 0) { if (input2 > 20) myList = addRear (myList, input1, input2); else myList = addFront (myList, inputi, input2); printf ("Enter a name (one word) and age, enter 'xxx' and o to exit : "); scanf("%s %d", inputi, &input2); printf(" "); print (myList); printf(" "); printLegal (myList); printf(" Enter the name of a person to look up their age : "); scanf("%s", input1); while ( stremp (inputi, "xxx") != 0 ) { printf ("\t%s is 8d years old ", inputl, 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) { printf ("The list is : "); while (ptr) { printf ("[%s-%d] ", ptr->name, ptr->age): ptr = ptr->next; } printf(" "); return; Person *addFront (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

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

Recommended Textbook for

Principles Of Database Systems With Internet And Java Applications

Authors: Greg Riccardi

1st Edition

020161247X, 978-0201612479

More Books

Students also viewed these Databases questions