#include #include typedef struct Node { struct Node *next; char c; }Node; typedef struct List { Node *head; Node *current; }List; void pairwise_flip(List *l) { Node *temp = NULL, *flip_temp = NULL; if(NULL == l || NULL == l->head) { printf("list or list-head is NULL.\n"); return; } l->current = l->head; while (NULL != l->current) { temp = l->current->next; if (NULL != temp) { if(l->current == l->head) { l->head->next = temp->next; temp->next = l->head; l->head = temp; } else { flip_temp = l->head; while(flip_temp->next != l->current) flip_temp = flip_temp->next; l->current->next = temp->next; temp->next = l->current; flip_temp->next = temp; } } else break; l->current = l->current->next; } } void print_list(List *l) { for(l->current = l->head; l->current; l->current = l->current->next) { printf("%c", l->current->c); } printf("\n"); } void free_list(List *l) { l->current = l->head->next; while(1) { free(l->head); l->head = l->current; if(NULL == l->current) break; l->current = l->current->next; } free(l); } int main(int argc, char *argv[]) { List *l = malloc(sizeof(List)); int i = 0, list_size = 5; if (argc == 2) { sscanf(argv[1], "%d", &list_size); } if(list_size <= 0) return 0; l->head = malloc(sizeof(Node)); l->head->c = 'a'; l->head->next = NULL; l->current = l->head; for(i = 1; i < list_size; i++) { l->current->next = malloc(sizeof(Node)); l->current = l->current->next; l->current->c = i+0x61; /* convert i to a char */ l->current->next = NULL; } print_list(l); pairwise_flip(l); print_list(l); free_list(l); return 0; }