I can\'t figure out how to write this program. Please write a completed program
ID: 3528461 • Letter: I
Question
I can't figure out how to write this program. Please write a completed program that runs using C. I've used all my resources and I still don't understand what I am doing wrong. I am offering 3,000 points. Write a program to maintain a phone book.
The phone book must be maintained as a linked list. Your program must have the following menu:
1. Add entry The user will be prompted to enter a name and a phone number, and the entry will be inserted into the list.
2. Lookup number The user will be prompted to enter the name they are looking for, if the name exists, it will print the entire entry. If not, print "not found". You can assume for this assignment there will not be multiple entries with the same name. Hint: consider using strcmp to compare the name entered with the name in each phonebook entry.
3. Delete entry The user will be prompted to enter the last name of the user to be deleted. The matching entry will be displayed with a message to confirm the deletion (y/n). Upon confirmation, the entry will be deleted. If the deletion is not confirmed, they will be returned to the main menu.
4. Print the entire address book.
5. Quit Make sure you make proper use of functions and proper code formatting
Explanation / Answer
/*---------------------*/
/* TELEPHONE DIRECTORY */
/*---------------------*/
# include <stdio.h>
# include <conio.h>
# include <string.h>
# include <stdlib.h>
int menu(); //main menu
void add(); //add to list
void find(); //search from the list
void edit(); //edit the record
void del(); //delete from the list
void disp(); //display all
struct node {
char lname[20], fname[20], tel[15];
struct node *dnext;
};
typedef struct node node;
node *dstart, *dtemp;
int menu()
{
int ch;
gotoxy(30,5);
printf(" TELEPHONE DIRECTORY ");
gotoxy(30,6);
printf(" =================== ");
gotoxy(3,24);
gotoxy(30,10);
printf(" 1. Add ");
gotoxy(30,11);
printf(" 2. Find ");
gotoxy(30,12);
printf(" 3. Edit ");
gotoxy(30,13);
printf(" 4. Delete ");
gotoxy(30,14);
printf(" 5. Display All ");
gotoxy(30,15);
printf(" 6. EXIT ");
gotoxy(30,20);
printf(" Enter your choice(1-6):");
gotoxy(55,20);
scanf("%d", &ch);
return ch;
}
void add()
{
node *dptr,*dprev;
dtemp=(node *)malloc(sizeof(node));
printf("First name: ");
scanf("%s", dtemp->fname);
printf("Last name:");
scanf("%s", dtemp->lname);
printf("Telephone No.: ");
scanf("%s", dtemp->tel);
dtemp->dnext=NULL;
if(dstart==NULL) dstart=dtemp;
else {
dprev=dptr=dstart;
while(strcmp(dtemp->fname,dptr->fname)>0){
dprev=dptr;
dptr= dptr->dnext;
if (dptr == NULL) break;
}
if(dptr==dprev) {
dtemp->dnext=dstart;
dstart=dtemp;
}
else if(dptr==NULL)
dprev->dnext=dtemp;
else {
dtemp->dnext=dptr;
dprev->dnext=dtemp;
}
}
}
void find()
{
node *dptr;
char dstr[20];
if(dstart==NULL) {
printf(" Telephone Directory is Empty.... ");
getch();
return;
}
printf("First Name to Find : ");
scanf("%s",dstr);
dptr=dstart;
while(strcmp(dptr->fname,dstr)!=0){
dptr= dptr->dnext;
if (dptr == NULL) break;
}
if(dptr!=NULL) {
printf("First Name : %s ",dptr->fname);
printf("Last Name : %s ",dptr->lname);
printf("Phone Number : %s ",dptr->tel);
}
else {
printf("No Matching Records Found ....... ");
}
getch();
}
void edit()
{
node *dptr;
char dstr[20];
if(dstart==NULL) {
printf(" Telephone Directory is Empty.... ");
getch();
return;
}
printf("First Name to Edit : ");
scanf("%s",dstr);
dptr=dstart;
while(strcmp(dptr->fname,dstr)!=0){
dptr= dptr->dnext;
if (dptr == NULL) break;
}
if(dptr!=NULL) {
printf("First Name : %s", dptr->fname);
gotoxy(14,19);
scanf("%s", dptr->fname);
printf("Last Name : %s", dptr->lname);
gotoxy(13,20);
scanf("%s", dptr->lname);
printf("Phone Number : %s", dptr->tel);
gotoxy(16,21);
scanf("%s", dptr->tel);
}
else {
printf("No Matching Records Found ....... ");
}
getch();
}
void del()
{
node *dptr,*dprev,*dtemp;
char dstr[20],dyn='n';
if(dstart==NULL) {
printf(" Telephone Directory is Empty.... ");
getch();
return;
}
printf("First Name to Delete : ");
scanf("%s",dstr);
dprev=dptr=dstart;
while(strcmp(dptr->fname,dstr)!=0){
dprev=dptr;
dptr= dptr->dnext;
if (dptr == NULL) break;
}
if(dptr!=NULL){
printf(" Deleting Record.....Confirm [y/n]: ");
dyn=getch();
printf(" ---------------------------------------------------------");
printf(" First Name : %s ",dptr->fname);
printf("Last Name : %s ",dptr->lname);
printf("Phone Number : %s ",dptr->tel);
printf("---------------------------------------------------------");
if(dyn=='y') {
if (dptr==dstart) {
dtemp=dstart->dnext;
free(dstart);
dstart=dtemp;
}
else {
dtemp=dptr->dnext;
free(dptr);
dprev->dnext=dtemp;
}
printf(" 1 Record Deleted....");
}
else
printf(" Record not Deleted....");
}
else {
printf(" No Matching Records Found .......");
}
getch();
}
void disp()
{
node *dptr;
if(dstart==NULL) {
printf(" Telephone Directory is Empty.... ");
getch();
return;
}
clrscr();
printf(" ------------------------------ ");
for(dptr=dstart; dptr!=NULL; dptr=dptr->dnext) {
printf(" First name: %s", dptr->fname);
printf(" Last name:%s", dptr->lname);
printf(" Telephone No.: %s", dptr->tel);
printf(" ------------------------------ ");
}
getch();
}
void main()
{
int ch;
dstart=(node *)malloc(sizeof(node));
dstart=NULL;
do{
clrscr();
ch=menu();
clrscr();
switch(ch) {
case 1: add();
break;
case 2: find();
break;
case 3: edit();
break;
case 4: del();
break;
case 5: disp();
break;
}
}while(ch!=6);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.