南京航空航天大學(xué)軟件技術(shù)基礎(chǔ)數(shù)據(jù)結(jié)構(gòu)上機(jī)實(shí)驗(yàn)報(bào)告_第1頁(yè)
南京航空航天大學(xué)軟件技術(shù)基礎(chǔ)數(shù)據(jù)結(jié)構(gòu)上機(jī)實(shí)驗(yàn)報(bào)告_第2頁(yè)
南京航空航天大學(xué)軟件技術(shù)基礎(chǔ)數(shù)據(jù)結(jié)構(gòu)上機(jī)實(shí)驗(yàn)報(bào)告_第3頁(yè)
南京航空航天大學(xué)軟件技術(shù)基礎(chǔ)數(shù)據(jù)結(jié)構(gòu)上機(jī)實(shí)驗(yàn)報(bào)告_第4頁(yè)
南京航空航天大學(xué)軟件技術(shù)基礎(chǔ)數(shù)據(jù)結(jié)構(gòu)上機(jī)實(shí)驗(yàn)報(bào)告_第5頁(yè)
已閱讀5頁(yè),還剩35頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

經(jīng)典word整理文檔,僅參考,雙擊此處可刪除頁(yè)眉頁(yè)腳。本資料屬于網(wǎng)絡(luò)整理,如有侵權(quán),請(qǐng)聯(lián)系刪除,謝謝!南京航空航天大學(xué)計(jì)算機(jī)軟件技術(shù)基礎(chǔ)實(shí)踐實(shí)驗(yàn)報(bào)告專年月1實(shí)驗(yàn)一------------------------------------4第一種算法---------------------------4第二種算法---------------------------7實(shí)驗(yàn)二------------------------------------9------------------------------9------------------------------11------------------------------14鏈棧---------------------------------15---------------------------------18實(shí)驗(yàn)三------------------------------------20二叉樹----------------------------20實(shí)驗(yàn)四------------------------------------22順序查找------------------------22二分查找------------------------24插入排序------------------------26選擇排序------------------------28冒泡排序------------------------30大作業(yè)------------------------------------32---------------------------322一、實(shí)驗(yàn)一實(shí)驗(yàn)對(duì)象:順序表。實(shí)驗(yàn)?zāi)康模簩?shí)現(xiàn)兩個(gè)順序表的合并。實(shí)驗(yàn)要求:將共同擁有的元素只存其一。思路清晰,編程正確,并將數(shù)據(jù)元素顯示出來(lái)。提高部分:實(shí)現(xiàn)同一表中去重的方法二、實(shí)驗(yàn)二實(shí)驗(yàn)對(duì)象:順序表、單鏈表、順序棧、鏈棧、隊(duì)列。實(shí)驗(yàn)?zāi)康模簩?shí)現(xiàn)順序表、單鏈表、順序棧、鏈棧、隊(duì)列的順序存儲(chǔ)結(jié)構(gòu)、鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)的定義、創(chuàng)建、插入和刪除操作。實(shí)驗(yàn)要求:思路清晰,編程正確,并將數(shù)據(jù)元素顯示出來(lái);注意各種數(shù)據(jù)結(jié)構(gòu)的順序存儲(chǔ)結(jié)構(gòu)和鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)的異同。三、實(shí)驗(yàn)三實(shí)驗(yàn)對(duì)象:鏈?zhǔn)蕉鏄?。?shí)驗(yàn)?zāi)康模簩?shí)現(xiàn)二叉樹的鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)的數(shù)據(jù)結(jié)構(gòu)的定義、創(chuàng)建、先序、中序和后序遍歷操作。實(shí)驗(yàn)要求:思路清晰,編程正確,并將結(jié)果序列輸出;注意合理運(yùn)用函數(shù)的遞歸調(diào)用。四、實(shí)驗(yàn)四實(shí)驗(yàn)對(duì)象:順序表。實(shí)驗(yàn)?zāi)康模赫莆枕樞虿檎摇⒍植檎?、插入排序、選擇排序、冒泡排序等基本的查找方法。實(shí)驗(yàn)要求:思路清晰,編程正確,并將查找結(jié)果顯示出來(lái)。提高部分:實(shí)現(xiàn)自動(dòng)產(chǎn)生數(shù)據(jù)并輸出或人工輸入數(shù)據(jù)的雙向選擇。通訊錄的制作五、綜合練習(xí)一、設(shè)計(jì)題目:通訊錄的制作二、設(shè)計(jì)目的:運(yùn)用〈數(shù)據(jù)結(jié)構(gòu)〉中的算法思想作數(shù)據(jù)結(jié)構(gòu),結(jié)合C語(yǔ)言基本知識(shí),編寫一個(gè)通訊錄管理系統(tǒng)。以把所學(xué)數(shù)據(jù)結(jié)構(gòu)知識(shí)應(yīng)用到實(shí)際軟件開發(fā)中去。3通過完成本課題,進(jìn)一步熟悉C語(yǔ)言的基本知識(shí),并掌握數(shù)據(jù)結(jié)構(gòu)的一些基本算法思想,掌握C法,數(shù)組的建立運(yùn)用和函數(shù)的調(diào)用等。三、設(shè)計(jì)內(nèi)容:本系統(tǒng)應(yīng)完成以下幾方面的功能:調(diào)用此函數(shù)用以輸入數(shù)據(jù)到內(nèi)存中,此過程包括建立相應(yīng)的鏈表或相應(yīng)的數(shù)組,便于讀取②顯示信息(Display()):用以顯示輸入的數(shù)據(jù),包括從內(nèi)存中讀出和從磁盤中讀出③查找(Search()):以姓名作為關(guān)鍵字查找要找的信息④刪除信息(Delete()):用以刪除選定的輸入信息(姓名作為關(guān)鍵字)⑤存盤(Save()):調(diào)用此函數(shù)將內(nèi)存中的數(shù)據(jù)保存至磁盤中⑥裝入(Load()):調(diào)用此函數(shù)用以將之前保存在磁盤的內(nèi)容讀入到內(nèi)存中或顯示到屏幕上注:本課題中輸入的數(shù)據(jù)應(yīng)包括以下幾項(xiàng)信息:姓名(NAME)學(xué)校(SCHOOL)城市(CITY)郵編(EIP)國(guó)家(STATE)編程對(duì)很多人來(lái)說(shuō)可能是一件很痛苦的事。但編程對(duì)我反而是一件很快樂事。我并不喜歡編程,但我很享受那種調(diào)出來(lái)時(shí)的感覺。每次編程時(shí)總會(huì)有很多錯(cuò)誤,有定義出錯(cuò)的,有算法出錯(cuò)的,還有打錯(cuò)的。由于對(duì)鏈表,指針,類等等之類掌握得差,所以每當(dāng)編譯時(shí),總是錯(cuò)誤百出,好不容易編譯通過了,但往往算法會(huì)存在原理性錯(cuò)誤。幸運(yùn)的是,通過參考各方面資料,以及在老師,同學(xué)的幫助下,都能實(shí)現(xiàn)所需要的功能,雖然不是很完美,但那是我努力的見證,那是我自己編出來(lái)的代碼,每調(diào)出一個(gè)程序,心里就很開心。很多在編譯、連接通過后,程序卻運(yùn)行不成功。這種錯(cuò)誤的查找是最難的,需要有相當(dāng)?shù)哪托暮图?xì)心去把問題找出來(lái)。這也是本次程序編輯過程中碰到的最大的難題,往往運(yùn)行之后得不到結(jié)果。經(jīng)過總結(jié)主要為申請(qǐng)地址,循環(huán)出錯(cuò)。此時(shí),我的解決方法是“注釋”法,即將有問題的大片語(yǔ)句用“/*……*/”注釋掉,以檢驗(yàn)其它語(yǔ)句的正確性,然后用printf函數(shù)輸出任意hello等字符,逐步減小注釋范圍,最終確定錯(cuò)誤的具體位置??傊幊逃绕涫钦{(diào)試是一項(xiàng)細(xì)致深入的工作,需要下工夫、動(dòng)腦子,很考驗(yàn)?zāi)土Φ囊豁?xiàng)工作。以上便是本人對(duì)于此次數(shù)據(jù)結(jié)構(gòu)上機(jī)實(shí)踐的一些心得體會(huì)。實(shí)驗(yàn)一:實(shí)現(xiàn)兩個(gè)順序表的合并。(說(shuō)明:將共同擁有的元素只存其一。)類型一://線性表(數(shù)組)合并#include<stdio.h>#include<time.h>4#include<stdlib.h>voidprint(inta[],intm);/*輸出函數(shù)*/voidcombin(inta[],intb[],intc[],intn,intm);//合并函數(shù)voidsort(inta[],intn);//排序函數(shù)voidget(inta[],intn);//元素產(chǎn)生函數(shù)intmain(){)inti,num;intj=0;inta[50]={0},b[50]={0},c[100]={0},n,m;//n為數(shù)據(jù)個(gè)數(shù)printf("firststructnumbern:\n");scanf("%d",&n);get(a,n);sort(a,n);)printf("A:\n");print(a,n);printf("secondstructnumberm:\n");scanf("%d",&m);get(b,m);sort(b,m);printf("B:\n");print(b,m);combin(a,b,c,n,m);sort(c,n+m);num=n+m;for(i=0;i<num;i++){j=i+1;for(;j<num;j++)c{組if(c[i]==c[j])c[j]=0;}}i=0;printf("C:\n");if(c[0]==0)printf("%d\t",c[0]);5for(;i<num;){if(c[i]==0)i++;elseprintf("%d\t",c[i++]);}printf("\n");return0;}voidsort(inta[100],intn){intt,i,j,k;for(i=0;i<n;i++){for(j=i+1;j<n;j++)if(a[i]>a[j]){t=a[j];a[j]=a[i];a[i]=t;}}}voidcombin(inta[100],intb[100],intc[100],intn,intm){inti=0,j=0,k=0;for(;i<m;i++)c[j++]=b[i];for(;k<n;k++)c[j++]=a[k];}voidprint(inta[100],intm){inti=0;printf("\n");for(;i<m;i++)printf("%d\t",a[i]);printf("\n");6}voidget(inta[50],intn){inti=0;srand(time(0));while(i<n)a[i++]=rand()%10;}類型二//線性表(結(jié)構(gòu)體)合并b#include<stdio.h>#include<string.h>#include<stdlib.h>#defineSeqListstructlisttype#defineMaxSize1000SeqList{intdata[MaxSize];intlast;};voidmerge(SeqList*p,SeqList*q,SeqList*c);//合并函數(shù)SeqList*init_SeqList();//創(chuàng)建鏈表函數(shù)voidmain(){SeqList*p;p=init_SeqList();inti,j,n;i=0;j=0;printf("PleaseinputthelengthofSeqListp:\nn=");scanf("%d",&n);printf("intputthedate:\n");while(i<n){scanf("%d",&p->data[i]);i++;p->last++;}7SeqList*q;q=init_SeqList();printf("PleaseinputthelengthofSeqListq:\nn=");scanf("%d",&n);printf("intputthedate:\n");while(j<n){scanf("%d",&q->data[j]);j++;q->last++;}SeqList*c;c=init_SeqList();merge(p,q,c);}voidmerge(SeqList*p,SeqList*q,SeqList*c)//合并函數(shù){inti,j,k,a;i=0;j=0;k=0;while(i<=p->last)//循環(huán)賦值到c{c->data[k]=p->data[i];k++;i++;}while(j<=q->last){i=0;while(i<=p->last&&p->data[i]!=q->data[j])//循環(huán)賦值到ci++;if(i>p->last){c->data[k]=q->data[j];k++;}j++;}i=0;for(;i<k;i++)//c中去重{for(j=i+1;j<k;j++){8if(c->data[i]==c->data[j]){for(;j<k;j++){c->data[j]=c->data[j+1];}k--;}}}printf("\nthemergeSeqlistc:\n");for(a=0;a<k;a++)printf("%d",c->data[a]);}SeqList*init_SeqList()//創(chuàng)建鏈表函數(shù){SeqList*p;p=(SeqList*)malloc(sizeof(SeqList));p->last=-1;returnp;}實(shí)驗(yàn)二:順序表、單鏈表、順序棧、鏈棧、隊(duì)列的順序存儲(chǔ)結(jié)構(gòu)、鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)的定義、創(chuàng)建、插入和刪除操作,將數(shù)據(jù)元素顯示出來(lái)。//線性表#include<stdio.h>#include<string.h>#include<stdlib.h>#defineMaxSize1000#defineSeqListstructlisttypeSeqList//結(jié)構(gòu)體定義{intdata[MaxSize];intlast;};voidprint(SeqList*p);//輸出函數(shù)SeqList*init_SeqList();//線性表的初始化intInsert_SeqList(SeqList*L,inti,intx);//在第i個(gè)位置插入xintDelete_SeqList(SeqList*L,inti);//刪除第i個(gè)位置的元素main(){9SeqList*p;inti,x;i=0;p=init_SeqList();printf("PleaseinputSeqListp(when-1stop!):\n");scanf("%d",&x);while(x!=(-1)){p->data[i]=x;i++;p->last++;scanf("%d",&x);}printf("\n輸入插入位置i=");scanf("%d",&i);printf("輸入待插數(shù)據(jù)x=");scanf("%d",&x);printf("\n");輸入待插位置Insert_SeqList(p,i,x);printf("待刪除的位置i=");scanf("%d",&i);printf("\n");Delete_SeqList(p,i);}voidprint(SeqList*p)//輸出函數(shù){inti;for(i=0;i<=p->last;i++)printf("%d\t",p->data[i]);printf("\n");}SeqList*init_SeqList()//線性表的初始化{SeqList*L;L=(SeqList*)malloc(sizeof(SeqList));L->last=-1;returnL;}intInsert_SeqList(SeqList*L,inti,intx)//在第i個(gè)位置插入x{intj;i--;10if(L->last==MaxSize-1)//表滿{printf("Seqlistfulled!\n");return-1;}if(i<0||i>L->last+1){printf("Error'i'!\n");return0;}if(i==L->last+1)L->data[i]=x;else{for(j=L->last;j>=i;j--)L->data[j+1]=L->data[j];L->data[i]=x;}L->last++;printf("\n插入后線性表為:\n");print(L);return1;}intDelete_SeqList(SeqList*L,inti)//刪除第i個(gè)位置的元素{intj;i--;if(i<0||i>L->last){printf("error'i'\n");return0;}for(j=i;j<L->last;j++)L->data[j]=L->data[j+1];L->last--;printf("\n刪除后線性表為\n");print(L);return1;}/*單鏈表的定義、創(chuàng)建、插入和刪除操作,將數(shù)據(jù)元素顯示出來(lái)。*/#include<stdio.h>#include<malloc.h>11#defineslnodestructnodeslnode//單鏈表的定義{intdata;slnode*next;};slnode*create();//單鏈表的尾插法創(chuàng)建voidinsert(slnode*h,intx);//單鏈表的插入(插入尾部)intdelete_y(slnode*h,inty);//單鏈表的節(jié)點(diǎn)刪除voidprint(slnode*L);//顯示單鏈表所有元素main(){slnode*h;intx,y;h=create();printf("鏈表初始數(shù)據(jù)為:\n");print(h);printf("請(qǐng)輸入要插入的數(shù)據(jù):\n");scanf("%d",&x);insert(h,x);printf("插入后鏈表數(shù)據(jù)為:\n");print(h);)printf("請(qǐng)輸入要?jiǎng)h除的數(shù)據(jù):\n");scanf("%d",&y);delete_y(h,y);刪除函數(shù)輸出鏈表數(shù)據(jù)})slnode*create()//單鏈表的尾插法創(chuàng)建{slnode*p,*s,*h;inti=0,a;h=(slnode*)malloc(sizeof(slnode));p=h;h->next=NULL;printf("請(qǐng)輸入數(shù)據(jù),以-1為結(jié)尾:\n");scanf("%d",&a);while(a!=-1){s=(slnode*)malloc(sizeof(slnode));s->data=a;if(h->next==NULL)h->next=s;elsep->next=s;p=s;12scanf("%d",&a);}p->next=NULL;return(h);}voidinsert(slnode*h,intx)//單鏈表的插入(插入尾部){slnode*p,*p1;p=(slnode*)malloc(sizeof(slnode));p->data=x;p->next=NULL;p1=h->next;while(p1->next!=NULL)p1=p1->next;p1->next=p;p->next=NULL;}intdelete_y(slnode*h,inty)//單鏈表的節(jié)點(diǎn)刪除{slnode*p1,*p2;p1=h->next;p2=h;while(p1->data!=y&&p1->next!=NULL){p2=p1;p1=p1->next;}if(p1->data!=y){printf("未找到\t");return0;}else{p2->next=p1->next;free(p1);printf("刪除后鏈表數(shù)據(jù)為:\n");print(h);}}voidprint(slnode*L){slnode*p;//顯示單鏈表所有元素for(p=L->next;p!=NULL;p=p->next)printf("%d",p->data);printf("\n");13}//順序棧的定義、創(chuàng)建、入棧和出棧#include<stdio.h>#include<malloc.h>#definemaxsize1024#definestackstructstackstack//順序棧的定義{intdate[maxsize];inttop;};單個(gè)元素入棧函數(shù))voidpush_x(stack*L,intx);stack*create();//創(chuàng)建intpop_y(stack*L);//棧頂元素出棧//入棧voiddisplay(stack*L);//顯示順序棧的數(shù)據(jù)元素main(){stack*h;h=create();printf("出棧順序:\n");display(h);}voidpush_x(stack*L,intx)//單個(gè)元素入棧{if(L->top==maxsize-1)printf("棧滿\n");else{L->top++;L->date[L->top]=x;}}stack*create()//創(chuàng)建{stack*L;inta;L=(stack*)malloc(sizeof(stack));L->top=-1;printf("請(qǐng)輸入入棧元素---以-1結(jié)束.\n");scanf("%d",&a);while(a!=-1){push_x(L,a);scanf("%d",&a);14}printf("\n");return(L);}intpop_y(stack*L)//順序棧出棧{inty;if(L->top==-1)printf("??誠(chéng)n");elsey=L->date[L->top];return(y);}voiddisplay(stack*L)//顯示順序棧的數(shù)據(jù)元素{inty;while(L->top!=-1){y=pop_y(L);printf("%d",y);L->top--;}printf("\n");}//鏈棧的入棧,出棧,以及創(chuàng)建#include<stdio.h>#include<malloc.h>#definelinkstackstructlinkstacklinkstack{intdate;linkstack*next;};linkstack*push_x(linkstack*top,intx);//x入棧linkstack*create();//鏈棧的創(chuàng)建voiddisplay(linkstack*top);//出棧main(){linkstack*h;h=create();printf("出棧順序如下:\n");display(h);15printf("\n");}linkstack*push_x(linkstack*top,intx)//x入棧{linkstack*p;p=(linkstack*)malloc(sizeof(linkstack));p->date=x;p->next=top;top=p;return(top);單個(gè)元素入棧函數(shù))}linkstack*create()//鏈棧的創(chuàng)建{linkstack*top;inta;top=(linkstack*)malloc(sizeof(linkstack));top=NULL;printf("請(qǐng)輸入鏈棧入棧元素---以-1結(jié)束.\n");scanf("%d",&a);while(a!=-1){top=push_x(top,a);scanf("%d",&a);}return(top);}voiddisplay(linkstack*top){inty;linkstack*p;if(top==NULL)printf("該鏈棧為空\(chéng)n");while(top!=NULL){y=top->date;printf("%d",y);p=top;top=top->next;free(p);}}/*順序隊(duì)列*/#include<stdio.h>#include<malloc.h>16#definemaxsize1024#definequeuestructqueuequeue//定義{intdate[maxsize];intfront,rear;intnum;};voidin_queue(queue*L,intx);//入隊(duì)操作queue*create();//創(chuàng)建intout_queue(queue*L);//出隊(duì)操作voiddisplay(queue*L);//顯示順序隊(duì)列的數(shù)據(jù)元素voiddisplay(queue*L)//顯示順序隊(duì)列的數(shù)據(jù)元素{while(L->num!=0){printf("%d",out_queue(L));}單個(gè)元素入棧函}數(shù))main(){queue*L;L=create();printf("該順序隊(duì)列出隊(duì)順序如下:\n");display(L);printf("\n");}voidin_queue(queue*L,intx)//入隊(duì)操作{if(L->num==maxsize)printf("隊(duì)滿\n");else{L->rear=(L->rear+1)%maxsize;L->date[L->rear]=x;L->num++;}}queue*create()//創(chuàng)建{queue*L;inta;L=(queue*)malloc(sizeof(queue));L->front=L->rear=-1;17L->num=0;printf("請(qǐng)輸入隊(duì)列元素---以-1為結(jié)尾:\n");scanf("%d",&a);while(a!=-1){in_queue(L,a);scanf("%d",&a);}return(L);}intout_queue(queue*L)//出隊(duì)操作{inty;if(L->num==0){printf("隊(duì)空\(chéng)n");單個(gè)元素入棧函return(0);數(shù))}else{L->front=(L->front+1)%maxsize;y=L->date[L->front];L->num--;return(y);}}//鏈隊(duì)#include<stdio.h>#include<malloc.h>#defineqnodestructqnode#definelinkqueuestructlinkqueueqnode//定義{intdate;qnode*next;};linkqueue//將鏈隊(duì)頭尾指針封裝在一起{qnode*front,*rear;};voidin_qnode(linkqueue*q,intx);//入隊(duì)操作linkqueue*create();//創(chuàng)建intout_qnode(linkqueue*q);//出隊(duì)voiddisplay(linkqueue*q);//顯示鏈隊(duì)的數(shù)據(jù)元素18main(){linkqueue*L;L=create();printf("該鏈隊(duì)出隊(duì)順序如下:\n");display(L);printf("\n");}voidin_qnode(linkqueue*q,intx)//入隊(duì)操作{qnode*p;p=(qnode*)malloc(sizeof(qnode));p->date=x;p->next=NULL;q->rear->next=p;q->rear=p;}linkqueue*create()//創(chuàng)建{qnode*L;inta;linkqueue*q;L=(qnode*)malloc(sizeof(qnode));q=(linkqueue*)malloc(sizeof(linkqueue));L->next=NULL;q->rear=q->front=L;printf("請(qǐng)輸入鏈隊(duì)入隊(duì)元素,以-1為結(jié)尾:\n");scanf("%d",&a);while(a!=-1){in_qnode(q,a);scanf("%d",&a);}return(q);}intout_qnode(linkqueue*q)//出隊(duì){inty;qnode*p;if(q->rear==q->front){printf("隊(duì)空\(chéng)n");return(0);19}p=q->front->next;y=p->date;q->front->next=p->next;//釋放pfree(p);if(q->front->next==NULL)q->rear=q->front;return(y);}voiddisplay(linkqueue*q)//顯示鏈隊(duì)的數(shù)據(jù)元素{while(q->front!=q->rear)printf("%d",out_qnode(q));}實(shí)驗(yàn)三:二叉樹的鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)的數(shù)據(jù)結(jié)構(gòu)定義、創(chuàng)建、先序/中序/后序遍歷,并將結(jié)果序列輸出。//二叉樹//測(cè)試輸入ABD0G000CE00F00輸出ABDGCEFDGBAECFGDBEFCA#include<stdio.h>#include<malloc.h>typedefstructbitree{chara;structbitree*lchild,*rchild;}Bitree;Bitree*create(Bitree*bt);//創(chuàng)建voidpreorder(Bitree*bt);//前序遍歷voidinorder(Bitree*bt);//中序遍歷voidpostorder(Bitree*bt);//后序遍歷main(){inta;Bitree*bt;bt=(Bitree*)malloc(sizeof(Bitree));printf("先序輸入數(shù)據(jù),0表示停止\n");\bt=create(bt);printf("\n\n先序輸出數(shù)據(jù)\n");preorder(bt);printf("\n\n中序輸出數(shù)據(jù)\n");inorder(bt);20printf("\n\n后序輸出數(shù)據(jù)\n");postorder(bt);}Bitree*create(Bitree*bt)//創(chuàng)建{charx;sscanf("\n%c",&x);if(x=='0')bt=NULL;)else{bt=(Bitree*)malloc(sizeof(Bitree));bt->a=x;bt->lchild=create(bt->lchild);bt->rchild=create(bt->rchild);)}return(bt);}voidpreorder(Bitree*bt)//前序遍歷{if(bt==NULL))return;printf("%c",bt->a);preorder(bt->lchild);preorder(bt->rchild);})voidinorder(Bitree*bt)//中序遍歷{if(bt==NULL)return;inorder(bt->lchild);printf("%c",bt->a);inorder(bt->rchild);}voidpostorder(Bitree*bt)//后序遍歷{if(bt==NULL)return;postorder(bt->lchild);postorder(bt->rchild);printf("%c",bt->a);}實(shí)驗(yàn)四:查找:順序查找、二分查找排序:插入排序、選擇排序、冒泡排序21//順序查找#include<stdio.h>#include<malloc.h>typedefstructNODE{inta[1024];}node;voidchoose(node*p,intm,intn);//選擇自動(dòng)或者手動(dòng)輸入voidget(node*p,intn);//隨機(jī)產(chǎn)生函數(shù)voidprint(node*p,intn);//輸出函數(shù)intsearch(node*p,intn,intdata);//查找函數(shù)main(){=1m=2)node*p;p=(node*)malloc(sizeof(node));intn;//數(shù)據(jù)總個(gè)數(shù)intm,data,temp;)printf("輸入輸入的元素總個(gè)數(shù)n:");scanf("%d",&n);printf("\n1自動(dòng)輸入數(shù)據(jù):\n2手動(dòng)輸入數(shù)據(jù):\n");scanf("%d",&m);choose(p,m,n);printf("\n輸入待查數(shù)據(jù):data=");scanf("%d",&data);)temp=search(p,n,data);if(!temp)printf("\n沒有這個(gè)數(shù)據(jù)\n");}voidchoose(node*p,intm,intn){switch(m){case1:{get(p,n);printf("\n表中數(shù)據(jù)為:\n");print(p,n);}break;case2:{inti;22intx;printf("輸入數(shù)據(jù)\n");for(i=0;i<n;i++){scanf("%d",&x);p->a[i]=x;}}}}voidget(node*p,intn){inti;srand(time(0));for(i=0;i<n;i++)p->a[i]=rand()%100;}voidprint(node*p,intn){inti;for(i=0;i<n;i++)printf("%d\t",p->a[i]);printf("\n\n");}intsearch(node*p,intn,intdata){inti,temp;temp=0;for(i=0;i<n;i++){if(p->a[i]==data){printf("\n查找成功---數(shù)據(jù)為:%d",p->a[i]);temp=1;}}returntemp;}//二分查找#include<stdio.h>#include<malloc.h>typedefstructNODE23{inta[1024];}node;voidchoose(node*p,intm,intn);//選擇自動(dòng)或者手動(dòng)輸入voidget(node*p,intn);//隨機(jī)產(chǎn)生函數(shù)voidprint(node*p,intn);//輸出函數(shù)intsearch(node*p,intn,intdata);//查找函數(shù)main(){node*p;)p=(node*)malloc(sizeof(node));intn;//數(shù)據(jù)總個(gè)數(shù)intm,data,temp;printf("輸入輸入的元素總個(gè)數(shù)n:");scanf("%d",&n);printf("\n1自動(dòng)輸入數(shù)據(jù):\n2手動(dòng)輸入數(shù)據(jù):\n");)scanf("%d",&m);get(p,n);choose(p,m,n);printf("\n輸入待查數(shù)據(jù):data=");scanf("%d",&data);temp=search(p,n,data);)if(!temp)printf("\n沒有這個(gè)數(shù)據(jù)\n");}voidchoose(node*p,intm,intn){switch(m){case1:{get(p,n);printf("\n表中數(shù)據(jù)為:\n");print(p,n);}break;case2:{inti;intx;printf("輸入數(shù)據(jù)\n");for(i=0;i<n;i++)24{scanf("%d",&x);p->a[i]=x;}}}}voidget(node*p,intn){inti;srand(time(0));for(i=0;i<n;i++)p->a[i]=rand()%100;intj,temp;for(i=0;i<n;i++)for(j=i+1;j<n;j++)if(p->a[i]>p->a[j]){temp=p->a[i];p->a[i]=p->a[j];p->a[j]=temp;}}voidprint(node*p,intn){inti;for(i=0;i<n;i++)printf("%d\t",p->a[i]);printf("\n\n");}intsearch(node*p,intn,intdata){inti,high,low,mid;inttemp=0;high=n-1;low=0;while(low<=high){mid=(high+low)/2;if(data<p->a[mid])high=mid-1;elseif(data>p->a[mid])low=mid+1;25else{printf("\n查找成功---數(shù)據(jù)為:%d",p->a[mid]);temp=1;break;}}returntemp;}//插入排序#include<stdio.h>#include<malloc.h>typedefstructNODE{inta[1024];}node;)voidchoose(node*p,intm,intn);//選擇自動(dòng)或者手動(dòng)輸入voidget(node*p,intn);//隨機(jī)產(chǎn)生函數(shù)voidprint(node*p,intn);//輸出函數(shù)voidinsertsort(node*p,intn);)main(){node*p;p=(node*)malloc(sizeof(node));intn;//數(shù)據(jù)總個(gè)數(shù)intm;)printf("輸入輸入的元素總個(gè)數(shù)n:");scanf("%d",&n);printf("\n1自動(dòng)輸入數(shù)據(jù):\n2手動(dòng)輸入數(shù)據(jù):\n");scanf("%d",&m);choose(p,m,n);insertsort(p,n);printf("排序后數(shù)據(jù)為:\n");print(p,n);}voidchoose(node*p,intm,intn){switch(m){case1:{26get(p,n);break;}case2:{inti;intx;printf("輸入數(shù)據(jù)\n");for(i=1;i<=n;i++){scanf("%d",&x);p->a[i]=x;}}}}voidget(node*p,intn){inti;srand(time(0));for(i=1;i<=n;i++)p->a[i]=rand()%100;printf("\n原數(shù)據(jù)順序?yàn)閈n");print(p,n);}voidprint(node*p,intn){inti;for(i=1;i<=n;i++)printf("%d\t",p->a[i]);printf("\n\n");}voidinsertsort(node*p,intn){inti,j;for(i=2;i<=n;i++){p->a[0]=p->a[i];j=i-1;while(p->a[0]<p->a[j]){p->a[j+1]=p->a[j];27j--;p->a[j+1]=p->a[0];}}}//選擇排序#include<stdio.h>#include<malloc.h>typedefstructNODE{inta[1024];}node;voidchoose(node*p,intm,intn);//選擇自動(dòng)或者手動(dòng)輸入voidget(node*p,intn);//隨機(jī)產(chǎn)生函數(shù)voidprint(node*p,intn);//輸出函數(shù)voidselectsort(node*p,intn);)main(){node*p;p=(node*)malloc(sizeof(node));intn;//數(shù)據(jù)總個(gè)數(shù))intm;printf("輸入輸入的元素總個(gè)數(shù)n:");scanf("%d",&n);printf("\n1自動(dòng)輸入數(shù)據(jù):\n2手動(dòng)輸入數(shù)據(jù):\n");scanf("%d",&m);choose(p,m,n);)selectsort(p,n);printf("排序后數(shù)據(jù)為:\n");print(p,n);}voidchoose(node*p,intm,intn){switch(m){case1:{get(p,n);}break;case2:{28inti;intx;printf("輸入數(shù)據(jù)\n");for(i=1;i<=n;i++){scanf("%d",&x);p->a[i]=x;}}}}voidget(node*p,intn){inti;srand(time(0));for(i=1;i<=n;i++)p->a[i]=rand()%100;printf("\n原數(shù)據(jù)順序?yàn)閈n");print(p,n);}voidprint(node*p,intn){inti;for(i=1;i<=n;i++)printf("%d\t",p->a[i]);printf("\n\n");}voidselectsort(node*p,intn){inti,j,k;for(i=1;i<=n;i++){k=i;for(j=i+1;j<=n;j++)if(p->a[k]>p->a[j])k=j;if(k!=i){p->a[0]=p->a[i];p->a[i]=p->a[k];p->a[k]=p->a[0];}29}}//冒泡排序#include<stdio.h>#include<malloc.h>typedefstructNODE{inta[1024];}node;voidchoose(node*p,intm,intn);//選擇自動(dòng)或者手動(dòng)輸入voidget(node*p,intn);//隨機(jī)產(chǎn)生函數(shù)voidprint(node*p,intn);//輸出函數(shù)voidbubblesort(node*p,intn);)main(){node*p;p=(node*)malloc(sizeof(node));intn;//數(shù)據(jù)總個(gè)數(shù))intm;printf("輸入輸入的元素總個(gè)數(shù)n:");scanf("%d",&n);printf("\n1自動(dòng)輸入數(shù)據(jù):\n2手動(dòng)輸入數(shù)據(jù):\n");scanf("%d",&m);choose(p,m,n);)bubblesort(p,n);printf("排序后數(shù)據(jù)為:\n");print(p,n);}voidchoose(node*p,intm,intn){switch(m){case1:{get(p,n);}break;case2:{inti;intx;printf("輸入數(shù)據(jù)\n");30for(i=1;i<=n;i++){scanf("%d",&x);p->a[i]=x;}}}}voidget(node*p,intn){inti;srand(time(0));for(i=1;i<=n;i++)p->a[i]=rand()%100;printf("\n原數(shù)據(jù)順序?yàn)閈n");print(p,n);}voidprint(node*p,intn){inti;for(i=1;i<=n;i++)printf("%d\t",p->a[i]);printf("\n\n");}voidbubblesort(node*p,intn){inti,j;for(i=1;i<=n;i++)for(j=i+1;j<=n;j++)if(p->a[i]>p->a[j]){p->a[0]=p->a[i];p->a[i]=p->a[j];p->a[j]=p->a[0];}}31通訊錄Choose=1choose=4choose=5choose=6choose=0)))))找不到,要加入數(shù)據(jù)清屏回到主主菜單//通訊錄0311102226#include<stdio.h>#include<malloc.h>#include<stdlib.h>#include<string.h>/*注:本課題中輸入的數(shù)據(jù)應(yīng)包括以下幾項(xiàng)信息:32姓名(NAME)學(xué)校(SCHOOL)城市(CITY)郵編(EIP)國(guó)家(STATE)*/typedefstructstudent{charNAME[30],SCHOOL[100],CITY[100],EIP[30],STATE[200];structstudent*next;}Student;voidmenu(Student*h);voidchoice(Student*h,intn);voidEnter(Student*h);voidDisplay(Student*h);voidSearch(Student*h);voidDelete(Student*h);voidSave(Student*h);voidLoad();voidinsert_data(Student*h);main(){Student*stu;stu=(Student*)malloc(sizeof(Student));if(stu==NULL){printf("\n內(nèi)存申請(qǐng)出錯(cuò)!\n");exit(1);}stu->next=NULL;while(1){menu(stu);system("cls");}}voidmenu(Student*h){charchoose;printf("**************************\n");printf("***通訊錄管理系統(tǒng)***\n");printf("**************************\n");printf("\n輸入相應(yīng)數(shù)字進(jìn)行選擇\n");printf("\n1輸入數(shù)據(jù)(從鍵盤輸入)\n2顯示信息\n3查找\n4刪除信息\n5存盤\n6裝入(.txt文件中的數(shù)據(jù)輸入到屏幕或者速到數(shù)據(jù)表中)\n0退出\n\n");scanf("%d",&choose);choice(h,choose);}voidchoice(Student*h,intn)33{switch(n){case0:exit(0);case1:Enter(h);break;case2:Display(h);break;case3:Search(h);break;case4:Delete(h);break;case5:Save(h);break;case6:Load(h);break;}}voidEnter(Student*h){charc='y';Student*p,*s;p=h->next;while(c!='n'&&c!='N'){insert_data(h);getchar();printf("\n是否繼續(xù)輸入數(shù)據(jù)\nn:停止輸入,任意鍵繼續(xù)輸入\n");scanf("%c",&c);}printf("\n");system("pause");}voidDisplay(Student*h){Student*s;s=h;if(h->next==NULL){charch;getchar();printf("\n表中無(wú)數(shù)據(jù)\n");printf("\n是否輸入學(xué)生的信息?\nY-----加入\n其他任意鍵繼續(xù)\n");scanf("%c",&ch);if((ch=='y')||(ch=='Y'))Enter(h);printf("\n加入成功\n");printf("\n");}34else{printf("\n表中數(shù)據(jù)為:\n");printf("NAMESCHOOLCITYEIPSTATE\n");while(s->next!=NULL){s=s->next;printf("%s\t%s\t%s\t%s\t%s\n",s->NAME,s->SCHOOL,s->CITY,s->EIP,s->STATE);}}printf("\n");system("pause");}voidSearch(Student*h){charName[30];inttemp=0;Student*s;s=h;if(s->next==NULL){charch;getchar();printf("\n表中無(wú)數(shù)據(jù)\n");printf("\n是否輸入學(xué)生的信息?\nY-----加入\n其他任意鍵繼續(xù)\n");scanf("%c",&ch);if((ch=='y')||(ch=='Y'))Enter(h);printf("\n");}else{printf("\n請(qǐng)輸入要查詢學(xué)生的名字\n");scanf("%s",Name);while(s->next!=NULL){s=s->next;if(!strcmp(s->NAME,Name)){printf("\n查找成功---學(xué)生信息為\n");printf("NAMESCHOOLCITYEIPSTATE\n");printf("%s\t%s\t%s\t%s\t%s\n",s->NAME,s->SCHOOL,s->CITY,s->EIP,s->STATE);35temp=1;}}if(temp==0){printf("\n沒有這個(gè)學(xué)生的信息\n");charch='n';getchar();printf("\n是否加入這個(gè)學(xué)生的信息?\nY-----加入\n其他任意鍵繼續(xù)\n");scanf("%c",&ch);if(ch=='y')insert_data(h);printf("\n");}}system("pause");}voidDelete(Student*h)//實(shí)現(xiàn)出去表中所有相同數(shù)據(jù)刪除{charName[30];inttemp;temp=0;Student*s,*q,*r;s=q=h;if(s->next==NULL){charch;getchar();printf("\n表中無(wú)數(shù)據(jù)\n");printf("\n是否輸入學(xué)生的信息?\nY-----加入\n其他任意鍵繼續(xù)\n");scanf("%c",&ch);if((ch=='y')||(ch=='Y'))Enter(h);printf("\n");}else{printf("\n請(qǐng)輸

溫馨提示

  • 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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論