0

I am facing problem in implementation of selection sort for double linked list.

Please some one help me in doing selection sort for double linked list in c.

node struct is:

struct node {
         char name [15];
         node * nextnode;
         node * prevnode;
}node;

I am sorting by name

Code is like this:

I need to sort the list in ascending order of names.

char names[5][15] = {"tom","jack","mike","bernard","leo"};

I have constructed a doubly linked list with above node structure and function below:

int list_add(char *lname)
{
    int i, length;
    struct node *current = NULL;
    struct node *temp = NULL;
    current = head;
    while (current->nextnode != NULL)
    {
        current = current->nextnode;
    }
    temp = malloc(sizeof(struct node));
    current->nextnode = temp;
    strcpy(temp->name,lname);
    temp->prevnode = current;
    temp->nextnode = NULL;

    return 0;
}

int init_list(char names[][15])
{
    int length;
    struct node * current = NULL;
    head = malloc(sizeof(struct node));
    strcpy(head->name,"Head");
    head->nextnode = head->prevnode = NULL;
    for(length = 0; length < 5; length++)
    {
        //printf("names are %s \n", (char *)names[length]);
        list_add(names[length]);
    }

    return length;
}

the above one is simple doubly linked list add logic.

selection sort code flow as follows:

start node = keeps track of element node from which shortest node needs to be searched.

temp1 is assigned start node - reason being to keep start nodes traversal not disturbed by swap

temp node holds shortest node for that traversal.

prev and next node's are used for swap logic.

with the below code I am not getting the sorted list.

int selsort(int length)
{
    int i = 0,j = 0;
    struct node *start = NULL;
    struct node *prev, *next, *temp, *temp1;
    struct node *current = NULL;
    struct node *traversal = NULL;
    prev = next = NULL;
    if (head == NULL)
        return 0;
    start = head;
    printf("selsort called \n");
    for(/*start = head->nextnode*/; start != NULL && i < 6; /*start = start->nextnode*/)
    {
        printf("Iteration number %d \n",i);
        start = head->nextnode;
        j = i;
        while(j)
        {
            start = start->nextnode;
            j--;
        }
        printf("start node %s \n",start->name);
        temp1 = start;
        current = start;
        temp = start;
        while(current != NULL)
        {
            if(strcmp(temp->name, current->name) > 0)
            {
                //ascending logic
                temp = current;
                current = current->nextnode;
            } 
            else 
                current = current->nextnode;
        }
        //printf("Before swap logic \n");
        //swap logic
        if(temp1->prevnode == NULL)
        {
            //swap current with temp
            next = temp1->nextnode;
            temp1->nextnode = temp->nextnode;
            temp1->prevnode = temp->prevnode;
            temp->nextnode->prevnode = temp1;
            temp->prevnode->nextnode = temp1;
            temp->nextnode = next;
            temp->prevnode = NULL;
            next->prevnode = temp;

        }else
        {
            //printf("second swap : %d \n",i);
            if(temp1->nextnode == NULL)
                continue;
            else 
            {
                next = temp1->nextnode;
                prev = temp1->prevnode;
                if (temp->nextnode == NULL)
                {
                    temp1->nextnode = NULL;
                    temp1->prevnode = temp->prevnode;
                    temp->prevnode->nextnode = temp1;
                }else{
                    temp1->nextnode = temp->nextnode;
                    temp1->prevnode = temp->prevnode;
                    temp->nextnode->prevnode = temp1;
                    temp->prevnode->nextnode = temp1;
                }
                temp->prevnode = prev;
                temp->nextnode = next;
                next->prevnode = temp;
                prev->nextnode = temp;
            }
        }
        i++;

        traversal = head;
        while(traversal != NULL)
        {
            printf("after iter %d Names are %s \n",i, traversal->name);
            traversal = traversal->nextnode;
        }
    }
}

The above implementation could not sort the doubly linked list.

I am getting infinite loop out put of:

after iter 4 Names are mike

after iter 4 Names are mike

after iter 4 Names are mike

after iter 4 Names are mike

Please somebody give a fix for my implementation or advice for new implementation(reference link). Thanks in advance.

kzs
  • 1,793
  • 3
  • 24
  • 44
  • 2
    Why do you think it doesn't work? Throwing code up on SO and asking someone else to debug it without detailing the steps *you* have gone through, including problems you've seen, is simply not productive. What happens when you run this? Is *anything* done to the list? Nothing? Does it produce an incorrect order? – WhozCraig Oct 13 '13 at 21:24
  • It might help to work your way through [a similar question](http://stackoverflow.com/questions/9919925/selection-sort-with-linked-lists) (although no accepted answer) or [a similar question with C++](http://stackoverflow.com/questions/16280812/implementing-selection-sort-for-linked-lists) or [a blog post on the subject](http://www.refcode.net/2013/02/sorting-linked-list-with-selection-sort.html) or even [some random code in another language](http://penguin.ewu.edu/cscd300/Topic/ListSorting/selectionSort.html) (beyond that I'm inclined to agree with WhozCraig). – Bernhard Barker Oct 13 '13 at 23:10
  • @WhozCraig @ Dukeling : I have updated my question with enough clarity and explanation. please let me know if i missed anything in my question that is not leading you anywhere to come up with advice/solution for me. – kzs Oct 14 '13 at 09:33
  • @Dukeling, The given reference s are for single linked list. how to debug double linked list. – kzs Oct 17 '13 at 06:49
  • Your code is not compilable, neither as C nor as C++. – Armali Sep 23 '14 at 12:15
  • As you could see, I have pasted the selsort method above. and you can just write any main routine using my selsort method. so what do you mean here, not compiling.. "selsort" method? – kzs Oct 09 '14 at 10:44

0 Answers0