Can Someone Please Improve My Coding So That I Can Sorting Using Contact_Name(Char)?I Really Need It.I've Been Looking For Several Days And I Still Dont Know How To Imply It Using Merged Sort
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define TRUE 1//set 1 equal to true
#define FALSE 0//set 0 equal to false
void new_contact();
void list_contact();
void delete_contact();
void sorting_contact();
void search_contact();
struct contact *mergesortcontact_name(struct contact *);
struct contact *combinecontact_name(struct contact *,struct contact*);
struct contact
{
char phone_number[20];
char contact_name[70];
char address [80];
struct contact *ptrnext;
};
struct contact *headptr, *newptr, *currentptr, *previousptr;
int main()
{
system("color B");
char ch;
int choice=TRUE;
headptr=(struct contact *)NULL;
while(choice==TRUE)
{
printf("\n\t\tWelcome to Smart Phone Book System");
printf("\n\nAdd a contact :A\nList a contact :L\nDelete a contact :D\nSearch for contact :S\nSort phonebook :T\nExit :x\n");
printf("\nPlease enter the mode you want:");
scanf(" %c%*c", &ch);
switch(ch)
{
case 'A': new_contact();system("CLS");break;
case 'L': system("CLS");list_contact();break;
case 'D': system("CLS");delete_contact();break;
case 'S': search_contact();break;
case 'T': system("CLS");sorting_contact(); break;
case 'X': choice=FALSE; break;
default: system("CLS");printf("\nPlease enter a proper mode");
}
}
return 0;
}
void new_contact()
{
newptr=(struct contact *)malloc(sizeof (struct contact));
if (headptr==(struct contact *)NULL)
{
headptr=newptr;
newptr->ptrnext=(struct contact *)NULL;
}
else
{
newptr->ptrnext=headptr;
headptr=newptr;
}
printf("\nEnter a new name : ");
fgets( newptr->contact_name,sizeof(newptr->contact_name),stdin);
printf("\nEnter a number : ");
fgets(newptr->phone_number,sizeof(newptr->phone_number),stdin);
printf("\nEnter address : ");
fgets(newptr->address,sizeof(newptr->address),stdin);
}
void list_contact()
{
int i=1;
if (headptr==(struct contact*)NULL)
{
printf("\nEmpty list");
return;
}
currentptr=headptr;
do
{
printf("\n----------------------------\n");
printf("\nContact number %d",i);
printf("\n\nName : %s", currentptr->contact_name);
printf("\nPhone number : %s", currentptr->phone_number);
printf("\nAddress : %s", currentptr->address);
i++;
printf("\n");
currentptr=currentptr->ptrnext;
}
while(currentptr !=(struct contact *)NULL);
}
void delete_contact()
{
char contact_name_delete[20];
if (headptr==(struct contact *)NULL)
{
printf("\n\nThe list is empty. Cannot delete!!!\n");
}
else
{
printf("\nDelete list by name.");
printf("\nEnter contact name to delete: ");
fgets(contact_name_delete,sizeof(contact_name_delete),stdin);
currentptr=headptr;
while(currentptr ->ptrnext!=(struct contact *)NULL)
{
if (strncmp(currentptr->contact_name,contact_name_delete,6)==0)
{
break;
}
else
{
previousptr=currentptr;
currentptr=currentptr->ptrnext;
}
}
if (strncmp(currentptr->contact_name,contact_name_delete,6)==0)
{
if ( currentptr==headptr)
{
headptr=currentptr ->ptrnext;
free(currentptr);
}
else
{
previousptr->ptrnext=currentptr->ptrnext;
free(currentptr);
}
printf("\nPhone number were deleted!\n");
}
else
printf("\nNumber to be deleted is not in the list!!! ");
}
}
void search_contact()
{ currentptr=headptr;
int count_match;
count_match=0;
char contact_name_add[30];
if (headptr ==(struct contact *)NULL)
{
printf("The phonebook is empty..");
}
else
{
printf("Please enter the name of your contact for searching : ");
fgets(contact_name_add,sizeof(contact_name_add),stdin);
printf("\n\nThe computer will now search for your contact\n");
while (currentptr != NULL)
{
if (strncmp(currentptr->contact_name, contact_name_add, 5) == 0)
{
printf("Name : %s\n", currentptr->contact_name);
printf("Phone Number : %s\n", currentptr->phone_number);
printf("Address : %s\n", currentptr->address);
count_match++;
}
currentptr = currentptr->ptrnext;
}
if (currentptr == NULL && count_match==0)
{printf("\n\nThe name you seek ( %s ) is not in the phonebook\n", contact_name_add);}
}
}
void sorting_contact()
{
if (headptr==NULL)
{
printf("\n\n\tNo contacts found.Nothing can be sorted\n\n\t");
return;
}
printf("\n\t\tThe sorted contact by name: ");
headptr=mergesortcontact_name(headptr);
currentptr=headptr;
do
{
printf("\n\n\tName : %s \n",currentptr->contact_name);
currentptr=currentptr->ptrnext;
}
while(currentptr!=NULL);
}
struct contact *mergesortcontact_name(struct contact *head)
{
struct contact *head1,*head2,*p1,*p2;
if (head==NULL||(head->ptrnext==NULL))return head;
head1=head;
head2=head->ptrnext;
while((head2!=NULL) &&(head2->ptrnext!=NULL))
{
head=head->ptrnext;
head2=head2->ptrnext->ptrnext;
}
head2=head->ptrnext;
head->ptrnext=NULL;
p1=mergesortcontact_name(head1);
p2=mergesortcontact_name(head2);
return combinecontact_name(p1,p2);
}
struct contact *combinecontact_name(struct contact *head1,struct contact *head2)
{
struct contact *head3;
if(head1==NULL)return head2;
if(head2==NULL)return head1;
if(head1->contact_name<head2->contact_name)
{
head3=head1;
head3->ptrnext=combinecontact_name(head1->ptrnext,head2);
}
else
{
head3=head2;
head3->ptrnext=combinecontact_name(head1,head2->ptrnext);
}
return head3;
}