单向链表反转

#include <stdlib.h>

struct List {
    int index;
    struct List *next;
};

void create_list(struct List **list, int len)
{
    *list = (struct List *) malloc(sizeof(struct List));
    (*list)->next = NULL;
    struct List* p;

    while(len)
    {
        p = (struct List*) malloc(sizeof(struct List));
        p->index = len--;
        p->next = (*list)->next;
        (*list)->next = p;
    }
}

void reverse_list(struct List **list)
{
    struct List *next, *prev, *node; 
    prev = NULL;

    node = *list;
    while(node) {
        next = node->next;
        if(next == NULL) *list = node;
        node->next = prev;
        prev = node;
        node = next;
    }
}

void destory_list(struct List *list)
{
    if(list->next) destory_list(list->next);

    free(list);
    list->index = 0;
    list->next = NULL;
}

int main(void)
{
    struct List *data;
    create_list(&data, 20);
    reverse_list(&data);

    destory_list(data);

    return 0;
}

 

东抄抄, 西抄抄, 写了个大概, 玩着玩着都会感觉混淆了.