資源描述:
《《實驗十鏈表操作》word版》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫。
1、實驗十鏈表操作[實驗?zāi)康腯掌握鏈表的定義和使用方法,能熟練進(jìn)行鏈表的各種操作。[實驗內(nèi)容]定義一學(xué)生鏈表,完成建立鏈表、打印鏈表、插入鏈表和刪除鏈表操作。[實驗步驟](1)分別定義四個函數(shù)create(),print(),insert(),del()。(2)在主函數(shù)中調(diào)用上述函數(shù),測試各函數(shù)的功能。源程序:#include#includestructstudent{intno;structstudent*next;};/////////////////////////建立鏈表structstudent*create(){structs
2、tudent*head,*p1,*p2;head=NULL;p1=p2=(structstudent*)malloc(sizeof(structstudent));scanf("%d",&p1->no);while(p1->no!=0){if(head==NULL)head=p1;elsep2->next=p1;p2=p1;p1=(structstudent*)malloc(sizeof(structstudent));scanf("%d",&p1->no);}p2->next=NULL;returnhead;}//////打印鏈表voidprint(structstud
3、ent*head){structstudent*p;p=head;while(p!=NULL){printf("%5d",p->no);p=p->next;}printf("");}////////刪除鏈表structstudent*del(structstudent*head,intnum){structstudent*p1,*p2;if(head==NULL)printf("NULL");else{p1=head;while(p1->no!=num&&p1->next!=NULL){p2=p1;p1=p1->next;}if(p1->no==num){if(p
4、1==head)head=head->next;elsep2->next=p1->next;free(p1);printf("deletesuccess!");}elseprintf("notfound");//////這一塊是對應(yīng)原來的循環(huán)條件}returnhead;}///////用遞歸反向輸出鏈表voidoutput(structstudent*head){if(head->next!=NULL){output(head->next);printf("%5d",head->no);}}/////////用遞歸(recursion)正向輸出鏈表voidoutp
5、ut1(structstudent*head){if(head!=NULL)printf("%5d",head->no);output1(head->next);}main(){structstudent*head,*head1;intnum;head=create();output1(head);//print(head);printf("");printf("");scanf("%d",&num);head=del(head,num);print(head);}