sorting linked list alphabitecally

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tinased
    New Member
    • Dec 2014
    • 1

    #1

    sorting linked list alphabitecally

    hello. I have to use a function in my phonebook program to sort the names alphabitcally
    here is what i have done so far.but what's wrong with it?


    void showAllContacts () {
    char t[40];
    list *head;
    if(head == NULL) {
    puts("There are no contacts to display!");
    } else {
    printf("\nAll contacts:\n");
    /* Set the temp pointer to point to the first record in the
    list. */
    list *temp = head;
    /* Loop through all the nodes in the linked list and print
    out all the corresponding name and phone records. */
    while(temp!= NULL){
    if(strcmp(temp->name,temp->next->name) > 0) {
    strcpy(t, temp->name);
    strcpy(temp->name, temp->next->name);
    strcpy(temp->next->name, t);
    cout<<temp->name<<temp->tel;
    }
    //else {
    //printf("%-20s %-9s\n", temp->name,temp->tel);
    //}
    temp=temp->next;
    }
    }
    }
  • Ajay Bhalala
    New Member
    • Nov 2014
    • 119

    #2
    You can write the code between the "Code Tags" for better view for understanding. If you use the code tags, your code area looks like below

    Code:
    void showAllContacts() { 
    char t[40];
    list *head;
    if(head == NULL) {
    puts("There are no contacts to display!");
    } else {
    printf("\nAll contacts:\n");
    /* Set the temp pointer to point to the first record in the
    list. */
    list *temp = head;
    /* Loop through all the nodes in the linked list and print
    out all the corresponding name and phone records. */
    while(temp!= NULL){
    if(strcmp(temp->name,temp->next->name) > 0) {
    strcpy(t, temp->name);
    strcpy(temp->name, temp->next->name);
    strcpy(temp->next->name, t);
    cout<<temp->name<<temp->tel;
    }
    //else {
    //printf("%-20s %-9s\n", temp->name,temp->tel);
    //}
    temp=temp->next;
    }
    }
    }

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      I suggest reading up on a bubble sort.

      Essentially you need a nested while loop. The outer loop starts with the first node and drops into the nested loop which start with the node from the outer loop and compares the string it has to every other node in the list. If the other node string is less, then the other node is swapped with the node in the outer loop. When the inner loop reaches the end of the list, the smallest string is in the node in the outer loop. Then the outer loop advances one node and the inner loop repeats.
      Last edited by weaknessforcats; Dec 29 '14, 04:30 PM.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        There are many sorting algorithms that differ in coding complexity, worst-case execution time, typical execution time, best-case execution time, etc.

        Bubble Sort is useful for illustrating certain program constructs, but it is not a particularly efficient search algorithm. I suggest you take a look at Insertion Sort.

        (Not that insertion sort is the best possible sort either.)

        Comment

        Working...