Okay so I want to swap POSITION (not values) of two nodes.
My Program is running with any errors or warnings, but I am not sure if I am swapping position or values.
Here is my sort function:
void sort(struct node **recordsHead,int (*compare_fcn)(struct node*, struct node*))
{
void swap(struct node**, struct node**);
struct node *tmp,*lastPtr = NULL;
int swapped;
do {
swapped = 0;
tmp = *recordsHead;
while (tmp->next_ != lastPtr)
{
if (compare_fcn(tmp, tmp->next_))
{
swap(&tmp, &(tmp->next_));
swapped = 1;
}
tmp = tmp->next_;
}
lastPtr = tmp;
} while (swapped);
}
Here is my Swap Function
void swap(struct node** node1, struct node** node2)
{
student_record *tmp;
tmp = (*node1)->record_;
(*node1)->record_ = (*node2)->record_;
(*node2)->record_ = tmp;
}
Thank you.
Rachel, continuing from the comments, since you appear to be attempting to 'swap-pointers' in a linked-list, you cannot simply swap adjacent pointers in a list without destroying the linkage between the nodes (e.g. the
node->next
pointers will no longer point to the next node in the list). Consider the following:Where
If you simply swap the
a
andb
pointers, where doa->next
andb-next
point?, e.g.Since you haven't swapped (or re-wired) the
next
pointers, they still hold (point to) the original memory locations, e.g.a->next = b;
andb->next = c;
(that's not right, you will skip 'a' completely, and if you did hita
,a->next
would backup tob
and thenb->next
skips toc
...)So if you want to sort by some member value and not swap values, but instead swap pointers, you must "re-wire" each
next
pointer to point to the node with the next ascending (or descending) member value in correct sort order that is different from simply swapping thea
,b
,c
pointer values.Next, you have to consider two distinct cases. A rewiring of the first node, and thus changing the address of the list itself (e.g. the
head
pointer), and then the general case for all other nodes (for a singly-linked-list, circular lists and doubly-linked list will required additional attention to theprev
andlast->first
linkages). The two cases you need to visualize and consider are swaps involving the first node:and your general case:
For cases involving the
head
node (and usingstruct node *iter = *head;
and choosingtmp = iter->next;
for the replacement, you would do something like the following:To handle the general case (in the following
else
clause, you would do something like the following:You would use no swap function in your code at all (you could, but you would need to pass the list address so a comparison for
head
could take place.Putting all the pieces together in a short example and sorting in ascending order on an integer value within each node, you can do something like the following:
Example Use/Output
Memory Leak/Error Check
It is imperative that you use a memory error checking program to insure you do not attempt to write beyond/outside the bounds of your allocated block of memory, attempt to read or base a conditional jump on an uninitialized value, and finally, to confirm that you free all the memory you have allocated.
For Linux
valgrind
is the normal choice. There are similar memory checkers for every platform. They are all simple to use, just run your program through it.Always confirm that you have freed all memory you have allocated and that there are no memory errors.
Look things over and let me know if you have any further questions.