#include #include #include struct list { char data; struct list *prev; struct list *next; }; typedef struct list LISTNODE; typedef LISTNODE *LISTNODEPTR; void insert (LISTNODEPTR *sPtr, char value) { LISTNODEPTR newPtr, previousPtr, currentPtr; newPtr = malloc (sizeof (LISTNODE)); if (newPtr != NULL) { newPtr->data = value; newPtr->next = NULL; newPtr->prev = NULL; previousPtr = NULL; currentPtr = *sPtr; while (currentPtr != NULL) { previousPtr = currentPtr; currentPtr = currentPtr->next; } if (previousPtr == NULL) { newPtr->next = *sPtr; *sPtr = newPtr; } else { previousPtr->next = newPtr; newPtr->prev = previousPtr; newPtr->next = currentPtr; } } else printf ("%c not inserted. No memory avilable.\n", value); } void printList (LISTNODEPTR currentPtr) { if (currentPtr == NULL) printf("List is empty.\n\n"); else { while (currentPtr != NULL) { printf("%c -> ", currentPtr->data); currentPtr = currentPtr->next; } printf ("NULL\n\n"); } } LISTNODEPTR fillList (LISTNODEPTR nodePtr, int nodes_number) { int i; char val; for (i = 0; i < 10; i++) { val = (rand () % 25) + 65; insert (&nodePtr, val); } return nodePtr; } void freeList (LISTNODEPTR list) { if (list != NULL) { freeList (list->next); free (list); } } void reverse_list (LISTNODEPTR list) { int list_length = 1, i; char tmp_data; LISTNODEPTR tmp_begin = list, tmp_end = list; while (tmp_end->next) { list_length++; tmp_end = tmp_end->next; } for (i = 0; i < list_length / 2; i++) { tmp_data = tmp_begin->data; tmp_begin->data = tmp_end->data; tmp_end->data = tmp_data; tmp_begin = tmp_begin->next; tmp_end = tmp_end->prev; } } int main (int argc, const char *argv[]) { LISTNODEPTR list = NULL; list = fillList (list, 10); printf ("Original list:\n"); printList (list); reverse_list (list); printf ("Reversed list:\n"); printList (list); freeList (list); return 0; }