b) You have been hired to write a ‘phonebook’ application for a small company. The phonebook stores names of company employees and their associated phone- numbers. Given an employee name it should be possible to retrieve the employee’s phone number. It should also be possible to add a name and associated number when a new employee joins the company; and to delete a name and number when the named employee leaves the company.
i) Explain how you might implement the phonebook application using a doubly-linked list, paying attention to the operations for getting the phone number for a given name and adding and deleting employee phone numbers. [10 marks]
ii) Suppose the company becomes very successful and grows to the point where it has many thousands of employees. Is the implementation based on a doubly linked list adequate? Give reasons for your answer. [5 marks]
Expert Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
This structure contains a template for records.
* Each record holds a Name, Phone number, and pointer to the next
* item in the list.
*/
struct entry {
int entry_number;
char *employee_name;
char *employee_number;
struct entry *prev;
struct entry *next;
};
/* Linked List pointers to head and current items */
struct entry *head = NULL;
struct entry *tail = NULL;
char MENU() {
char input;
printf(“* Menu *n”);
printf(“A: Add employeen”);
printf(“P: retrieve employee number n”);
printf(“D: Delete employeen”);
printf(“Q: Quitn”);
printf(“nEnter Choice: “);
input = getchar(); //get character input
getchar(); //flush the newline/carrage return
return input;
}
/*
* Function: getInput
*
* Description: Grabs an input string from the user and stores it
* into a dynamically allocated memory location.
*
* Variables: char* – String to output to the user describing what to input
*
* Return: A pointer to the string location in memory
*/
char *getInput(char *outputString) {
char *userInput = NULL;
size_t inputBuffer = 0;
int inputCount = 0;
//allocate the userInput
//userInput = (char *)malloc(inputBytes+1);
//not needed with the getline function since it
//automatically calls realloc to manage the memory
printf(“%s”, outputString);
inputCount = getline(&userInput, &inputBuffer, stdin);
//remove the newline character from the string
userInput[inputCount-1] = ‘