结构体定义
typedef struct numbers
{
int number;
struct numbers *next;
}ints;
链表创建
ints *chain(){
ints *head,*a = NULL,*b;
while(1){
if(NULL == (b = malloc(sizeof(ints)))){
printf("内存申请失败");
exit(1);
}
printf("输入一个数:");
scanf("%d",b);
if(0 == b->number)break;
b -> next = NULL;
if(NULL == a) {
a = b;
head = a;
}else{
a -> next = b;
a = b;
}
}
return head;
}
链表反转循环实现
ints *invert(ints *head)
{
ints *x;
ints *a,*b;
x = head,b = NULL;
while(x != NULL){
a = x;
x = x -> next;
a -> next = b;
b = a;
}
head -> next = a;
return head;
}
链表反转递归实现(调用形式:head = invert(head,NULL);)
ints *invert(ints *next, ints *up){
ints *head = next;
if(NULL != next -> next){
head = invert(next -> next, next);
}
next -> next = up;
return head;
}