//单链表逆置
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}Node , *LinkList ;
LinkList CreatList(LinkList L,int *num)//创建链表并统计长度,输入0结束;
{
LinkList p,q;
L = (LinkList)malloc(sizeof(Node));
L -> next = NULL;
q = L;
while(1)
{
p = (LinkList)malloc(sizeof(Node));
scanf("%d",&p -> data);
p -> next = NULL;
if(p -> data == 0)
break;
(*num) ++;
q -> next = p;
q = p;
}
return L;
}
LinkList reverList(LinkList L)//变相的头插法重置链表
{
LinkList p , q;
p = L -> next;
L -> next = NULL;
while(p)
{
q = p -> next;
p -> next = L -> next;
L -> next = p;
p = q;
}
p = L -> next;
while(p)
{
printf("%d ",p -> data);
p = p -> next;
}
return L;
}
int main()
{
LinkList L;
int len = 0;
L = CreatList(L,&len);
printf("the length of your List is:%d \n",len);
reverList(L);
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}Node , *LinkList ;
LinkList CreatList(LinkList L,int *num)//创建链表并统计长度,输入0结束;
{
LinkList p,q;
L = (LinkList)malloc(sizeof(Node));
L -> next = NULL;
q = L;
while(1)
{
p = (LinkList)malloc(sizeof(Node));
scanf("%d",&p -> data);
p -> next = NULL;
if(p -> data == 0)
break;
(*num) ++;
q -> next = p;
q = p;
}
return L;
}
LinkList reverList(LinkList L)//变相的头插法重置链表
{
LinkList p , q;
p = L -> next;
L -> next = NULL;
while(p)
{
q = p -> next;
p -> next = L -> next;
L -> next = p;
p = q;
}
p = L -> next;
while(p)
{
printf("%d ",p -> data);
p = p -> next;
}
return L;
}
int main()
{
LinkList L;
int len = 0;
L = CreatList(L,&len);
printf("the length of your List is:%d \n",len);
reverList(L);
system("pause");
return 0;
}
// 只能说和头插法几乎一样