有关链表(输入输出数据,并删除一个数据)的问题,head=p1和p1=head的区别?
程序代码:
#include<stdio.h> #include<stdlib.h> #define LEN sizeof(struct student) struct student *creat(); struct student *del(); int print(); //******************************************// int n; //******************************************// struct student { int num; float score; struct student *next; }; //******************************************// int main() { struct student *p,*stu; int n; stu=creat(); p=stu; print(p); printf("Please input the num to deleat:"); scanf("%d",&n); print(del(p,n)); printf("\n\n"); system("pause"); } //******************************************// struct student *creat() { struct student *head,*p1,*p2; p1=p2=(struct student*) malloc(LEN); printf("input the num: "); scanf("%d",&p1->num); printf("input the score: "); scanf("%f",&p1->score); head=NULL; n=0; while(p1->num!=0) { n++; if(n==1) { head=p1; //head指向p1 } else { p2->next=p1; //p2指向p1 } p2=p1; //除去p1,让p2代替p1 p1=(struct student *)malloc(LEN); //重新输入一个p1 printf("input the num: "); scanf("%d",&p1->num); printf("input the score: "); scanf("%f",&p1->score); } p2->next=NULL; //p1 return head; } //******************************************// int print(struct student *head) { struct student *p; printf("There are %d records\n",n); printf("\t\tnumber\t\tscore\n"); p=head; while(p!=NULL) { printf("\t\t%d\t\t%.2f\n",p->num,p->score); p=p->next; } } //******************************************// struct student *del(struct student *head,int num) { struct student *p1,*p2; if(head==NULL) { printf("\nThis list is null!\n"); goto END; } p1=head; //为什么这里是p1指向head而不是head指向p1 while(p1->num!=num && p1->next!=NULL) { p2=p1; // p1=p1->next; // } if(p1->num==num) { if(p1==head) { head=p1->next; } else { p2->next=p1->next; } printf("\nDeleat NO:%d succesed!\n",num); n=n-1; } else { printf("%d not been found!\n",num); } END: return(head); }