I have to write a program that will essentially sort nodes in a linked list. I have 5 functions that need to be written for this assignment and I am stuck on one of them. The function I am having trouble with is swapping two nodes. The header for the function is the following:
void swap (struct lnode** head, struct lnode* n1, struct lnode* n2)
I have gotten this to work properly as long as the two nodes are not next to each other. We have a list.h file provided to us and we are supposed to use two functions evictNode(struct lnode** head, struct lnode* node) and void insertNode (struct lnode** head, struct lnode* prevNode, struct lnode* nodeToBeInserted). These functions handle reassignment of the next and previous pointers as well. I am just not entirely sure on how to swap the nodes if they are next to each other. I have tried drawing it out but have not been able to wrap my mind around it.
Oh by the way, the way I handle the rest of the nodes is with the following code:
evictNode(head, n1);
evictNode(head, n2);
insertNode(head, n1prev, n2);
insertNode(head, n2prev, n1);
EDIT: Attempts
struct lnode* temp = n2;
insertNode(head,n1prev,temp)
evictNode(head, n2)
where struct lnode* n1prev = nodeGetPrev(n1)
there are two functions that return the previous/next pointers
I will do it as follow: