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

This lab is a take-home lab. There is no formal lab next week (April 4 and April

ID: 3811002 • Letter: T

Question

This lab is a take-home lab. There is no formal lab next week (April 4 and April 6). Complete the assigned task and submit it via Blackboard before 4pm on Thursday, April 6. First, create a directory called lab9 on your machine using mkdir lab9. Second, complete the program lab9.c shown below - write the eight functions specified. #include #include #include typedef struct person {char *name; int age; struct person *next;} Person; Person *addFront(Person *, char*, int);//add at front, for people under 21 Person *addRear(Person *, char *, int);//add at rear, for people 21 or older void print(Person *);//print the list (name and age for each item) void printLast(Person *);//print the last person (and age) in the list void printFirst(Person *);//print the first person (and age) in the list int size(Person *);//return the length of the list int inList(Person *, char*);//returns 1 if the name is in the list, else 0 int getAge(Person *, char *);//returns the age of the person specified int getAge (Person*, char*);//return -1 if the person is not in the list int main(void) {Person *myList = NULL; int theAge; char theName[128]; printf("Enter the name of a person and an age (an integer): "); scanf("%s %d", theName, &theAge;); while (strcmp(theName, "quit") ! = 0) {if (theAge

Explanation / Answer

Here is the code for you:

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

Person *addFront(Person*, char*, int);
Person *addRear(Person*, char*, int);
void print(Person*);
void printLast(Person*);
void printFirst(Person*);
int size(Person*);
int inList(Person*, char*);
int getAge(Person*, char*);

int main() {
   Person *myList = NULL;
   int theAge;
   char theName[128];
   printf("Enter the name of a person and an age (an integer): ");
   scanf("%s%d", theName, &theAge);
   while(strcmp(theName, "quit") != 0) {
       if(theAge < 21)
           myList = addFront(myList, theName, theAge);
       else
           myList = addRear(myList, theName, theAge);
       printf("Enter another name and age (or "quit" an any integer when done): ");
       scanf("%s%d", theName, &theAge);
   }
   printf(" The list is ");
   print(myList);
   printf(" The list has %d elements ", size(myList));
   printf(" The first person in the list (and their age) is: ");
   printFirst(myList);
   printf(" The last person in the list (and their age) is: ");
   printLast(myList);
   printf(" ");
   printf("Enter the name of a person (or "exit" to exit): ");
   scanf("%s", theName);
   while(strcmp(theName, "exit") != 0) {
       if(inList(myList, theName))
           printf(" Found %s (age is %d) ", theName, getAge(myList, theName));
       else
           printf(" %s was not found in the list ", theName);
       printf(" Enter the name of a person (or "exit" to exit): ");
       scanf("%s", theName);  
   }
   return 0;
}

Person *addFront(Person *start, char *theName, int theAge) {
   Person *person = (Person*)malloc(sizeof(Person));
   strcpy(person->name, theName);
   person->age = theAge;
   person->next = NULL;
   if(start == NULL)
       return person;
   person->next = start;
   start = person;
   return start;  
}
Person *addRear(Person *start, char *theName, int theAge) {
   Person *person = (Person*)malloc(sizeof(Person));
   strcpy(person->name, theName);
   person->age = theAge;
   person->next = NULL;
   if(start == NULL)
       return person;
   Person *temp = start;
   while(temp->next != NULL)
       temp = temp->next;
   temp->next = person;
   return start;      
}
void print(Person *temp) {
   while(temp != NULL) {
       printf("%s, %d ", temp->name, temp->age);
       temp = temp->next;
   }
}
void printLast(Person *temp) {
   while(temp->next != NULL)
       temp = temp->next;
   printf("%s, %d ", temp->name, temp->age);  
}
void printFirst(Person *temp) {
   printf("%s, %d ", temp->name, temp->age);
}
int size(Person *temp) {
   int count = 0;
   while(temp != NULL) {
       temp = temp->next;
       count++;
   }
   return count;
}
int inList(Person *temp, char *theName) {
   while(temp != NULL)
       if(strcmp(theName, theName) == 0)
           return 1;
   return 0;      
}
int getAge(Person *temp, char *theName) {
   while(temp != NULL)
       if(strcmp(theName, theName) == 0)
           return temp->age;
   return 0;
}

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