#include <stdio.h>
#include <string.h>
#include <ctype.h>
struct list {
struct list *next;
int ch;
};
void swaptwo ( struct list **pp, struct list *one, struct list *two )
{
struct list *tmp=NULL, **aaa, **bbb;
if (!pp || !*pp || !one || !two || one==two) return ;
for (aaa=bbb=NULL; *pp; pp = &(*pp)->next) {
if ( !aaa && *pp == one) aaa = pp;
else if ( !bbb && *pp == two) bbb = pp;
if (aaa && bbb) break;
}
if (!aaa || !bbb) return;
tmp = *aaa;
*aaa = *bbb;
*bbb = tmp;
tmp = one->next;
one->next = two->next;
two->next = tmp;
}
struct list arr[]
= { {arr+1, 'A'} , {arr+2, 'B'} , {arr+3, 'C'} , {arr+4, 'D'} , {arr+5, 'E'} , {arr+6, 'F'} , {arr+7, 'G'} , {arr+8, 'H'}
, {arr+9, 'I'} , {arr+10, 'J'} , {arr+11, 'K'} , {arr+12, 'L'} , {arr+13, 'M'} , {arr+14, 'N'}, {arr+15, 'O'} , {arr+16, 'P'}
, {arr+17, 'Q'} , {arr+18, 'R'} , {arr+19, 'S'} , {arr+20, 'T'} , {arr+21, 'U'} , {arr+22, 'V'}, {arr+23, 'W'} , {arr+24, 'X'}
, {arr+25, 'Y'} , {NULL, 'Z'} };
int main (void) {
struct list *root = arr, *ptr;
printf( "Swap Q<-->X\n" );
swaptwo ( &root, arr+16, arr+23);
for (ptr=root ; ptr; ptr = ptr->next ) {
printf( "-> %c" , ptr->ch );
}
printf( "\n" );
printf( "Swap B<-->C\n" );
swaptwo ( &root, arr+1, arr+2);
for (ptr=root ; ptr; ptr = ptr->next ) {
printf( "-> %c" , ptr->ch );
}
printf( "\n" );
printf( "Swap A<-->B\n" );
swaptwo ( &root, arr, arr+1);
for (ptr=root ; ptr; ptr = ptr->next ) {
printf( "-> %c" , ptr->ch );
}
printf( "\n" );
return 0;
}