Your task is to manage phone book records using doubly linked lists with dummy n
ID: 3535323 • Letter: Y
Question
Your task is to manage phone book records using doubly linked lists with dummy node or without dummy node (choose one) . You will have to implement . the basic functions that support addition and deletion of nodes to linked list. In addition, consider the following specifications: 1. populate a linked list by reading from input file. Each person ’s data should be stored in a separate node in the linked list. The records are stored by alphabetical order of name 2. if command - line option “ - p †is provided, store the person ’ s records by order of phone number . . 3. I mplement the functions in the following screenshot. phone book manager (command) ================================= press 1 to display press 2 to insert pr ess 3 to delete press 4 to search press q to quit ================================= > Enter the command: 1 > Edward 8067945007 Daniel 8067945006 Jack 8067945005 Joshua 8067945004 James 8067945003 Matthew 8067945002 Harold 8067945001 ... > Enter the command: 2 input the name and phone: > Dora 8067945001 // you can add same name to the phone book, but phone numbe r should be unique. if you input the existing phone number, the program display the messages (ex, "phone number is exists, choose another phone number") data is stored. // when data is stored, it will display the stored data with previous and next data. ------------------------- Ethan 8067944000 Dora 8067945001 Daniel 8067945006 ----- -------------------- > Enter the command 3 > input the name > Dora data is deleted > Enter the command 4 > Search with name(press n) or phone(press p) ? > n > input the name : > Edward // it should display the searched data with next two people. Edward 8067945007 Ethan 8067944000 David 8067944002 Your task is to manage phone book records using doubly linked lists with dummy node or without dummy node (choose one) . You will have to implement . the basic functions that support addition and deletion of nodes to linked list. In addition, consider the following specifications: 1. populate a linked list by reading from input file. Each person ’s data should be stored in a separate node in the linked list. The records are stored by alphabetical order of name 2. if command - line option “ - p †is provided, store the person ’ s records by order of phone number . . 3. I mplement the functions in the following screenshot. phone book manager (command) ================================= press 1 to display press 2 to insert pr ess 3 to delete press 4 to search press q to quit ================================= > Enter the command: 1 > Edward 8067945007 Daniel 8067945006 Jack 8067945005 Joshua 8067945004 James 8067945003 Matthew 8067945002 Harold 8067945001 ... > Enter the command: 2 input the name and phone: > Dora 8067945001 // you can add same name to the phone book, but phone numbe r should be unique. if you input the existing phone number, the program display the messages (ex, "phone number is exists, choose another phone number") data is stored. // when data is stored, it will display the stored data with previous and next data. ------------------------- Ethan 8067944000 Dora 8067945001 Daniel 8067945006 ----- -------------------- > Enter the command 3 > input the name > Dora data is deleted > Enter the command 4 > Search with name(press n) or phone(press p) ? > n > input the name : > Edward // it should display the searched data with next two people. Edward 8067945007 Ethan 8067944000 David 8067944002Explanation / Answer
#include <stdio.h> // Standard C input & output library
#include <stdlib.h> // mallac, callac, and free functions
#include <string.h> // String functions (string copy, string compare)
typedef struct PhoneBookEntry // Declaration of phone book structure
{
char FirstName[21];
char LastName[21];
int PhoneNum;
} person;
// FUNCTION PROTOTYPES:
void Add (int*, int, person*); // Function to add an entry
void Del (int*, int, person*); // Function to delete an entry
void Print (int*, int, person*); // Function to print the phone book
void Sort (person *);
int main() { // Main driving function
int select=0; // Integer variable for user input of phone book operation
int contact_ctr=0; // Counter to track number of contacts
int i=0; // Index for printing "for" loop
person contacts[50]; // Declare an array of phone book entries
person* pcontacts; // Declare a pointer for array Entries
pcontacts = (person*) calloc(0, sizeof(person)); // Set pointer to allocated/initialized block of memory, size of person struct
if (pcontacts == NULL)
{printf("Out of memory! Aborting...");
return 1;}
else {} // Do nothing!
do { // Print menu to screen, ask user for selection...
printf(" Phone Book Application: ");
printf("1) Add friend ");
printf("2) Delete friend ");
printf("3) Show phone book ");
printf("4) Exit ");
printf("What do you want to do? ");
scanf("%d", &select); // Store user input in variable "select"
getchar(); // Clear the input buffer
switch (select)
{
case 1: // User wants to add an entry
pcontacts = realloc(pcontacts, contact_ctr * sizeof(person)); // Reallocate memory for additional contact
Add (&contact_ctr, i, contacts);
break;
case 2: // User wants to delete an entry
Del (&contact_ctr, i, contacts);
break;
case 3: // User wants to print the phone book
Print (&contact_ctr, i, contacts);
break;
case 4: // User wants to quit, break the switch and exit to loop
break;
default: // User entered a value other than 1,2,3, or 4.
printf("You entered an invalid selection. Try again. ");
break;
} // End switch statement
} // End do loop
while (select!=4);
printf(" This phone book will now close. Goodbye! ");
// Pause program until user inputs any character
getchar();
// Return statement
return 0;
} // End main
void Add (int *contact_ctr, int i, person *contacts)
{
(*contact_ctr)++;
// Need to add realloc failsafe, if NULL
printf(" First name: ");
scanf("%s", &contacts[*contact_ctr-1].FirstName);
printf("Last name: ");
scanf("%s", &contacts[*contact_ctr-1].LastName);
printf("Phone number (7-digit, no dash): ");
scanf("%d", &contacts[*contact_ctr-1].PhoneNum);
printf("Record added to the phone book ");
} // End function Add
void Del (int *contact_ctr, int i, person *contacts)
{
char delTempf[21]; // Temporary string for deletion of an entry - first name
char delTempl[21]; // Temporary string for deletion of any array - last name
char nullStr[21] = {""}; // Null string for use in deleting entries
printf(" First name: ");
scanf("%s", &delTempf);
printf("Last name: ");
scanf("%s", &delTempl);
// compare strings, find entry or return entry not found
for (i=0; i<*contact_ctr; i++)
{
if (strcmp(delTempf, contacts[i].FirstName) == 0)
{
strcpy(contacts[i].FirstName, nullStr);
strcpy(contacts[i].LastName, nullStr);
strcpy(contacts[i].PhoneNum, nullStr);
free(&contacts[i]);
(*contact_ctr)--; // Contact deleted, update the contact total counter
break;
}
else
{
printf(" Entry not found. ");
}
} // End for loop
printf("Record deleted from the phone book ");
} // End function Del
void Print (int *contact_ctr, int i, person *contacts)
{
char nullStr[21] = {""}; // Null string
if (*contact_ctr > 0) // If there are entries, print them
{
printf(" Phone Book Entries: ");
for(i=0; i<*contact_ctr; i++)
{
if (strcmp(nullStr, contacts[i].FirstName) != 0) // If this index is NOT null, print it
{
printf("%s %s %d ", contacts[i].FirstName, contacts[i].LastName, contacts[i].PhoneNum);
}
else {} // Do nothing (skip this index because no entry exists)
} // end for loop
}
else // Else the book is empty, tell the user
{
printf(" The phone book is currently empty. ");
}
} // End function Print
// keep looping trough until nothing is swapped
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.