sorting by name or number

This is a discussion on sorting by name or number within the C Programming forums, part of the General Programming Boards category; Could I get some pointers on a good way to add code to sort records that are read from a ...

  1. #1
    Unregistered
    Guest

    sorting by name or number

    Could I get some pointers on a good way to add code to sort records that are read from a file, contact.txt, then print them out on the screen and also rewrite them to file in sorted order by either name or phone number. the user is provided an interface to choose. this is my insert function, i cannot figure how to do call these records to another function, sort_records(). any ideas? thanks
    Thanks for any help

    void add_to_list (void)
    /* Add a new name to our address book */
    {
    ADDRESS *new_name;

    FILE *f;
    f=fopen("contact.txt","a+");
    new_name= malloc (sizeof (ADDRESS));
    if (new_name == NULL) {
    printf ("Out of memory!\n");
    exit (-1);
    }

    /* Get input for the new item */
    printf("Name: ");
    fgets(new_name->name, MAXLEN, stdin);
    printf ("Address: ");
    fgets (new_name->address, MAXLEN, stdin);
    printf ("Telephone: ");
    fgets (new_name->phone, MAXLEN, stdin);
    printf ("City: ");
    fgets (new_name->city, MAXLEN, stdin);
    printf ("State: ");
    fgets (new_name->state, MAXLEN, stdin);
    printf ("Email: ");
    fgets (new_name->email, MAXLEN, stdin);
    printf("Contact Added\n");
    fprintf(f,"Name: %s\n",new_name->name);
    fprintf(f,"Address: %s\n",new_name->address);
    fprintf(f,"Telephone: %s\n",new_name->phone);
    fprintf(f,"City: %s\n",new_name->city);
    fprintf(f,"State: %s\n",new_name->state);
    fprintf(f,"Email: %s\n",new_name->email);
    if(!f)
    fclose(f);
    new_name->next= hol;
    hol= new_name;


    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Since it's a linear linked list, you're probably going to end up with a bubble sort...
    Code:
    ADDRESS * p;
    int sorted = 0;
    for (; sorted == 0; )
    {
     sorted = 1;
     for (p = hol; p -> next != NULL; p = p -> next)
     {
      if (compare (p, p -> next))
      {
       swap (p, p -> next);
       sorted = 0;
      }
     }
    }
    So now we just need to fill in the blanks for compare () and swap ()

    compare should be easy to implement. It just returns 1 if the first node pointed to should be after the second node, and 0 otherwise. The logic of that function will depend on by which value you sort the nodes.

    swap is a little more complicated. Basically, it's supposed to just swap the two nodes.. there are two ways to do this...

    1) Swap all the values in *p with all the values in *(p -> next), except for their -> next fields

    2) Make it so that the node before p points to p-> next, p -> next points to p, and p points to the node that was after p -> next
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Unregistered
    Guest
    how can i get the sort function to read from the file, contact.txt? thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Program that requests input of an integer number
    By theejuice in forum C Programming
    Replies: 6
    Last Post: 04-30-2008, 02:18 AM
  3. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  4. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  5. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21