#include<stdio.h>
#include<stdlib.h>
typedef struct LNode{
int data;
struct LNode* next;
}LNode,*LinkList;
LinkList TailInsert(LinkList L){
L=(LinkList)malloc(sizeof(LNode));
int x;
LNode *s,*p=L;
scanf("%d",&x);
while(x!=99){
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
p->next=s;
p=s;
scanf("%d",&x);
}
p->next=NULL;
return L;
}
LinkList Reverse_2(LinkList L){
LNode *pre ,*p=L->next,*r=p->next;
p->next=NULL; //P作为尾指针
while(r!=NULL){
pre=p;
p=r;
r=r->next;
p->next=pre; //每次循环都指向前一个指针
}
L->next=p; //L做头结点
return L;
}
void main(){
LinkList L;
L=TailInsert(L);
L=Reverse_2(L);
LNode *p=L->next;
while(p){
printf("%d ",p->data);
p=p->next;
}
}
单链表转置之指针反转
最新推荐文章于 2022-10-14 19:59:02 发布