#include <stdio.h>
#include <malloc.h>
typedef struct Node
{
int data;
struct Node *next;
}*rlink;
rlink create()
{
rlink s=(rlink)malloc(sizeof(struct Node));
if(s==NULL)
return NULL;
else
s->data=0;
s->next=NULL;
return s;
}
rlink insert_rear(rlink L,int element)
{
rlink s= create();
s->data=element;
if(L==NULL)
{
L=s;
}
else{
rlink p=L;
while(p->next!=NULL)
{
p=p->next;
}
p->next=s;
}
return L;
}
rlink ni(rlink L)
{
if(L==NULL)
{
return NULL;
}
else if(L->next==NULL)
{
return L;
} else
{
rlink p=ni(L->next);
L->next->next=L;
L->next=NULL;
return p;
}
}
void output(rlink L)
{
if(L==NULL)
{
puts("link is NULL");
return;
}
rlink p=L;
while (p!=NULL)
{
printf("%d\t",p->data);
p=p->next;
}
puts("");
}
int main() {
rlink L=NULL;
int n,element;
printf("please input n:");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("please input %d element:",i+1);
scanf("%d",&element);
L=insert_rear(L,element);
}
output(L);
L= ni(L);
output(L);
return 0;
}
单链表逆置
于 2023-09-30 16:07:42 首次发布