#include "list.h" ////////////////////////////////////////////////////////////////////// // // swapnode() - swap the position of two given nodes in a list, being // sure to appropriately adjust any and all pertinent // pointers to ensure list integrity following the // operation (including potential changes to the list's // own first and last pointers, along with connectivity // between the nodes from the start to end of the list). // // note: you are NOT to just adjust node info values. You must // disconnect the identified nodes from the list and // reconnect them- the node is our unit of transaction, // NOT the data it contains. // // behavior: on a NULL list- do nothing: return NULL // on an EMPTY list- do nothing: return list unchanged // on a NULL/invalid nodes to swap- do nothing: return // the list unchanged // // note: where applicable, make use of the getpos() and // setpos() functions to aid you in accomplishing this // task. Don't needlessly reinvent the wheel. // // as with the other functions, you may use no more // than one return() statement per function. // List *swapnode(List *myList, Node *item1, Node *item2) { if(myList!=NULL) { if(myList->first!=NULL) { if(item1!=NULL&&item2!=NULL) { if(item1!=item2) { int pos; //declare variables, because swaps needed. Node *tmp=NULL; if(item1==myList->first) { if(item2==myList->last)//swapping first and last { pos=getpos(myList,item2); //mv last to prev myList->last=setpos(myList,(pos-1)); myList->first=myList->first->after; item1->after=NULL; //mv first to after myList->last->after=item1; //mv Item1 to last myList->last=item1; //adjust last to actual last item2->after=myList->first;//put item 2 before first myList->first=item2;//adjust first to actual first } else //swapping first and where ever in-between { myList->first=myList->first->after; item1->after=item2->after; if(item2!=myList->first)//if item 2 didn't become first { pos=getpos(myList,item2); /*disconnet I2, connect I1*/ tmp=setpos(myList,(pos-1)); tmp->after=item1; item2->after=myList->first; //re-connect item 1 myList->first=item2; //adjust first to actual first } else //if item 2 became first myList->first->after=item1; //connect item completely } } else if(item2==myList->first) //inverse of if item 2 was first { if(item1==myList->last) { pos=getpos(myList,item1); myList->last=setpos(myList,(pos-1)); myList->first=myList->first->after; item2->after=NULL; myList->last->after=item2; myList->last=item2; item1->after=myList->first; myList->first=item1; } else { myList->first=myList->first->after; item2->after=item1->after; if(item2!=myList->first) { pos=getpos(myList,item1); tmp=setpos(myList,(pos-1)); tmp->after=item2; item1->after=myList->first; myList->first=item1; } else myList->first->after=item2; } } else if(item1==myList->last) //final checks for if they are first or last, checking if a single items last & { //none are first pos=getpos(myList,item1); myList->last=setpos(myList,pos-1); item1->after=item2->after; myList->last->after=NULL; //moving last back 1 and pointing item 1 to what item 2 points to pos=getpos(myList,item2); tmp=setpos(myList,pos-1); tmp->after=item1; //dis-connecting item 2 and totally connecting item 1 myList->last->after=item2; myList->last=item2; //moving item 2 to last pos and re-adjusting last pointer } else if(item2==myList->last) //inverse of last check { pos=getpos(myList,item2); myList->last=setpos(myList,pos-1); item2->after=item1->after; myList->last->after=NULL; pos=getpos(myList,item1); tmp=setpos(myList,pos-1); tmp->after=item2; myList->last->after=item1; myList->last=item1; } else //IF neither nodes are first nor last { pos=getpos(myList,item1); tmp=setpos(myList,(pos-1)); tmp->after=item1->after; //disconnect item1 item1->after=item2->after; pos=getpos(myList,item2); Node *tmp2=setpos(myList,pos-1); //because tmp still holds place for item 1 tmp2->after=item1; //re-connecting item1 whilst dis-connecting 2 item2->after=tmp->after; tmp->after=item2; //re-connecting item to where item 1 was } } } } } return(myList); }