チェーンテーブルを逆順に並べ、チェーンヘッドをチェーンテール、チェーンテールをチェーンヘッドにします.
1792 ワード
#include
#include
#define LEN sizeof(struct student)
int n=0;
struct student{ //
int num;
char name[20];
struct student *next;
};
struct student *creat(){ //
struct student *head,*p1,*p2;
head=0;
p1=p2=(struct student *)malloc(LEN);
printf(" ID , , 0 0 :");
scanf("%d %s",&p1->num,p1->name);
while(p1->num!=0){
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%d %s",&p1->num,p1->name);
}
p2->next=0;
return head;
}
void print(struct student *p){ //
while(p!=0){
printf("%d %s
",p->num,p->name);
p=p->next;
}
}
struct student *paixu(struct student *p){
struct student *head,*p1,*p2,*p3;
head=p1=p2=p;
int k=0;
while(p1!=0) {
k++;
if(p1->next!=0){ //
if(k==1) p1=p1->next; //
else { p3=p1->next; //
p1->next=p2;
p2=p1;
p1=p3;}
}
else { //
head->next=0;
head=p1;
p1->next=p2;
break;
}
}
return head;
}
int main()
{ struct student *head=creat(); //
struct student *head1=paixu(head); //
print(head1); //
return 0;
}