#include<stdio.h> #include<stdlib.h> #include<conio.h> /*定義結(jié)構(gòu)體*/ struct student { int num; float score; struct student *next; }; /*創(chuàng)建一個(gè)只有頭結(jié)點(diǎn)的空鏈表*/ struct student *create_head() { struct student *head; head=(struct student*)malloc(sizeof (struct student) ); if(head==NULL) //小心別漏這個(gè) { printf("申請(qǐng)頭結(jié)點(diǎn)失敗!\n"); return NULL; } head->num = 0; head->score = 0.0; head->next=NULL; return head; } // 將s指向的結(jié)點(diǎn)插入鏈表,使鏈表保持升序,并返回頭結(jié)點(diǎn) struct student *insert(struct student *head,struct student *s) { struct student *p=head; while(p->next!=NULL && s->score > p->next->score)//特別注意&&左右不能寫(xiě)反,若s最大,最后p->next=NULL,p->next->score運(yùn)行出錯(cuò) p=p->next; if(p->next==NULL) //s->score最大的情況 //其實(shí)兩種情況可以并在一塊寫(xiě) { p->next=s; //連接結(jié)點(diǎn) s->next=NULL; //p->next就等于NULL } else { s->next=p->next; p->next=s; //連接結(jié)點(diǎn) } return head ; } /*查找符合條件的結(jié)點(diǎn),并返回指向該結(jié)點(diǎn)的指針*/ struct student *search(struct student *head) { struct student *p=head->next; int num; printf("請(qǐng)輸入要查找學(xué)生的學(xué)號(hào):\n"); scanf("%d",&num); while(p!=NULL&&p->num!=num) //特別注意兩條件不能寫(xiě)反,若寫(xiě)反最后p指向NULL時(shí)p->num找不到 運(yùn)行出錯(cuò) p=p->next; if(p==NULL) //特別注意兩個(gè)if不能調(diào)換,若調(diào)換最后p指向NULL時(shí)p->num運(yùn)行出錯(cuò) { printf("找不到符合條件的結(jié)點(diǎn)!!!\n"); return NULL; //查找不到返回空指針 } if(p->num==num) { printf("找到符合條件的結(jié)點(diǎn)\n該結(jié)點(diǎn)為%d\t%.1f\n",p->num,p->score); return p; //返回查找到的指針 } } /*輸出鏈表各結(jié)點(diǎn)的值,也稱(chēng)對(duì)鏈表的遍歷*/ void print(struct student *head) { struct student *p; printf(" 鏈表如下: \n"); p=head->next; while(p!=NULL) { printf("%d\t%.1f\n",p->num,p->score); p=p->next; } } /*釋放鏈表*/ void free_list(struct student *head) { struct student *p=head ; printf("釋放鏈表:\n"); while(p!=NULL) { head=head->next; free(p); p=head; } printf("釋放鏈表成功!\n"); } /*刪除鏈表中值為num的結(jié)點(diǎn),并返回鏈表的首指針*/ struct student *delete_note(struct student *head,int num_x) { struct student *p1=head->next , *p2=head ; while(p1!=NULL&&p1->num!=num_x) //特別注意&&左右條件不能調(diào)換,若調(diào)換如果p1指向NULL時(shí)p1->num運(yùn)行出錯(cuò) { p2=p1; p1=p1->next; } if(p1==NULL) //特別注意兩個(gè)if不能調(diào)換,若調(diào)換如果p1指向NULL時(shí),p1->num運(yùn)行出錯(cuò) { printf("找不到符合刪除要求的結(jié)點(diǎn)!!!\n"); return NULL; } if(p1->num==num_x) { p2->next=p1->next; free(p1); printf("結(jié)點(diǎn)刪除成功!\n"); } return head; } /*完整的有頭結(jié)點(diǎn)鏈表操作程序*/ void main() { struct student *p , *head ; char c; int num ; float score ; printf("有頭結(jié)點(diǎn)鏈表操作程序:\n"); head=create_head(); while(1) { printf("I:插入結(jié)點(diǎn)(自動(dòng)升序) P:輸出鏈表 S:查找結(jié)點(diǎn) D:刪除結(jié)點(diǎn) E:釋放鏈表并退出程序! \n"); c=getch(); switch(c) { case'I': case'i': printf("請(qǐng)分別輸入要插入學(xué)生的學(xué)號(hào)和分?jǐn)?shù):\n"); scanf("%d%f",&num,&score); p=(struct student*)malloc( sizeof(struct student) ); if(p==NULL) { printf("申請(qǐng)?jiān)摻Y(jié)點(diǎn)失敗!!!\n"); exit (0) ; } p->num=num; p->score=score; //給p賦值 insert(head,p); printf("插入成功!\n"); break; case'p': print(head); break; case's': search(head); break; case'd': printf("請(qǐng)輸入要?jiǎng)h除的學(xué)生的學(xué)號(hào):\n"); scanf("%d",&num); delete_note(head,num); break; case'e': free_list(head); exit (0); } } }
|
|