Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct _list_elem
- {
- char data;
- struct _list_elem* next;
- } list_elem;
- list_elem* invert_worker(list_elem* elem, list_elem** base)
- {
- list_elem* nelem;
- if(elem->next)
- {
- nelem = invert_worker(elem->next, base);
- nelem->next = elem;
- }
- else
- {
- *base = elem;
- }
- return elem;
- }
- void invert(list_elem** base)
- {
- list_elem* tmp;
- tmp = *base;
- invert_worker(*base, &tmp);
- (*base)->next = NULL;
- *base = tmp;
- }
- int main(int argc, char** argv)
- {
- list_elem *list, *p, *pl;
- char j;
- list = (list_elem*)malloc(sizeof(list_elem));
- memset(list, 0, sizeof(list_elem));
- pl = list;
- for(j = 'a'; j <= 'z'; j++)
- {
- p = (list_elem*)malloc(sizeof(list_elem));
- memset(p, 0, sizeof(list_elem));
- p->data = j;
- pl->next = p;
- pl = pl->next;
- }
- pl->next = NULL;
- invert(&list);
- pl = list;
- while(pl)
- {
- printf("%1s\n", &pl->data);
- pl = pl->next;
- }
- pl = list;
- while(pl)
- {
- p = pl->next;
- free((void*)p);
- pl = p;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment