Hi there,

I made a linked list program to add, delete, add in the middle and delete from anywhere.
I'm searching to make a function to sort the string data that I have in lexicographic order.

Here's my struct:

struct node
{
    char data[100];
    struct node *previous;  // Points to the previous node
    struct node *next;   // Points out to the next node
}*head, *last;

Any help?

Recommended Answers

All 5 Replies

Consider a simple linked list insertion sort. Instead of a direct comparison with relational operators, look at using strcmp on the data member instead and you'll be good to go.

Okay so i have an example here that I found on your very useful link.
I'm just stuck on what to do on the: struct jsw_node result = {0};
what do i need to change this for?

 1 void jsw_insertion ( struct jsw_list *list )
 2 {
 3   struct jsw_node result = {0};
 4   struct jsw_node *i, *step, *next;
 5 
 6   for ( step = list->head; step != NULL; step = next ) {
 7     next = step->next;
 8 
 9     for ( i = &result; i->next != NULL; i = i->next ) {
10       if ( i->next->data > step->data )
11         break;
12     }
13 
14     step->next = i->next;
15     i->next = step;
16   }
17 
18   list->head = result.next;
19 }

I think all you have to do is change line 10 to use strcmp()

can you post a code as an axample?
I'm not sure of how to do it.

I'm a newbie in C sorry i'm asking a lot

if( strcmp(  i->next->data, step->data ) > 0)
{


}

I'm a newbie in C sorry i'm asking a lot

Not a problem -- we were all in your shoes at one time or another.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.