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.