Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This lab maintains a linked list of people, where each node contains a name (one

ID: 3705601 • Letter: T

Question

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

#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(" %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;

Explanation / Answer

Solution:

code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct person {
        char *name;
        int age;
        struct person *next;
} Person;

void print(Person *);                   // prints the entire list
Person *addFront(Person *, char *, int);// adds a new node to the front of the list
Person *addRear(Person *, char *, int); // adds a new node to the end of the list
void printLegal(Person *);              // prints the names who are 21 or older
int getAge(Person *, char *);           // 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", input1, &input2);
        while (input2 != 0) {
                if (input2 > 20)
                   myList = addRear(myList, input1, input2);
                else
                   myList = addFront(myList, input1, input2);
                printf("Enter a name (one word) and age, enter 'xxx' and 0 to exit : ");
                scanf("%s %d", input1, &input2);
        }
        printf(" "); print(myList);
        printf(" "); printLegal(myList);
        printf(" Enter the name of a person to look up their age : ");
        scanf("%s", input1);
        while ( strcmp(input1, "xxx") != 0 ) {
                printf(" %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) {
        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;
}

Person *addRear(Person *head, char *n, int a) {
   Person *ret = head;
   Person *newNode = malloc( sizeof(Person) );
    newNode->name = malloc( strlen(n) + 1 );
    strcpy(newNode->name, n);
    newNode->age = a;
    newNode->next = NULL;
    if(head == NULL) {
       return newNode;
   } else {
       while(head->next) {
           head = head->next;
       }
       head->next = newNode;
   }
   return ret;
}

void printLegal(Person *ptr) {
   printf("The Legal list is : ");
    while (ptr) {
       if(ptr->age >= 21)
           printf("[%s-%d] ", ptr->name, ptr->age);
       ptr = ptr->next;
   }
    printf(" ");
    return;
}

int getAge(Person *head, char *name) {
   while(head != NULL) {
       if(strcmp(head->name, name) == 0) {
           return head->age;
       }
       head = head->next;
   }
   return -1;
}

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote