链表反转不能正常显示
程序代码:
#include<stdio.h> #include<stdlib.h> struct Node{ int num; char name[10]; char sex; float score; Node * next; }; int n=0; struct Node * creat(void) { struct Node *p1,*p2; struct Node *head=NULL; p2=p1=(struct Node*)malloc(sizeof(struct Node)); scanf("%d %s %c %f",&p1->num,p1->name,&p1->sex,&p1->score); while(p1->num!=0) { ++n; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(struct Node*)malloc(sizeof(struct Node)); scanf("%d %s %c %f",&p1->num,p1->name,&p1->sex,&p1->score); } p2->next=NULL; return head; } void print(struct Node*head) { struct Node *p; p=head; if(head!=NULL) { do{ printf("%d\t%s\t%c\t%f\n",p->num,p->name,p->sex,p->score); p=p->next; }while(p!=NULL); } } struct Node reserve(struct Node * head) { struct Node *p,*q; p=NULL; while(head!=NULL) { q=p; p=head; head=head->next; p->next=q; } return *p; } void Free(struct Node *head) { struct Node *ptr; while(head!=NULL) { ptr=head; head=head->next ; free(ptr); } } int main() { struct Node *head; head=creat(); printf("The init:\n"); print(head); printf("反转:\n"); reserve(head); print(head); Free(head); return 0; }