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

Write a program in C that uses an array of structures to hold contact informatio

ID: 3761673 • Letter: W

Question

Write a program in C that uses an array of structures to hold contact information for your friends. The program should allow the user to enter as many friends as the user wants. Create functions to add or delete entries in the phone book and to print valid phone book entries. Do not display phone book entries that are invalid or NULL (0). You can assume that all people have unique names. Make sure you allocate and free memory as necessary. You MUST use function for all the options. • You MUST use pointer to structure. • You MUST use dynamic memory allocation. • You MUST check if memory allocation was successful. • You MUST free the memory for deleted record. • You MUST free all assigned memory before program exits. • Do NOT use global variables. • You MUST have input check when it is needed.

Output example:

Phone Book Application

1) Add friend

2) Delete friend

3) Show phone book

What do you want to do: 1

First name: Bob

Last name: Smith

Phone number: 123-4567

Record added to the phone book

Phone Book Application

1) Add friend

2) Delete friend

3) Show phone book

What do you want to do: 1

First name: John

Last name: Doe

Phone number: 893-4567

Record added to the phone book

Phone Book Application

1) Add friend

2) Delete friend

3) Show phone book

What do you want to do: 3

Phone Book Entries:

Bob Smith 123-4567

John Doe 893-4567

Dr. Roman V. Yampolskiy CECS-121

Phone Book Application

1) Add friend

2) Delete friend

3) Show phone book

What do you want to do: 2

First name: John

Last name: Doe

Record deleted from the phone book

Phone Book Application

1) Add friend

2) Delete friend

3) Show phone book

What do you want to do: 3

Phone Book Entries:

Bob Smith 123-4567

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct PhoneBookPage
{
char firstName[50];
char lastName[50];
char phoneNumber[10];
struct PhoneBookPage *next;
}PhoneBookPage;

void AddFriend(PhoneBookPage **book)
{
PhoneBookPage *temp;
temp = (PhoneBookPage *)malloc(sizeof(PhoneBookPage));
if(temp != NULL)
{
printf("Enter the first name of the person: ");
scanf("%s", temp->firstName);
printf("Enter the last name of the person: ");
scanf("%s", temp->lastName);
printf("Enter the phone number: ");
scanf("%s", temp->phoneNumber);
if(*book == NULL)
*book = temp;
else
{
temp->next = *book;
*book = temp;
}
}   
}

int DeleteFriend(PhoneBookPage **book)
{
PhoneBookPage *temp, *x;
char first[50], last[50];
printf("Enter the first name of the person to delete: ");
scanf("%s", first);
printf("Enter the second name of the person to delete: ");
scanf("%s", last);
if(*book == NULL)
return -1;
else
{
if(strcmp(first, (*book)->firstName) == 0 && strcmp(last, (*book)->lastName) == 0)
{
temp = *book;
*book = (*book)->next;
}
else
{
x = *book;
while(x->next != NULL)
{
if(strcmp(first, x->next->firstName) == 0 && strcmp(last, x->next->lastName) == 0)
{
temp = x->next;
x->next = x->next->next;
free(temp);
return 1;
}
x = x->next;
}
}
}
return -1;

}

void ShowFriend(PhoneBookPage *book)
{
while(book != NULL)
{
printf("%s %s %s ", book->firstName, book->lastName, book->phoneNumber);
book = book->next;
}
}
  
int main()
{
struct PhoneBookPage *myBook = NULL;
int ch, result;
while(1)
{
printf("Phone Book Application: ");
printf("1. Add Friend. 2. Delete Friend. 3. Show PhoneBookPage. 4. Exit.");
printf("What do you want to do: ");
scanf("%i", &ch);
switch(ch)
{
case 1:
           AddFriend(&myBook);
           break;
case 2:  
           result = DeleteFriend(&myBook);
           if(result != -1)
           printf("Record Deleted Successfully. ");
           else
           printf("Unable to delete the record. ");
           break;
case 3:
           ShowFriend(myBook);
           break;
case 4:
           exit(0);
default:   printf("Invalid choice. Please go with the menu... ");
}
}
}

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