结构体定义
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;
}

本文详细介绍了链表的基本操作,包括如何创建链表以及如何通过循环和递归来实现链表的反转。通过实例代码展示了内存申请、输入处理、链表节点创建与连接的过程,以及链表反转的具体实现方法。
1万+

被折叠的 条评论
为什么被折叠?



