#include "stdio.h"
#include "stdlib.h"
typedef int zlp;
typedef struct Node{
zlp data;
struct Node *next;
}Linklist;
void display(Linklist *a){
Linklist *curr = a->next;
while(curr!=NULL){
printf("%d",curr->data);
curr=curr->next;
}
printf("\n");
}
void display_p(Linklist *a){
Linklist *curr = a;
while(curr!=NULL){
printf("%d",curr->data);
curr=curr->next;
}
printf("\n");
}
Linklist *creat(){
Linklist *head = (Linklist*)malloc(sizeof(Linklist));
Linklist *curr = head;
for(int i=0;i<5;i++){
Linklist *temp = (Linklist*)malloc(sizeof(Linklist));
temp->data=i;
curr->next=temp;
curr=curr->next;
}
curr->next=NULL;
return head;
}
Linklist *fanzhuan(Linklist *a){
Linklist *curr = (Linklist*)malloc(sizeof(Linklist));
Linklist *new_head;
Linklist *temp;
a=a->next;
while(a!=NULL){
temp = a;
a = a->next;
temp->next=new_head;
new_head = temp;
}
curr->next=new_head;
return curr;
}
Linklist *fz(Linklist *head){
if(head == NULL||head->next == NULL){
return 0;
}
Linklist *one = head->next;
Linklist *two = head->next->next;
while(two!=NULL){
one->next=two->next;
two->next=head->next;
head->next=two;
two=one->next;
}
return head;
}
int main(){
Linklist *a = creat();
display(a);
Linklist *b=fanzhuan(a);
display(b);
Linklist *c=fz(b);
display(c);
return 0;
}
单链表的反转
最新推荐文章于 2025-06-14 15:30:38 发布