Sorting linked list alphabetically in c [duplicate

2019-09-14 05:43发布

问题:

This question already has an answer here:

  • Sorting a linked list in C 6 answers

I would to ask you, if it is possible to simple sort alphabetically linked list with names? I think that it is possible, but i dont know how. Can you help me with that? I will be very thankful.

"i" pressed should scan new name and add this name to linked list and then to sort this list alphabetically "d" pressed should to display entire sorted list

"k" pressed program ends

I did this with array of struct and it work well, but i dont know how to do same with linked list...

Thank you very much :)

here is code:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct list{
    char name[100];
    struct list *next;
}LIST;

int main()
{
    int i, n, k = 0, v = 0, m = 0, j = 0;
    char str[100], c;
    LIST *p_first = NULL, *p_act = NULL, *p_prev = NULL;

    while((c=getchar())!='k')
    {
        if(c=='i')
        {
            if(k == 0)
            {
                p_first = (LIST *) malloc(sizeof(LIST));  //scan first element of struct
                scanf("%s", p_first->name);
                p_act = p_first;
            }
            else
            {
                p_act->next = (LIST *) malloc(sizeof(LIST));  //scan next element of struct
                p_act = p_act->next;
                scanf("%s", p_act->name);

                //here should be code to sort text alphabetically
        }
        p_act->next = NULL;
        k++;
    }
    else if(c=='d')
    {
        //display all elements of linked list

        for(i = 0; i < k; i++) { 
            if(i == 0)
                p_act = p_first;
            else
                p_act = p_act->next;
            printf("%s\n", p_act->name);
        }
    }
    }
    getchar(); 
    return 0;
}

回答1:

If you want the actual code, you came to the wrong place. But I can give you the basic idea.

Whenever you try to solve a problem like this, try to find the fundamental smaller problems that you need to solve. For example, sorting (of any kind), requires the ability to swap elements and, in most of the cases, to compare elements.

The comparison is pretty intuitive: you need to call strcmp() or similar on pairs of elements. But, you may want to do a function that does this job to keep your code clean.

The swap is a little bit harder. If you analyse the swap procedure, you will see that it implies 2 basic operations: removing an element from the list and inserting a new one at a given position. So, you may want to create 2 functions that implement those operations. I recommend you to impement a function that removes an element after the one you provide and insert after an element you provide (your list being a single-liked list, it is hard to find the element before).

To remove the element after A, just use the following assignment:

a->next=(a->next)->next 

(so A->next points to the element after it's neighbour)

To insert B after A:

B->next = A->next
A->next = B

Now, that you have those 2 operations, it is simple to create a function that swaps 2 elements (actually, I recommend to create a function that swaps 2 elements after those you indicate, so swap(A, B) swaps A->next and B->next). Also, the comparison function should compare elements after elements you indicated.

You could use those procedures I described above with any sorting algorithm, like quicksort or bubblesort to achieve your result.

Note that this is merely the basic idea of an implementation and some indications about the way you should think at a problem like this. I hope it is clear enough.