Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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


C#
#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

    //function prototype or function definition
    void new_contact();//add contact using linked list
    void list_contact();
    void delete_contact();

    void sorting_contact();//sort contact name using merged sort
    void search_contact();//search for contact phone number using searching

   //Function prototype for merge sorting contacts
    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;  //pointer that can point to a node



    int main()
    {

     system("color B");
     char ch;
     int choice=TRUE;//set choice equal true
     headptr=(struct contact *)NULL;//set head pointer equal to null

        while(choice==TRUE)//condition if choice equal 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()//add data
      {
        newptr=(struct contact *)malloc(sizeof (struct contact)); // pointer to a new
                        //  memory allocation

        if (headptr==(struct contact *)NULL)//node is empty?
           {
              headptr=newptr; //first pointer point to first node
              newptr->ptrnext=(struct contact *)NULL; //first node pointer point to null
      }

          else
            {
              newptr->ptrnext=headptr;// new node pointer point to previous first node
              headptr=newptr; // head point to new node,new node become first node

            }
        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) //empty list
          {
             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; //point to next node

             }

             while(currentptr !=(struct contact *)NULL);
     }

        void delete_contact()
        {
           char contact_name_delete[20];


            if (headptr==(struct contact *)NULL)//node is empty?
                {
                  printf("\n\nThe list is empty. Cannot delete!!!\n");//inform the user that the list is empty
                }

            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) //found the location
                {
                 break;
                }
            else
                {
               previousptr=currentptr;//save the previous current address
                    currentptr=currentptr->ptrnext; //point to next node
                }

            }

           if (strncmp(currentptr->contact_name,contact_name_delete,6)==0)
           {
            if ( currentptr==headptr) //number is the first and only node in list
                    {
                    headptr=currentptr ->ptrnext; //head point to NULL
                    free(currentptr);
                        }
            else //delete at the middle of link list
                    {
                    previousptr->ptrnext=currentptr->ptrnext;//previous node point to next node
                    free(currentptr);//destroy  node, free the memory.
                    }
            printf("\nPhone number were deleted!\n");
            }

            else
            printf("\nNumber to be deleted is not in the list!!! ");
               }

          }


        void  search_contact()//The function for searching the contact based on name
         {   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 : ");//user inputs name for searching
                fgets(contact_name_add,sizeof(contact_name_add),stdin);//uses linear search concept


                 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)//condition if head pointer equal to 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;//set current pointer to the head pointer

        do
        {
            printf("\n\n\tName             : %s \n",currentptr->contact_name);
           currentptr=currentptr->ptrnext; //current pointer point to the next node
        }
        while(currentptr!=NULL);//looping stop when current pointer equal to NULL

}




  //Function definition for merge sorting customer according to book code
struct contact *mergesortcontact_name(struct contact *head)
{
    struct contact *head1,*head2,*p1,*p2; //declare variable

    //If the record is empty or only have 1 record, then no need to sort
    if (head==NULL||(head->ptrnext==NULL))return head;

    head1=head;
    head2=head->ptrnext;
    //Loop to determine the middle and end of the list
    while((head2!=NULL) &&(head2->ptrnext!=NULL))
    {
        head=head->ptrnext;
        head2=head2->ptrnext->ptrnext;
    }
    head2=head->ptrnext;
    head->ptrnext=NULL;

//CHAPTER 2 RECURSIVE
    //Divide and Conquer Recursively
    p1=mergesortcontact_name(head1);
    p2=mergesortcontact_name(head2);
    return combinecontact_name(p1,p2);
}

//Function definition for merging the divided list
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;
        //Recursion to merge the divided list
        head3->ptrnext=combinecontact_name(head1->ptrnext,head2);
    }
    else
    {
        head3=head2;
        //Recursion to merge the divided list
        head3->ptrnext=combinecontact_name(head1,head2->ptrnext);
    }
    return head3;
}
Posted
Updated 29-May-14 12:01pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900