I wrote a mailing list program using linked list that works great. But I am trying to change it to include a menu to add and delete phone listing. I have tried several things, but I can not figure it out. Any assistance will be appreciated. Thanks.
Here is the coding.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct telephonelist_type
{
char name[30];
char phone_no[15];
struct telephonelist_type *nextlisting;
};
struct telephonelist_type *phonelist, *current; /* two pointers to structures - type telephonelist_type */
void input(struct telephonelist_type *); /*function prototype to input listing*/
void display(struct telephonelist_type *); /*function prototype to display listing*/
void mldelete(struct telephonelist_type *); /*function prototype to delete listing*/
int menu_select(void); //function prototype for menu selection
int main()
{
//Get a pointer to the first structure in the list
phonelist = (struct telephonelist_type *) malloc(sizeof(struct telephonelist_type*));
current = phonelist;
for(;;){
switch(menu_select()){
case 1: input(current); /* enter an address */
break;
case 2: mldelete(current); /* remove an address */
break;
case 3: exit(0); /*exit(0) */
break;
}
}
return 0;
display(phonelist);
}
/* Select an operation. */
int menu_select(void)
{
int s = 0;
//int c;
printf("\n************************************");
printf("\n Directory Listing ");
printf("\n************************************");
printf("\n");
printf(" 1. Enter a telephone listing\n");
printf(" 2. Delete a telephone listing\n");
printf(" 3. Quit");
printf("\n************************************");
do {
printf("\n\n\nEnter your choice: ");
scanf("%d", s);
} while(s<0 || s>3);
return s;
}
//Accepts the Names and Phone Numbers
void input(struct telephonelist_type *record)
{
//Requests User to Enter Record.
printf("Enter a Name: ");
gets(record->name);
printf("Enter a Phone Number: ");
gets(record->phone_no);
current->nextlisting = (struct telephonelist_type *) malloc(sizeof(struct telephonelist_type));
current = current->nextlisting;
current->nextlisting = NULL;
return;
}
/* Remove the last element from the list. */
void mldelete(struct telephonelist **last)
{
struct telephonelist *contents;
*last = NULL;
}
//Contents is a pointer ato a structure of telephonelist_type
void display(struct telephonelist_type *contents)
{
printf("\nThe list contains the following records: \n");
while (contents != NULL) //Display until end of linked list
{
printf("\n%-30s %-20s", contents->name, contents->phone_no);
contents = contents->nextlisting;
}
return;
}