Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //list and revers list
- #define CNT 10
- #define STR_LEN 10
- typedef struct member list_t;
- struct member {
- char str[STR_LEN];
- list_t *prev;
- };
- list_t* revers(list_t *old)
- {
- list_t *new_head = (list_t *)malloc(sizeof(list_t));
- new_head->prev = NULL;
- strcpy(new_head->str, old->str);
- if (old->prev != NULL) {
- new_head->prev = revers(old->prev);
- }
- return new_head;
- }
- int main(void)
- {
- int i;
- //init list
- list_t *head = (list_t *)malloc(sizeof(list_t));
- head->prev = NULL;
- scanf("%s", head->str);
- //fill list
- list_t *tmp;
- for (i = 0; i < CNT - 1; i++) {
- tmp = head;
- head = (list_t *)malloc(sizeof(list_t));
- head->prev = tmp;
- scanf("%s", head->str);
- }
- // print list
- printf("list: ");
- list_t *node;
- node = head;
- for (; node != NULL;) {
- printf("%s ", node->str);
- node = node->prev;
- }
- printf("\n\n");
- //reverse list
- list_t *new = revers(head);
- //printf reverse list
- printf("reverse list: ");
- node = new;
- for (; node != NULL;) {
- printf("%s ", node->str);
- node = node->prev;
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement