I've created the following nodes in my list:
Code:
addElement(&start, "B", 1);
addElement(&start, "D", 2);
addElement(&start, "A", 3);
addElement(&start, "C", 4);
next i have a loop which runs it x amount of nodes. It compares the next element with the current element
i.e. : if ( B is greater than A)
i then pass a reference to the current node(petId) i.e. B and it swaps it to AB
Code:
for(i=0; i <= 5; i++){
while (start && start->next != NULL)
{
if (strcmp(start->next->petName, start->petName) <0){
current=swap(current,start->petId);
}
start = start->next;
}
start = head;
}
i then run my sort function:
Code:
petNode* swap(petNode *current, int petId)
{
petNode *temp;
petNode *tmp;
temp=current;
if(current->petId==petId)
{
tmp=current->next;
current->next=current->next->next;
tmp->next=current;
current=tmp;
return(current);
}
else
{
while(temp->next->next!=NULL)
{
if(temp->next->petId==petId)
{
tmp=temp->next->next;
temp->next->next=temp->next->next->next;
tmp->next=temp->next;
temp->next=tmp;
break;
}
temp=temp->next;
}
return(current);
}
}
The original list
DBAC becomes BACD
and i can't figure it out. it works for some combos like:
BDAC becomes ABCD... any ideas??
i have tried printing the list after every swap the occurs and get:
BDAC
BDAC
BADC
BACD
i'm stuck :|