You are on page 1of 7

#

#
#
#

include
include
include
include

<stdio.h>
<conio.h>
<string.h>
<stdlib.h>

int Menu ();


void Create ();
void Search ();
void Change ();
void Delete ();
void PrintList ();
void Destroy ();
void Pop ();
void Push ();
void Peek ();
struct node
{
char lname[20],fname[20],mname[20],tel[20],nullStr[20];
struct node *next;
};
typedef struct node node;
node *start, *temp;
int Menu()
{
int ch;
printf("\n\n TELEPHONE DIRECTORY \n");
printf(" ===================\n ");
printf("[1]. Create \n");
printf(" [2]. Search \n");
printf(" [3]. Change \n");
printf(" [4]. Delete \n");
printf(" [5]. PrintList \n");
printf(" [6]. Destroy \n");
printf(" [7]. Pop \n");
printf(" [8]. Push \n");
printf(" [9]. Peek \n");
printf(" [10]. EXIT \n");
printf("\n Enter Number: ");
scanf("%d", &ch);
return ch;
}
void Create()
{
node *ptr,*prev;
temp=(node *)malloc(sizeof(node));
printf(" First Name: ");
scanf("%s", temp->fname);
printf(" \n Middle Name: ");
scanf("%s",temp->mname);
printf(" \n Last Name: ");
scanf("%s", temp->lname);
printf(" \n Telephone No.: ");
scanf("%s", temp->tel);
temp->next=NULL;

if(start==NULL) start=temp;
else
{
prev=ptr=start;
while(strcmp(temp->fname,ptr->fname)>0)
{
prev=ptr;
ptr= ptr->next;
if (ptr == NULL) break;
}
if(ptr==prev)
{
temp->next=start;
start=temp;
}
else if(ptr==NULL)
prev->next=temp;
else
{
temp->next=ptr;
prev->next=temp;
}
}
}
void Push ()
{
node *ptr,*prev;
temp=(node *)malloc(sizeof(node));
printf(" First Name: ");
scanf("%s", temp->fname);
printf(" \n Middle Name: ");
scanf("%s",temp->mname);
printf(" \n Last Name: ");
scanf("%s", temp->lname);
printf(" \n Telephone No.: ");
scanf("%s", temp->tel);
temp->next=NULL;
if(start==NULL) start=temp;
else
{
prev=ptr=start;
if(ptr==prev)
{
temp->next=start;
start=temp;
}
else if(ptr==NULL)
prev->next=temp;
else
{
temp->next=ptr;
prev->next=temp;
}
}
}
void Pop ()
{
node *ptr,*prev,*temp;

char yn='n';
if(start==NULL)
{
printf("\n\t\t\tTelephone Directory is Empty....\n");
getch();
return;
}
prev=ptr=start;
if(ptr!=NULL)
{
printf("\nDeleting Record.....Confirm [y/n]: ");
yn=getch();
printf("\n\n--------------------------------------------------------\n");
printf(" First Name : %s\n",ptr->fname);
printf(" Middle Name : %s \n",ptr->mname);
printf(" Last Name : %s\n",ptr->lname);
printf(" Phone Number : %s\n",ptr->tel);
printf("--------------------------------------------------------");
if(yn=='y')
{
if (ptr==start)
{
temp=start->next;
free(start);
start=temp;
}
else
{
temp=ptr->next;
free(ptr);
prev->next=temp;
}
printf("\n\n Record Deleted...");
}
else
printf("\n\nRecord not Deleted...");
}
else
{
printf("\nNo Matching Records Found ...");
}
getch();
}
void Peek ()
{
node *ptr,*prev;
if(start==NULL)
{
printf("\n\t\t\tTelephone Directory is Empty...\n");
getch();
return;
}
prev=ptr=start;
printf("\t\t------------------------------\n");
if(ptr==start)
{
printf("\t\tFirst Name : %s", ptr->fname);
printf ("\n\t\tMiddle Name : %s",ptr->mname);

printf("\n\t\tLast Name : %s", ptr->lname);


printf("\n\t\tTelephone No. : %s", ptr->tel);
printf("\n\t\t------------------------------\n");
}
getch();
}
void Search()
{
node *ptr;
char str[20];
if(start==NULL)
{
printf("\n\t\t\tTelephone Directory Is Empty...\n\n");
getch();
return;
}
printf(" First Name to Find : ");
scanf("%s",str);
ptr=start;
while(strcmp(ptr->fname,str)!=0)
{
ptr= ptr->next;
if (ptr == NULL) break;
}
if(ptr!=NULL)
{
printf(" First Name : %s\n",ptr->fname);
printf(" Middle Name : %s \n",ptr->mname);
printf(" Last Name : %s\n",ptr->lname);
printf(" Phone Number : %s\n",ptr->tel);
}
else
{
printf("No Matching Records Found .......\n");
}
getch();
}
void Change()
{
node *ptr;
char str[20];
if(start==NULL)
{
printf("\n\t\t\tTelephone Directory is Empty....\n");
getch();
return;
}
printf(" First Name to Edit : ");
scanf("%s",str);
ptr=start;
while(strcmp(ptr->fname,str)!=0)
{
ptr=ptr->next;
if (ptr == NULL) break;
}
if(ptr!=NULL)
{
printf(" First Name : %s", ptr->fname);

scanf("%s", ptr->fname);
printf (" Middle Name : %s", ptr->mname);
scanf("%s", ptr->mname);
printf(" Last Name : %s", ptr->lname);
scanf("%s", ptr->lname);
printf(" Phone Number : %s", ptr->tel);
scanf("%s", ptr->tel);
}
else
{
printf("No Matching Records Found .......\n");
}
getch();
}
void Delete()
{
node *ptr,*prev,*temp;
char str[20],yn='n';
if(start==NULL)
{
printf("\n\t\t\tTelephone Directory is Empty....\n");
getch();
return;
}
printf(" First Name to Delete : ");
scanf("%s",str);
prev=ptr=start;
while(strcmp(ptr->fname,str)!=0)
{
prev=ptr;
ptr=ptr->next;
if (ptr == NULL) break;
}
if(ptr!=NULL)
{
printf("\nDeleting Record.....Confirm [y/n]: ");
yn=getch();
printf("\n\n--------------------------------------------------------\n");
printf(" First Name : %s\n",ptr->fname);
printf(" Middle Name : %s \n",ptr->mname);
printf(" Last Name : %s\n",ptr->lname);
printf(" Phone Number : %s\n",ptr->tel);
printf("--------------------------------------------------------");
if(yn=='y')
{
if (ptr==start)
{
temp=start->next;
free(start);
start=temp;
}
else
{
temp=ptr->next;
free(ptr);
prev->next=temp;
}

printf("\n\n1 Record Deleted...");


}
else
printf("\n\nRecord not Deleted...");
}
else
{
printf("\nNo Matching Records Found ...");
}
getch();
}
void PrintList()
{
node *ptr;
if(start==NULL)
{
printf("\n\t\t\tTelephone Directory is Empty...\n");
getch();
return;
}
printf("\t\t------------------------------\n");
for(ptr=start; ptr!=NULL; ptr=ptr->next)
{
printf("\t\tFirst Name : %s", ptr->fname);
printf ("\n\t\tMiddle Name : %s",ptr->mname);
printf("\n\t\tLast Name : %s", ptr->lname);
printf("\n\t\tTelephone No. : %s", ptr->tel);
printf("\n\t\t------------------------------\n");
}
getch();
}
void Destroy()
{
node *ptr,*temp,*prev;
char yn = 'n';
printf("\nDeleting Record.....Confirm [y/n]: ");
yn=getch();
for(ptr=start; ptr!=NULL; ptr=ptr->next)
{
printf("\n\n--------------------------------------------------------\n");
printf(" First Name : %s\n",ptr->fname);
printf(" Middle Name : %s \n",ptr->mname);
printf(" Last Name : %s\n",ptr->lname);
printf(" Phone Number : %s\n",ptr->tel);
printf("--------------------------------------------------------");
if(yn=='y')
{
if (ptr==start)
{
temp=start->next;
free(start);
start=temp;
}
else
{

temp=ptr->next;
free(ptr);
prev->next=temp;
}
printf("\n\n Record Deleted...");
}
else
printf("\n\nRecord not Deleted...");
}
printf(" All Contacts Have Been Deleted.\n");
}
void main()
{
int ch;
start=(node *)malloc(sizeof(node));
start=NULL;
do
{
ch=Menu();
switch(ch)
{
case 1: Create();
break;
case 2: Search();
break;
case 3: Change();
break;
case 4: Delete();
break;
case 5: PrintList();
break;
case 6: Destroy();
break;
case 7: Pop();
break;
case 8: Push();
break;
case 9: Peek();
break;
}
}while(ch!=10);
}

You might also like