I am using a bubble sort to change the nodes positions. I understand there are a few situations to be aware of if node 1 is at the beginning of the list. If node 2 is at the end of the list. If node1 is at the beginning and node2 is at the end. So I believe the issue is when I am swapping nodes that are neighbors as in node1->next_ = node2; because if I do my regular swap i end up with node2->next_ = node2.
I would like to know if I am on track because I tried something similar to what I wrote and ended up infinitely loop. I think there is something I am not understanding like I am losing a pointer somewhere.
I believe this is correct except for the case of if the 2 nodes are neighbors in the linked list.
edit clarified naming of links. reverted to original.
void swap(struct student_record_node** node1, struct student_record_node** node2)
{
struct student_record_node *p1, *x1, *n1, *p2,*x2,*n2, *temp;
p1 = (*node1)->prev_;
x1 = *node1;
n1 = (*node1)->next_;
p2 = (*node2)->prev_;
x2 = *node2;
n2 = (*node2)->next_;
/* swap next_ */
if (p1 == NULL && n2 == NULL)
{
/* step one swap nodes */
temp = *node1;
*node1 = *node2;
*node2 = temp;
/* step two swap node1 prev to be node2 prev */
(*node2)->prev_ = NULL;
/* step three swap node1 next to be node 2 next */
(*node2)->next_ = n1;
/* step four swap node1 next prev to be node2 next prev */
(*node2)->next_->prev_ = x2;
/* step 5 swap node2 next to be node1 next */
(*node1)->next_ = NULL;
/* step 6 swap node2 prev to be node1 prev */
(*node1)->prev_ = n2;
/* step 7 swap node2 prev next to be node1 prev next */
(*node1)->prev_->next_ = x1;
}
else if (p1 == NULL)
{
/* step one swap nodes */
temp = *node1;
*node1 = *node2;
*node2 = temp;
/* step two swap node1 prev to be node2 prev */
(*node2)->prev_ = NULL;
/* step three swap node1 next to be node 2 next */
(*node2)->next_ = n1;
/* step four swap node1 next prev to be node2 next prev */
(*node2)->next_->prev_ = x2;
/* step 5 swap node2 next to be node1 next */
(*node1)->next_ = n2;
/* step 6 swap node2 prev to be node1 prev */
(*node1)->prev_ = p2;
/* step 7 swap node2 prev next to be node1 prev next */
(*node1)->prev_->next_ = x1;
/* step 8 swap node2 next prev to be node1 next prev */
(*node1)->next_->prev_ = x1;
}
else if(n2 == NULL)
{
/* step one swap nodes */
temp = *node1;
*node1 = *node2;
*node2 = temp;
/* step two swap node1 prev to be node2 prev */
(*node2)->prev_ = p1;
/* step three swap node1 next to be node 2 next */
(*node2)->next_ = n1;
/* step four swap node1 next prev to be node2 next prev */
(*node2)->next_->prev_ = x2;
/* step 5 node1 prev next swapped with node2 prev next */
(*node2)->prev_->next_ = x2;
/* step 6 swap node2 next to be node1 next */
(*node1)->next_ = NULL;
/* step 7 swap node2 prev to be node1 prev */
(*node1)->prev_ = p2;
/* step 8 swap node2 prev next to be node1 prev next */
(*node1)->prev_->next_ = x1;
}
else
{
/* step one swap nodes */
temp = *node1;
*node1 = *node2;
*node2 = temp;
/* step two swap node1 prev to be node2 prev */
(*node2)->prev_ = p1;
/* step three swap node1 next to be node 2 next */
(*node2)->next_ = n1;
/* step four swap node1 next prev to be node2 next prev */
(*node2)->next_->prev_ = x2;
/* step 5 node1 prev next swapped with node2 prev next */
(*node2)->prev_->next_ = x2;
/* step 6 swap node2 next to be node1 next */
(*node1)->next_ = n2;
/* step 7 swap node2 prev to be node1 prev */
(*node1)->prev_ = p2;
/* step 8 swap node2 prev next to be node1 prev next */
(*node1)->prev_->next_ = x1;
/* step 9 swap node2 next prev to be node1 next prev */
(*node1)->next_->prev_ = x1;
}
/* swap surrounding */
}
(*node1)->prev_is NULL then you are absolutely not allowed to access(*node1)->prev_->next_(hint: you do). Of course, if you used a debugger, you would find exactly the line that crashes, and you wouldn't need to ask here.prev_pointers.