#单链表逆序显示
单链表的逆序显示利用了递归函数
#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
typedef struct node{
datatype data;
struct node * next;
}linknode, *linknode_p;
linknode * create(void){
linknode * L = NULL;
L = (linknode *)malloc(sizeof(linknode));
if(NULL == L){
printf("create error!\n");
return NULL;
}
L->data = 0;
L->next = NULL;
return L;
}
int insert(linknode * L, datatype data){
if(L == NULL){
printf("L is NULL!\n");
return -1;
}
linknode * p = NULL;
p = (linknode *)malloc(sizeof(linknode));
if(p == NULL){
printf("insert error!\n");
return -1;
}
p->data = data;
p->next = L->next;
L ->next = p;
return 0;
}
void show(linknode * L){
if(L == NULL){
printf("show error!\n");
return;
}
linknode * p = L->next;
while(p != NULL){
printf("%d\n", p->data);
p = p->next;
}
}
void reverse(linknode * p){
if( p != NULL ){
reverse(p->next);
printf("%d\n", p->data);
}
}
int main(int argc, const char *argv[])
{
linknode * L = NULL;
L = create();
int i = 0;
for(i=0; i<10; i++){
insert(L, i);
}
printf("show:\n");
show(L);
printf("rese:\n");
reverse(L->next);
return 0;
}