3、>#include#defineNULL0#defineLENsizeof(structstudent)structstudent{longnum;/*學(xué)號*/floatscore;/*分?jǐn)?shù),其他信息可以繼續(xù)在下面增加字段*/structstudent*next;/*指向下一節(jié)點(diǎn)的指針*/};intn;/*節(jié)點(diǎn)總數(shù)*//*==========================功能:創(chuàng)建節(jié)點(diǎn)返回:指向鏈表表頭的指針==========================*/structstudent*Create(){structst
4、udent*head;/*頭節(jié)點(diǎn)*/structstudent*p1=NULL;/*p1保存創(chuàng)建的新節(jié)點(diǎn)的地址*/structstudent*p2=NULL;/*p2保存原鏈表最后一個(gè)節(jié)點(diǎn)的地址*/n=0;/*創(chuàng)建前鏈表的節(jié)點(diǎn)總數(shù)為0:空鏈表*/p1=(structstudent*)malloc(LEN);/*開辟一個(gè)新節(jié)點(diǎn)*/p2=p1;/*如果節(jié)點(diǎn)開辟成功,則p2先把它的指針保存下來以備后用*/if(p1==NULL)/*節(jié)點(diǎn)開辟不成功*/{printf("Cann'tcreateit,tryitagaininamoment!");
5、returnNULL;}else/*節(jié)點(diǎn)開辟成功*/{head=NULL;/*開始head指向NULL*/printf("Pleaseinput%dnode--num,score:",n+1);scanf("%ld,%f",&(p1->num),&(p1->score));/*錄入數(shù)據(jù)*/}while(p1->num!=0)/*只要學(xué)號不為0,就繼續(xù)錄入下一個(gè)節(jié)點(diǎn)*/{n+=1;/*節(jié)點(diǎn)總數(shù)增加1個(gè)*/if(n==1)/*如果節(jié)點(diǎn)總數(shù)是1,則head指向剛創(chuàng)建的節(jié)點(diǎn)p1*/{head=p1;/*注意:此時(shí)的p2就是p1,也就是p1->next指
6、向NULL。這樣寫目的是與下面else保持一致。*/p2->next=NULL;}else{p2->next=p1;/*指向上次下面剛開辟的節(jié)點(diǎn)*/}p2=p1;/*把p1的地址給p2保留,然后p1去產(chǎn)生新節(jié)點(diǎn)*/p1=(structstudent*)malloc(LEN);printf("Pleaseinput%dnode--num,score:",n+1);scanf("%ld,%f",&(p1->num),&(p1->score));}p2->next=NULL;/*此句就是根據(jù)單向鏈表的最后一個(gè)節(jié)點(diǎn)要指向NULL*/free(p1);/
7、*釋放p1。用malloc()、calloc()的變量都要free()*/p1=NULL;/*特別不要忘記把釋放的變量清空置為NULL,否則就變成"野指針",即地址不確定的指針。*/returnhead;/*返回創(chuàng)建鏈表的頭指針*/}/*===========================功能:輸出節(jié)點(diǎn)返回:void===========================*/voidPrint(structstudent*head){structstudent*p;printf("Now,These%drecordsare:",n);p
8、=head;if(head!=NULL)/*只要不是空鏈表,就輸出鏈表中所有節(jié)點(diǎn)*/{printf("headis%o",head);/*輸出頭指