I am passing the head of my linked list to the following function. This function is supposed to work like a bubble sort, swapping the nodes when necessary. After the function is called and printed nothing has changed.
The swap function:
void bubble_sort_title(Box temp, int nodes, FILE *outputp)
{
Box head = temp, p1 = NULL, p2 = NULL, p3 = NULL, p4 = NULL;
int i, j;
//int nodes is used as the nodes size
for(i = 0; i < nodes; i++) {
temp = head;
for(j=0; j < nodes; j++) {
if(strcmp(temp->title, temp->next->title) > 0) {
if (temp->prev == NULL) { //if head is to be swapped
p1 = temp;
p2 = temp->next;
p3 = p2->next;
p1->next = p3;
p2->next = p1;
p3->prev = p1;
p1->prev = p2;
p2->prev = NULL;
head = p2;
temp = head;
temp = temp->next;
} else if (temp->next->next == NULL) { //if tail is to be swapped
p1 = temp;
p2 = temp->prev;
p3 = temp->next;
p2->next = p3;
p3->next = p1;
p1->next = NULL;
p1->prev = p3;
p3->prev = p2;
temp = temp->next;
} else if (temp->prev != NULL && temp->next->next != NULL) { //if middle is to be swapped
p1 = temp->prev;
p2 = temp;
p3 = temp->next;
p4 = p3->next;
p1->next = p3;
p4->prev = p2;
p3->next = p2;
p2->prev = p3;
p3->prev = p1;
p2->next = p4;
temp = temp->next;
}
}
}
}
}
What is wrong with me logic?


Sign In
Create Account


Back to top









