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

b) You have been hired to write a ‘phonebook’ application for a small company. T

ID: 3845642 • Letter: B

Question

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]


Pseudocode Please!!

Explanation / Answer

The code is given below :

i) code for phonebook :

#include "iostream.h"
#include "conio.h"
#include "string.h"
#include "iomanip.h"
#include "fstream.h"
#include "process.h"

const int TRUE = 1;
const int FALSE = 0;

struct phoneBookApp
{
char empname[15],joindate[15],phonenum[12];
};

struct DoublyLinked_List
{
struct phoneBookApp ph;
struct DoublyLinked_List *next;
};

typedef struct DoublyLinked_List node;

node *head = NULL,*New;

void initialize(node *N)
{
cout << " EnterName of EMployee : ";
cin >> N -> ph.empname;
cout << " Enter the Joining date of an Employee : ";
cin >> N -> ph.joindate;
cout << " Enter phonenum number : ";
cin >> N -> ph.phonenum;
}

int empty(node *n)
{
if(strcmp(n -> ph.empname,"") == 0 && strcmp(n -> ph.joindate,"") == 0 && strcmp(n -> ph.empname,"") == 0)
return 1;
else
return 0;
}

void create()
{
New = new node;
strcpy(New -> ph.empname,"");
strcpy(New -> ph.joindate,"");
strcpy(New -> ph.phonenum,"");
New -> next = NULL;
if(head == NULL)
head = New;
}

int insert()
{
ofstream outfile;
outfile.open("d:\Dbase.txt",ios::app);
if(outfile.fail())
{
cout << "Error : Couldn't open file.";
getch();
exit(0);
}

if(head == NULL)
create();

if(head -> next == NULL)
if(empty(head))
{
initialize(New);
outfile.write((char *) &head,sizeof(head));
return 1;
}

char ntmp[15];
create();
initialize(New);
strcpy(ntmp,New -> ph.empname);
node *temp = head,*loc = head;
if(strcmpi(ntmp,head -> ph.empname) < 0)
{
New -> next = head;
head = New;
}

else
{
while(temp -> next != NULL)
{
temp = temp -> next;
if(strcmpi(ntmp,temp -> ph.empname) > 0)
loc = temp;
}

New -> next = loc -> next;
loc -> next = New;
}
return 1;
}

void search(char *n)
{
node *temp = head;
int found = FALSE;

if(head == NULL)
found = FALSE;

else if(strcmp(n,temp -> ph.empname) == 0)
found = TRUE;

else
{
while(temp -> next != NULL)
{
temp = temp -> next;
if(strcmp(n,temp -> ph.empname) == 0)
{
found = TRUE;
break;
}
}
}

if(found)
{
cout << " empname : " << temp -> ph.empname;
cout << " Address : " << temp -> ph.joindate;
cout << " phonenum NO. : " << temp -> ph.phonenum;
}

else
{
cout << " " << setw(44) << "Data not found";
cout << " " << setw(53) << "Press any key to continue...";
}
}
int remove(char *n)
{
if(head == NULL)
return 0;

node *temp = head,*loc = head;
if(strcmp(n,temp -> ph.empname) == 0)
{
if(temp -> next == NULL)
{
head = NULL;
delete temp;
}

else
{
head = temp -> next;
delete temp;
}
return 1;
}

else
{
while(temp -> next != NULL)
{
temp = temp -> next;
if(temp == NULL)
return 0;

if(strcmpi(n,temp -> ph.empname) == 0)
{
if(temp -> next != NULL)
{
temp = loc -> next;
loc -> next = temp -> next;
delete temp;
}
else
loc -> next = NULL;

return 1;
}

loc = loc -> next;
}
}
return 0;
}
int modify(char *n)
{
node *temp = head;
if(head == NULL)
return 0;
  
if(strcmpi(n,temp -> ph.empname) == 0)
{
cout << " ***** Previous Data *****";
cout << " empname : " << temp -> ph.empname;
cout << " joining date : " << temp -> ph.joindate;
cout << " phonenum NO. : " << temp -> ph.phonenum;
cout << " ***** Enter New Data ***** ";
initialize(head);
return 1;
}

else
{
while(temp -> next != NULL)
{
temp = temp -> next;
if(temp == NULL)
return 0;

if(strcmpi(n,temp -> ph.empname) == 0)
{
cout << " ***** Previous Data *****";
cout << " empname : " << temp -> ph.empname;
cout << " joining date : " << temp -> ph.joindate;
cout << " phonenum NO. : " << temp -> ph.phonenum;
cout << " ***** Enter New Data ***** ";
initialize(temp);
return 1;
}
}
}
return 0;
}

void display()
{
node *temp = head;
if(head == NULL)
{
cout << " " << setw(46) << "Database is empty";
cout << " " << setw(53) << "Press any key to continue...";
return;
}

if(!empty(head))
{
cout << " empname : " << temp -> ph.empname;
cout << " Address : " << temp -> ph.joindate;
cout << " phonenum NO. : " << temp -> ph.phonenum << endl << endl;
}

while(temp -> next != NULL)
{
temp = temp -> next;
cout << " empname : " << temp -> ph.empname;
cout << " Address : " << temp -> ph.joindate;
cout << " phonenum NO. : " << temp -> ph.phonenum << endl;
}
}

int menu()
{
cout << " **********************************" << endl;
cout << " * ---------------------- *" << endl;
cout << " * Phonebook application *" << endl;
cout << " * ---------------------- *" << endl;
cout << " **********************************" << endl << endl;
cout << " -----------------------------------" << endl;
cout << " 1. Add ";
cout << " 2. Search ";
cout << " 3. Delete ";
cout << " 4. Modify ";
cout << " 5. View ";
cout << " 0. Exit ";
cout << " -----------------------------------" << endl << endl;

int choice;
cout << setw(45) << "Enter Choice : ";
cin >> choice;
return choice;
}

int main()
{
char empname[15];
do
{
clrscr();
int choice = menu();
int success;
cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ";
switch(choice)
{
case 1:
success = insert();
if(success)
cout << " " << setw(51) << "Data Inserted Successfully";

cout << " " << setw(53) << "Press any key to continue...";
break;

case 2:
cout << " Enter empname : ";
cin >> empname;
search(empname);
break;

case 3:
cout << " Enter empname : ";
cin >> empname;
success = remove(empname);
if(success)
cout << " " << setw(50) << "Data Deleted Successfully";
else
cout << " " << setw(48) << "Data Doesn't Exists";

cout << " " << setw(53) << "Press any key to continue...";
break;

case 4:
cout << " Enter empname : ";
cin >> empname;
success = modify(empname);
if(success)
cout << " " << setw(50) << "Data Modified Successfully";
else
cout << " " << setw(48) << "Data Doesn't Exists";

cout << " " << setw(53) << "Press any key to continue...";
break;

case 5:
display();
break;

case 0:
clrscr();
cout << " ";
cout << setw(46) << "TELEPHONE DIARY ";
cout << setw(47) << "(Using Linked List)";
cout << " " << setw(51) << "Press any key to halt...";
getch();
return 0;

default:
cout << endl << setw(58) << "Please select appropriate option ";
cout << setw(55) << "Press any key to continue...";
}

getch();
}while(1);
}

ii) Eventhough the company becomes very successful and grows to the point where it has many thousands of employees. then the implementation based on a doubly linked list adequate because the doubly linked list can store lots of data and it is huge interms of memory and the time complexity is also good and it is very fast and reliable.