![數(shù)據(jù)結(jié)構(gòu)和算法實(shí)驗(yàn)指導(dǎo)書(shū)模板_第1頁(yè)](http://file4.renrendoc.com/view/c7cb49c5a65f5809ef02a1bf0bd13a2f/c7cb49c5a65f5809ef02a1bf0bd13a2f1.gif)
![數(shù)據(jù)結(jié)構(gòu)和算法實(shí)驗(yàn)指導(dǎo)書(shū)模板_第2頁(yè)](http://file4.renrendoc.com/view/c7cb49c5a65f5809ef02a1bf0bd13a2f/c7cb49c5a65f5809ef02a1bf0bd13a2f2.gif)
![數(shù)據(jù)結(jié)構(gòu)和算法實(shí)驗(yàn)指導(dǎo)書(shū)模板_第3頁(yè)](http://file4.renrendoc.com/view/c7cb49c5a65f5809ef02a1bf0bd13a2f/c7cb49c5a65f5809ef02a1bf0bd13a2f3.gif)
![數(shù)據(jù)結(jié)構(gòu)和算法實(shí)驗(yàn)指導(dǎo)書(shū)模板_第4頁(yè)](http://file4.renrendoc.com/view/c7cb49c5a65f5809ef02a1bf0bd13a2f/c7cb49c5a65f5809ef02a1bf0bd13a2f4.gif)
![數(shù)據(jù)結(jié)構(gòu)和算法實(shí)驗(yàn)指導(dǎo)書(shū)模板_第5頁(yè)](http://file4.renrendoc.com/view/c7cb49c5a65f5809ef02a1bf0bd13a2f/c7cb49c5a65f5809ef02a1bf0bd13a2f5.gif)
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
...wd......wd......wd...《數(shù)據(jù)構(gòu)造與算法》實(shí)驗(yàn)報(bào)告綦娜娜編哈爾濱理工大學(xué)榮成學(xué)院實(shí)驗(yàn)一順序表的實(shí)現(xiàn)和應(yīng)用一、實(shí)驗(yàn)?zāi)康?、掌握順序表的定義;2、掌握順序表的根本操作,如查找、插入、刪除及排序等。二、實(shí)驗(yàn)內(nèi)容1、編寫(xiě)函數(shù),實(shí)現(xiàn)在順序表中查找值為x的元素的位置的簡(jiǎn)單順序查找算法,編寫(xiě)主函數(shù)驗(yàn)證此算法,并分析算法的時(shí)間復(fù)雜度2、編寫(xiě)函數(shù),實(shí)現(xiàn)在順序表中刪除第i個(gè)位置上的元素,編寫(xiě)主函數(shù)驗(yàn)證此算法,并分析算法的時(shí)間復(fù)雜度3、編寫(xiě)函數(shù),實(shí)現(xiàn)在順序表中第i個(gè)位置上插入值為x的元素,編寫(xiě)主函數(shù)驗(yàn)證此算法,并分析算法的時(shí)間復(fù)雜度4、編寫(xiě)函數(shù),實(shí)現(xiàn)在順序表中將所有偶數(shù)排在所有奇數(shù)前面,編寫(xiě)主函數(shù)驗(yàn)證此算法,并分析算法的時(shí)間復(fù)雜度三、實(shí)驗(yàn)提示1、#include<stdio.h>#defineMAXSIZE20typedefstruct{ intdata[MAXSIZE]; intlast;}list;/*編寫(xiě)函數(shù),實(shí)現(xiàn)在順序表中查找值為x的元素的位置的簡(jiǎn)單順序查找算法,編寫(xiě)主函數(shù)驗(yàn)證此算法,并分析算法的時(shí)間復(fù)雜度*/intlocate(list*l,intx){//代碼 inti; for(i=0;i<l->last;i++) if(l->data[i]==x) returni+1; return-1;}main(){ listb; intx,i,p; b.last=10; for(i=0;i<b.last;i++) b.data[i]=i+2; printf("請(qǐng)輸入x的值:"); scanf("%d",&x); p=locate(&b,x); if(p==-1) printf("no!"); else printf("position=%d\n",p);}時(shí)間復(fù)雜度T(n)=O(n);2、#include<stdio.h>#defineMAXSIZE20typedefstruct{ intdata[MAXSIZE]; intlast;}list;/*編寫(xiě)函數(shù),實(shí)現(xiàn)在順序表中刪除第i個(gè)位置上的元素,編寫(xiě)主函數(shù)驗(yàn)證此算法,并分析算法的時(shí)間復(fù)雜度*/intdelete(list*l,inti){ intj,k,p;//定義一個(gè)用來(lái)保存被刪原素; if(i>=0&&i<l->last) //只承受有效輸入 { for(j=0;j<l->last;j++)//遍歷數(shù)組 if(j==i-1) //匹配 { p=l->data[j]; //保存被刪原素; for(k=j;k<l->last;k++) //前進(jìn)一位; { l->data[k]=l->data[k+1]; } break;//退出循環(huán) } l->last=l->last-1; returnp;//對(duì)于此題來(lái)說(shuō)可以輸出p; } return0;}main(){ listb; intx,i; b.last=10; for(i=0;i<b.last;i++) b.data[i]=i+2; printf("請(qǐng)輸入x的值:"); scanf("%d",&x); if(delete(&b,x)!=0) { for(i=0;i<b.last;i++) printf("%3d",b.data[i]); } else printf("Error!");}//時(shí)間復(fù)雜度T(n)=O(n);3、#include<stdio.h>#defineMAXSIZE20typedefstruct{ intdata[MAXSIZE]; intlast;}list;/*編寫(xiě)函數(shù),實(shí)現(xiàn)在順序表中第i個(gè)位置上插入值為x的元素,編寫(xiě)主函數(shù)驗(yàn)證此算法,并分析算法的時(shí)間復(fù)雜度*/intinsert(list*l,intx,inti){ intj,k; if(i<=l->last+1&&i>0) { if(i==l->last+1) //特殊值last+1要插入到整個(gè)數(shù)組之后 { l->data[l->last]=x; } else { for(j=0;j<l->last;j++) { if(j==i-1) //匹配 { for(k=l->last;k>j;k--) //將所選插入位置之后原素后移 { l->data[k]=l->data[k-1]; } l->data[j]=x; //把x賦值給所選位置 break; } } } l->last=l->last+1; //數(shù)值長(zhǎng)度加一 return1; } return0;//無(wú)效位置}main(){ listb; intx,i; b.last=10; for(i=0;i<b.last;i++) b.data[i]=i+2; printf("請(qǐng)輸入x的值:"); scanf("%d",&x); if(insert(&b,66,x)!=0) { for(i=0;i<b.last;i++) printf("%3d",b.data[i]); } else printf("Error!");}//時(shí)間復(fù)雜度T(n)=O(n);4、#include<stdio.h>#defineMAXSIZE20typedefstruct{ intdata[MAXSIZE]; intlast;}list;/*編寫(xiě)函數(shù),實(shí)現(xiàn)在順序表中將所有偶數(shù)排在所有奇數(shù)前面,編寫(xiě)主函數(shù)驗(yàn)證此算法,并分析算法的時(shí)間復(fù)雜度*/voidfun(list*l){ //這個(gè)代碼有點(diǎn)晦澀,但空間時(shí)間復(fù)雜度是雞兒低 inti,ou=0,temp; //i計(jì)數(shù),ou代表偶數(shù)個(gè)數(shù) for(i=0;i<l->last;i++) //循環(huán) { if(l->data[i]%2==0) //判斷是不是偶數(shù),如果是偶數(shù)的話和當(dāng)前第ou個(gè)位置的原素交換位置 { temp=l->data[ou]; l->data[ou]=l->data[i]; l->data[i]=temp; ou+=1; //偶數(shù)個(gè)數(shù)加一 } } printf("當(dāng)前數(shù)組中偶數(shù)有%d個(gè),奇數(shù)有%d個(gè):\n",ou,l->last-ou);}main(){ listb; inti=0,m=0; b.last=10; printf("請(qǐng)輸入數(shù)組元素的值:\n"); for(i=0;i<b.last;i++) { printf("b.data[%d]=",i); scanf("%d",&b.data[i]); } fun(&b); for(i=0;i<b.last;i++) printf("%3d",b.data[i]);}//時(shí)間復(fù)雜度為T(mén)(n)=O(n);四、實(shí)驗(yàn)報(bào)告要求1、撰寫(xiě)實(shí)驗(yàn)報(bào)告;2、對(duì)實(shí)驗(yàn)中出現(xiàn)的問(wèn)題和結(jié)果進(jìn)展總結(jié)。實(shí)驗(yàn)二鏈表的實(shí)現(xiàn)和應(yīng)用一、實(shí)驗(yàn)?zāi)康?、掌握鏈表的定義;2、掌握鏈表的根本操作,如查找、插入、刪除、排序等。二、實(shí)驗(yàn)內(nèi)容1、單鏈表的創(chuàng)立2、單鏈表的查找3、單鏈表的排序4、單鏈表的刪除5、鏈表的應(yīng)用--約瑟夫環(huán)問(wèn)題三、實(shí)驗(yàn)提示1、//創(chuàng)立單鏈表,要求:結(jié)點(diǎn)個(gè)數(shù)為n個(gè),每個(gè)節(jié)點(diǎn)數(shù)據(jù)域的值必須小于m。編輯主函數(shù)驗(yàn)證之。#include<stdio.h>#include<stdlib.h>typedefstructaa{intdata;structaa*next;}NODE;NODE*Creatlink(intn,intm){ inti; NODE*tou,*p; //tou頭結(jié)點(diǎn) tou=(NODE*)malloc(sizeof(NODE)); //創(chuàng)立并初始化頭結(jié)點(diǎn) tou->next=NULL;tou->data=n; printf("請(qǐng)輸入%d個(gè)小魚(yú)%d的數(shù),中間用空格隔開(kāi):\n",n,m); for(i=0;i<n;i++) //頭插法 { p=(NODE*)malloc(sizeof(NODE)); scanf("%d",&p->data); if(p->data>=m) { printf("輸入的第%d個(gè)數(shù)據(jù)大于%d,GG\n",i+1,m); exit(0); //程序強(qiáng)制中斷,好似是在頭文件stdlib.h里 } p->next=tou->next; tou->next=p; } returntou;}outlink(NODE*h){NODE*p;p=h->next;printf("\n\nTHELIST:\n\nHEAD");while(p){printf("->%d",p->data);p=p->next;}printf("\n");}main(){NODE*head;head=Creatlink(8,22);outlink(head);}2、//查找值為ch的節(jié)點(diǎn)在鏈表中是否出現(xiàn),如果存在,返回在鏈表中位序,如果不存在返回0#include<stdio.h>#include<stdlib.h>#defineN8typedefstructlist{intdata;structlist*next;}SLIST;SLIST*creatlist(char*);voidoutlist(SLIST*);intfun(SLIST*h,charch){ inti; SLIST*p; p=h->next; //p賦值為壽元節(jié)點(diǎn) for(i=0;i<N;i++) { if(p->data==ch) returni+1; p=p->next; } return0;}main(){SLIST*head;intk;charch;chara[N]={'m','p','g','a','w','x','r','d'};head=creatlist(a);outlist(head);printf("Enteraletter:");scanf("%c",&ch);k=fun(head,ch);if(k==0)printf("\nNotfound!\n");elseprintf("Thesequencenumberis:%d\n",k);}SLIST*creatlist(char*a){ inti; SLIST*tou,*p; tou=(SLIST*)malloc(sizeof(SLIST)); //創(chuàng)立并初始化頭結(jié)點(diǎn) tou->data=N; tou->next=NULL; for(i=0;i<N;i++) //前叉法 { p=(SLIST*)malloc(sizeof(SLIST)); p->data=a[i]; p->next=tou->next; tou->next=p; } returntou;}voidoutlist(SLIST*h){SLIST*p;p=h->next;if(p==NULL)printf("\nThelistisNULL!\n");else{printf("\nHead");do{printf("->%c",p->data);p=p->next;}while(p!=NULL);printf("->End\n");}}3、//去偶操作,鏈表中各節(jié)點(diǎn)按數(shù)據(jù)域遞增有序鏈接,函數(shù)fun的功能是,刪除鏈表中數(shù)據(jù)域值一樣的節(jié)點(diǎn),使之只保存一個(gè)#include<stdio.h>#include<stdlib.h>#defineN8typedefstructlist{intdata;structlist*next;}SLIST;voidfun(SLIST*h){ SLIST*p,*shanchu; //用于遍歷的指針p,用于刪除的指針shanchu p=h->next; /p為壽元節(jié)點(diǎn) while(p->next!=NULL) //終止條件 { if(p->data==p->next->data) //判斷是否有重復(fù)原素 { shanchu=p->next; p->next=shanchu->next; free(shanchu); } else p=p->next; }}SLIST*creatlist(int*a){SLIST*h,*p,*q;inti;h=p=(SLIST*)malloc(sizeof(SLIST));for(i=0;i<N;i++){q=(SLIST*)malloc(sizeof(SLIST));q->data=a[i];p->next=q;p=q;}p->next=0;returnh;}voidoutlist(SLIST*h){SLIST*p;p=h->next;if(p==NULL)printf("\nThelistisNULL!\n");else{printf("\nHead");do{printf("->%d",p->data);p=p->next;}while(p!=NULL);printf("->End\n");}}main(){SLIST*head;inta[N]={1,2,2,3,4,4,4,5};head=creatlist(a);printf("\nThelistbeforedeleting:\n");outlist(head);fun(head);printf("\nThelistafterdeleting:\n");outlist(head);}4、//在main函數(shù)中屢次調(diào)用fun函數(shù),每調(diào)用一次fun函數(shù),輸出鏈表尾部節(jié)點(diǎn)中的數(shù)據(jù),并釋放該節(jié)點(diǎn),使得鏈表縮短。#include<stdio.h>#include<stdlib.h>#defineN8typedefstructlist{intdata;structlist*next;}SLIST;voidfun(SLIST*p){ SLIST*bianli,*shanchu; //遍歷,刪除 bianli=p; while(bianli->next->next!=NULL) { bianli=bianli->next; } printf("%d",bianli->next->data); //輸出 shanchu=bianli->next; //釋放 free(shanchu); bianli->next=NULL;}SLIST*creatlist(int*a){SLIST*h,*p,*q;inti;h=p=(SLIST*)malloc(sizeof(SLIST));for(i=0;i<N;i++){q=(SLIST*)malloc(sizeof(SLIST));q->data=a[i];p->next=q;p=q;}p->next=0;returnh;}voidoutlist(SLIST*h){SLIST*p;p=h->next;if(p==NULL)printf("\nThelistisNULL!\n");else{printf("\nHead");do{printf("->%d",p->data);p=p->next;}while(p!=NULL);printf("->End\n");}}main(){SLIST*head;inta[N]={11,12,15,18,19,22,25,29};head=creatlist(a);printf("\nOutputfromhead:\n");outlist(head);printf("\nOutputfromtail:\n");while(head->next!=NULL){fun(head);printf("\n\n");printf("\nOutputfromheadagain:\n");outlist(head);}}5、實(shí)現(xiàn)約瑟夫環(huán)函數(shù)〔選做〕#include<stdio.h>#include<stdlib.h>typedefstructlist{intdata;structlist*next;}SLIST;SLIST*creatlist(intm){ inti; SLIST*tou,*p,*wei;//頭指針生成節(jié)點(diǎn)指針尾指針 tou=(SLIST*)malloc(sizeof(SLIST)); //頭節(jié)點(diǎn) wei=tou; printf("請(qǐng)輸入%d個(gè)數(shù)用空格隔開(kāi):\n",m); for(i=0;i<m;i++) //尾插法 { p=(SLIST*)malloc(sizeof(SLIST)); scanf("%d",&p->data); wei->next=p; wei=p; } wei->next=tou->next; //令最后一個(gè)原素指向首元結(jié)點(diǎn)成環(huán) returntou;}voidoutlist(SLIST*h,intm,intc){ inti; SLIST*p,*shanchu; //用于遍歷的指針p,用于刪除的指針shanchu p=h->next; //p指向首元結(jié)點(diǎn) while(p!=p->next) //當(dāng)環(huán)中只剩下一個(gè)原素時(shí)完畢 { for(i=1;i<c-1;i++) //根據(jù)輸入的c剔除節(jié)點(diǎn) { p=p->next; } shanchu=p->next; //shanchu指向當(dāng)前要剔除的節(jié)點(diǎn) printf("%d",shanchu->data); p->next=shanchu->next; //將shanchu指針指向的節(jié)點(diǎn)出環(huán) free(shanchu); p=p->next; } printf("%d",p->data); //輸出最后的一個(gè)節(jié)點(diǎn)的內(nèi)容 free(p); free(h);}main(){SLIST*head;intm,c; printf("請(qǐng)分別輸入m和c的值:"); scanf("%d,%d",&m,&c);head=creatlist(m);outlist(head,m,c);}四、實(shí)驗(yàn)報(bào)告要求1、撰寫(xiě)實(shí)驗(yàn)報(bào)告;2、對(duì)實(shí)驗(yàn)中出現(xiàn)的問(wèn)題和結(jié)果進(jìn)展總結(jié)。實(shí)驗(yàn)三棧的實(shí)現(xiàn)和應(yīng)用一、實(shí)驗(yàn)?zāi)康?、掌握棧的建設(shè)方法;2、掌握棧的根本操作,如入棧、出棧、判斷空棧等;3、棧的應(yīng)用。二、實(shí)驗(yàn)內(nèi)容1、順序棧的初始化2、判斷棧是否為空3、順序棧出棧4、順序棧入棧5、棧的應(yīng)用--漢諾塔三、實(shí)驗(yàn)提示1、棧的根本操作,按提示將函數(shù)補(bǔ)充完整#include<stdio.h>#include<stdlib.h>#defineSTACK_MAX100typedefstruct{inttop;intdata[STACK_MAX];}stack;voidinit(stack*st)/*初始化順序棧*/{ st->top=0;}intEmpty(stack*st)/*判斷棧是否為空*/{ if(st->top==0) return0; //空0 else return1; //非空1}intpop(stack*st)/*出棧*/{ returnst->data[--st->top];}voidpush(stack*st,intdata)/*入棧*/{ st->data[st->top++]=data;}intmain(void){stackst;init(&st);push(&st,5);push(&st,6);printf("%d",pop(&st));return0;}2、#include<stdio.h>voidmain(){voidhanoi(intn,charone,chartwo,charthree);/*對(duì)hanoi函數(shù)的聲明*/intm;printf("inputthenumberofdiskes:");scanf("%d",&m);printf("Thesteptomoveing%ddiskes:\n",m);hanoi(m,'A','B','C');}voidhanoi(intn,charone,chartwo,charthree)/*定義hanoi函數(shù),將n個(gè)盤(pán)從one座借助two座,移到three座*/{ statick=1; //定義靜態(tài)變量k用來(lái)標(biāo)明走了多少步 voidmove(charx,chary); //因?yàn)閙ove函數(shù)定義在該函數(shù)的后邊且之前咩有聲明在此需要提前聲明才能使用 if(n==1) //當(dāng)?shù)谝粋€(gè)座上僅剩一個(gè)盤(pán)的時(shí)候?qū)⒋吮P(pán)移到第三個(gè)上 { printf("第%d步:",k++); //輸出是第多少步 move(one,three); //移動(dòng) } else { hanoi(n-1,one,three,two); //將前n-1個(gè)盤(pán)從第一個(gè)座移到二個(gè)座上,第三個(gè)座當(dāng)橋梁 printf("第%d步:",k++); move(one,three); hanoi(n-1,two,one,three); //將上邊轉(zhuǎn)移到第二個(gè)座上的盤(pán)轉(zhuǎn)移到第三個(gè)盤(pán)上,第一個(gè)盤(pán)當(dāng)橋梁 }}voidmove(charx,chary)/*定義move函數(shù)*/{printf("%c-->%c\n",x,y);}四、實(shí)驗(yàn)報(bào)告要求1、撰寫(xiě)實(shí)驗(yàn)報(bào)告;2、對(duì)實(shí)驗(yàn)中出現(xiàn)的問(wèn)題和結(jié)果進(jìn)展總結(jié)。實(shí)驗(yàn)四隊(duì)列的實(shí)現(xiàn)和應(yīng)用一、實(shí)驗(yàn)?zāi)康?、掌握隊(duì)列的建設(shè)方法;2、掌握隊(duì)列的根本操作,如出隊(duì)、入隊(duì)、判斷隊(duì)空等;3、隊(duì)列的應(yīng)用。二、實(shí)驗(yàn)內(nèi)容1、順序隊(duì)列的初始化2、判斷隊(duì)列是否為空3、順序隊(duì)列出隊(duì)4、順序隊(duì)列入隊(duì)5、隊(duì)列的應(yīng)用--回文判斷三、實(shí)驗(yàn)提示1、//隊(duì)列的根本操作,按提示將函數(shù)補(bǔ)充完整#include<stdio.h>#include<stdlib.h>#defineSTACK_MAX100typedefstruct{intfront,rear;intdata1[STACK_MAX];}Queue;voidinitQueue(Queue*q)/*初始化隊(duì)列*/{ q->front=q->rear=0;}intEmptyQueue(Queue*q)/*判斷隊(duì)列空*/{ if(q->front==q->rear) return1; //1代表空 else return0; //0代表非空}intDeQueue(Queue*q)/*出隊(duì)列*/{ if(q->rear==q->front) //判斷需要出隊(duì)時(shí)隊(duì)列是否為空 { printf("當(dāng)前隊(duì)列已經(jīng)空了。"); exit(0); } else { returnq->data1[q->front++]; //將隊(duì)頭原素出列然后隊(duì)頭指針加一 }}voidInQueue(Queue*q,intdata)/*入隊(duì)列*/{ if(q->rear==STACK_MAX) //判斷需要入隊(duì)時(shí)隊(duì)列是否已滿 { printf("當(dāng)前隊(duì)列空間已滿。"); exit(0); } else { q->data1[q->rear]=data; //入隊(duì) q->rear++; }}intmain(){ Queueq; initQueue(&q); InQueue(&q,1); InQueue(&q,2); InQueue(&q,3); printf("%d\n",DeQueue(&q)); printf("%d\n",DeQueue(&q)); printf("%d\n",DeQueue(&q));}2、//判斷給定的字符序列是否是回文〔提示:將一半字符入棧〕#include<stdio.h>#include<stdlib.h>#defineSTACK_MAX100typedefstruct{inttop;intdata[STACK_MAX];}stack;typedefstruct{intfront,rear;intdata1[STACK_MAX];}Queue;voidinit(stack*st)/*初始化順序棧*/{ st->top=0;}intEmpty(stack*st)/*判斷???/{ if(st->top==0) return1; else return0;}}intpop(stack*st)/*出棧*/{ if(st->top==0) { printf("棧已空!"); exit(0); } else { charc; c=st->data[--st->top]; return〔int〕c; }}voidpush(stack*st,intdata)/*入棧*/{ if(st->top==STACK_MAX-1) { printf("棧已空!"); exit(0); } else { st->data[st->top++]=data; }}voidinitQueue(Queue*q)/*初始化隊(duì)列*/{ q->front=q->rear=0;}intEmptyQueue(Queue*q)/*判斷隊(duì)列空*/{ if(q->front==q->rear) return1; else return0;}intDeQueue(Queue*q)/*出隊(duì)列*/{ return(int)q->data1[q->front++];}voidInQueue(Queue*q,intdata)/*入隊(duì)列*/{ q->data1[q->rear++]=data;}intIsHuiWen(stack*st,Queue*q,char*a){{ inti,zhan,dui,k=0; //i計(jì)數(shù),zhan代表應(yīng)往棧里邊傳幾個(gè)原素,dui代表應(yīng)從第幾個(gè)原素開(kāi)場(chǎng)往隊(duì)列傳值,k計(jì)算數(shù)組a[]中有多少原素 while(a[k]!='\0') k++; if(k%2==0) { zhan=k/2; dui=k/2+1; } if(k%2==1) { zhan=k/2; dui=k/2+2; } for(i=0;i<zhan;i++) { push(st,a[i]); } for(i=zhan;i<k;i++) { InQueue(q,a[i]); } for(i=0;i<k/2;i++) { if(pop(st)!=DeQueue(q)) return0; } return1;}intmain(){ chara[10]={'a','b','c','d','b','a'}; stackst; Queueq; init(&st); initQueue(&q);printf("%d\n",IsHuiWen(&st,&q,a));}四、實(shí)驗(yàn)報(bào)告要求1、撰寫(xiě)實(shí)驗(yàn)報(bào)告;2、對(duì)實(shí)驗(yàn)中出現(xiàn)的問(wèn)題和結(jié)果進(jìn)展總結(jié)。實(shí)驗(yàn)五二叉樹(shù)的遍歷及應(yīng)用一、實(shí)驗(yàn)?zāi)康?.掌握二叉樹(shù)的定義;2.掌握二叉樹(shù)的根本操作,如二叉樹(shù)的建設(shè)、遍歷、結(jié)點(diǎn)個(gè)數(shù)統(tǒng)計(jì)、樹(shù)的深度計(jì)算等。二、實(shí)驗(yàn)內(nèi)容用遞歸的方法實(shí)現(xiàn)以下算法:1.以二叉鏈表表示二叉樹(shù),建設(shè)一棵二叉樹(shù);2.輸出二叉樹(shù)的中序遍歷結(jié)果;3.輸出二叉樹(shù)的前序遍歷結(jié)果;4.輸出二叉樹(shù)的后序遍歷結(jié)果;5.計(jì)算二叉樹(shù)的深度;6.統(tǒng)計(jì)二叉樹(shù)的結(jié)點(diǎn)個(gè)數(shù);7、二叉樹(shù)的層序遍歷結(jié)果。三、實(shí)驗(yàn)提示1、//按要求將程序補(bǔ)充完整#include<stdio.h>#include<string.h>#include<stdlib.h>#defineNULL0typedefstructBiTNode{chardata;structBiTNode*lchild,*rchild;}BiTNode,*BiTree;//二叉樹(shù)的建設(shè)BiTreeCreate(BiTreeT){ charch; //設(shè)置一個(gè)接收數(shù)據(jù)的變量scanf("%c",&ch);if(ch=='#')T=NULL; //設(shè)置完畢符else{T=(BiTree)malloc(sizeof(BiTNode)); //生成心節(jié)點(diǎn)T->data=ch; T->lchild=Create(T->lchild); //遞歸建設(shè)T->rchild=Create(T->rchild);}returnT;}//二叉樹(shù)的前序遞歸遍歷voidPreorder(BiTreeT){ if(T) { printf("%c",T->data); Preorder(T->lchild); Preorder(T->rchild); }}//統(tǒng)計(jì)二叉樹(shù)的結(jié)點(diǎn)個(gè)數(shù)intSumleaf(BiTreeT){ intn; if(T==NULL)return0; else { n=1+Sumleaf(T->lchild)+Sumleaf(T->rchild); } returnn;}//二叉樹(shù)的中序遞歸遍歷voidzhongxu(BiTreeT){ if(T) { Preorder(T->lchild); printf("%c",T->data); Preorder(T->rchild); }}//二叉樹(shù)的后序遞歸遍歷voidhouxu(BiTreeT){ if(T) { Preorder(T->lchild); Preorder(T->rchild); printf("%c",T->data); }}//計(jì)算二叉樹(shù)的深度intDepth(BiTreeT){ //誰(shuí)大選誰(shuí) intn; if(T==NULL) return0; else if(Depth(T->lchild)>Depth(T->rchild))returnDepth(T->lchild)+1; else returnDe
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度交通安全知識(shí)普及與駕駛技能培訓(xùn)合同
- 企業(yè)并購(gòu)居間合同委托書(shū)
- 二零二五年度辦公室勞動(dòng)合同地址確認(rèn)及員工離職補(bǔ)償協(xié)議
- 三農(nóng)田灌溉方案與實(shí)施手冊(cè)
- 汽車(chē)維修保養(yǎng)規(guī)范手冊(cè)
- 醫(yī)療器械產(chǎn)品采購(gòu)合同
- 石材購(gòu)銷(xiāo)合同補(bǔ)充合同
- 合作收購(gòu)不良資產(chǎn)協(xié)議
- 人力資源管理勞動(dòng)法律法規(guī)遵守作業(yè)指導(dǎo)書(shū)
- 企業(yè)并購(gòu)交易操作指導(dǎo)書(shū)
- 空調(diào)維保應(yīng)急預(yù)案
- 2023年高考語(yǔ)文全國(guó)乙卷作文范文及導(dǎo)寫(xiě)(解讀+素材+范文)課件版
- 模塊建房施工方案
- 多域聯(lián)合作戰(zhàn)
- 生理產(chǎn)科學(xué)-正常分娩期的護(hù)理(助產(chǎn)學(xué)課件)
- 煤場(chǎng)用車(chē)輛倒運(yùn)煤的方案
- PPK計(jì)算模板完整版
- 居民自建房經(jīng)營(yíng)業(yè)態(tài)不超過(guò)三種承諾書(shū)
- 河南省陜州區(qū)王家后鄉(xiāng)滹沱鋁土礦礦產(chǎn)資源開(kāi)采與生態(tài)修復(fù)方案
- 中國(guó)高血壓臨床實(shí)踐指南(2022版)解讀
- 最常用漢字5000個(gè)
評(píng)論
0/150
提交評(píng)論