軟件基礎試驗報告_第1頁
軟件基礎試驗報告_第2頁
軟件基礎試驗報告_第3頁
軟件基礎試驗報告_第4頁
軟件基礎試驗報告_第5頁
已閱讀5頁,還剩24頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、軟件基礎實驗報告作者: 日期:計算機軟件技術基礎實驗報告姓名:X XX班級:XX 0X01學號:30X05050XX實驗一線性表:1、建立單向鏈表,表長任意;2、可交互輸出單鏈表中的內(nèi)容;3、編寫算法計算出自己所建單鏈表的長度并輸出;4、 刪除自己所建單鏈表中的第K個結點,并將剩余結點輸出5、將單鏈表倒排,輸出結果。源程序如下:# in c lud e <stdio.h>#inclu d ev mall o c.h>t ypedef i n t da t aty p e;typed ef st ru ct no de/鏈表結構體 /dataty pe da t a;st r

2、uct no de *ne x t;li nk 1 ist ;li n k1 ist *creat 1 i s t()/ 建立鏈表/ /i nt x ;1 in k l is t * h e a d, *s ;he a d=N ULL;p r intf("n輸入鏈表數(shù)據(jù):");scan f ("%d",& x );whi 1 e (x!=0 )s=ma 1 lo c(si zeof( 1 in klist) ;/ 為鏈表開辟一系列的空間 /s-> da t a = x;s > n ext=he a d;head=s;p r i ntf

3、("n輸入鏈表數(shù)據(jù):");s can f(" %d" ,&x);re t ur n h e ad;v oid lis t Con te nt (li n k 1ist *h )/ / 輸出鏈表內(nèi)容 /link 1 ist * s;s=h;w hi l e (s!=NULL )p r int f ("%4 d ",s- > data);s= s -> n ext;i nt 1i stLon g (li n klist *h )/ / 計算鏈表長度 /i n t i=0;lin klist *s;s=h;while(s

4、!=N U LL )i+;s = s ->next;retur n (i);vo id DeleteN o de (li nk 1i st * h ,in t k)/ / 刪除第 K 個節(jié)點/ /in t i = 0;lin k li st *p, * q;P =h;i f (k=1)h = h>n ext;free (p);el s ew h ile(i< k -1&&p! =N UL L)i +;q=p;P =p->ne xt ;q >next=p- >n ext;fre e (p);lin k li s t *DaoXu(linklis

5、t *h )/ 逆序排列鏈表 / /l i nkli s t *r, *q p;r=h;p =r> ne x t;q=p->n ext;i f (h=NULL)p r i nt f("鏈表為空n ");while(q ! =N ULL&& h! = N U LL)p- > n ex t =r;r =p;p=q;q=q- >n ext;h-> n ext= N U L L;p _>next= r;return(p);ma in ()i n t k,x;linkli s t *h ;dopr i n tf ("n 功

6、能:n ");p r i nt f ("1 建立鏈表 n");pr in t f( "2.輸出鏈表內(nèi)容;n");printf("3.獲得鏈表長度n”);pr intf ("4刪除第K個節(jié)點n ");p ri nt f (" 5將鏈表倒序輸出n");p r intf(" 6 .退出 n");p r i ntf("請輸入功能號: n");scan f("%d", & x);if(x<1|x > 6)printf(&quo

7、t;錯誤! n");elseswitch(x)ca se 1: h= crea tl i st();break;c ase 2:list L o n g( h );break;c a s e3: pr in t f ("鏈表的長度是:%d", li stLong (h) ); break;ca se 4:p ri ntf("請輸入要刪除的節(jié)點:n");'sc anf("%d",&k);'Del e te Node (h,k);lis t Con tent (h ); b re a k; cas e 5

8、: h =DaoXu(h);listC onte nt ( h);b r e a k; cas e 6: e xit(O);bre a k;whil e (1 );運行結果:安箱跑5K骨也+ +姑烤丟出入輸入搭表數(shù)據(jù);為 輸入錯表數(shù)據(jù)目叫 輸入槌表數(shù)據(jù)沖弓 輸入槌表數(shù)據(jù):" 輸入錯耒數(shù)據(jù):騎 輸入鎮(zhèn)表數(shù)據(jù)個畫表燈倒 的:T7WW出人 3辟功2_* - - -fe'nr3 1 « -r-J 1號LZJ功二逢玄鏈表變出銹表內(nèi)容:|8|5%: 內(nèi)長代序# 衷奠眄倒能 槌餐量功 :立出出入 ' D:寶M牧祇仁豐4富權文陣夾8卵輕一巧實驗總結:1 .在編寫倒排鏈表的程

9、序時, 對于循環(huán)的計數(shù)的控制 沒有搞好,以致無法得到想要的鏈表;2.要給一個指針創(chuàng)立空間 之后才能調(diào)用它,否則會出錯。解決辦法:1.通過單步調(diào)試程序,調(diào)整循環(huán)次數(shù),來使循環(huán)中的 個參數(shù)達到自己想要的 通過查閱資料,完成對鏈表程序的實現(xiàn)。在寫每一個子函數(shù)時,常常會遺漏小的判斷條件, 比如遺漏了判 斷是否為空等;還有就是在對指針操作時,有時多加了 *,或者分號寫成逗號;在調(diào)試程序的過程中有很多小的錯誤或者判斷條件錯 誤等。實驗二在交互方式完成下列任務:1、動態(tài)交互建立二叉樹,結點個數(shù)任意;2、分別用DLR LD R、LR D三種方式對二叉樹進行便 利并輸出結果;3、計算二叉樹中的結點個數(shù)并輸出;4

10、、計算二叉樹的深度并輸出;源程序如下:# incl u de "stdio . h"# inc 1 ud e " mal 1 o c .h"str uct BTN odei nt d a t a ;?struc t BTN o de *Lch i1 d,*R c hi 1d ;st ruct BTNode *build(st r uc t BTNode *p);s truct B TN o de *creatren t (struc t BTNod e *p );void DLR(s tru c t BTNode *T);str uct B TNode

11、*crea tr ent(st r uc t BTNod e *p)?int x;? pr in t f("輸入根:rent=");?scanf ("% d", &x);? if(x= 1 00 0 ) p=N U LL; e Ise?p-> da ta=x;p> Lch i I d =(s t ru ct B TNod e *) malloc(s i zeof(st ructBTNode);?retup- > Rchil d =( s tr u ct BTNod e *) i f ( p=NULL) r eturn p ;p&

12、gt; Lch il d =build(p >Lchi p->Rchil d =build( p >Rch il d ); r n p;m a lloc(size o f (struc t BT Node);Id ); st r? iuctBTNo de *bui 1 d( s truct BTN o d ent x ;printf("輸入數(shù)據(jù)(輸入值為時,表示該結點為空 sc a nf ("%d", &x);? i f(x= 1 0 00) p =NUL L;e lsep >d at a = x;? p -> L child

13、= (s tru ct BT N o de *)m al loc( s p >Rch ild = (st r uct B T Nod e *) m all):v a lue=");?i zeof( s true t oc (sizeof(s trBTNode); u ct B TNod e );? i f( p =NULL) r etur n p;p >Lchi1 d= build(p- > Lch il d); p->Rchild= b u i ld(p- > Rchi ld);?retur n p;?vo i d DLR(st r u ct BTNo

14、d e *T)i f(T= NU LL)r et ur n;p r int f ("% d ", T->da t a );DLR(T > Lch ild);DL R(T >Rc h i ld);v o i d LD R(s tr u ct BTNod e *T)?if (T =NUL L) r et u rn;LD R (T->L chi 1 d)?pr i ntf("%d", T ->da t a);LDR(T >Rchi 1 d);voi d LR D(s t rue t BTN o d e * T )?i f (

15、T= =N ULL) re turn ;?LRD (T >Lc h i1 d);?LRD(T->R h i1 d);? p r i ntf(” %d ",T->dat a );v o id m ai n ()?struct B TNo d e * r en t =NU LL;int flag ;?while( 1 )?n3 :后序 n ")BT Node);? prin t f("選擇輸入的操作:n 1:創(chuàng)建2 :先序n 3:中序 ?scanf("%d",& f la g );?swi tch (flag)? case

16、 1 :? r ent=( s truc t B TNode *) ma lloc( s iz e of( s t r uct rent=crea tr en t(rent);?br e a k;ca se 2 :DLR(rent) ;printf("n " ) ;br e ak ;case 3: LDR ( r en t);p r intf ("n") ; br e ak ;?ca se 4:L RD(re n t);pri nn") ;b re ak;運行結果: 5 AUserspU«5ktopL>ebug-,e11入入入入

17、入擇 心刖一 刖 j 刖.!_刖. .刖II刖.1-!.-刖4 刖.>._ 刖二刖二- :為旺為 Hl 000! 為 0- 1000. jAjK1000l, 為辰,為 H10H0P為詁 為氐為* 1她,1000,1000,1000,1080,1Q00,寛工空空空空空空空工 占善苦苫苫RE占苫苫警小 士 口士 口士 口士口士 口士 口士 口士口士 口士 n 該該閡雷囂該該該 丞疋示示-巫血亦示示亦 Ex ulKjllyEv.-.-V my _!/_<valiia=l vlue=10B0 u alkie =2=5 ualue =1000 uluc =10B0 ualue=1000MAl

18、ue -8 value=1000 value=100H總作操石的衛(wèi)彎先中后8 B8.liI I實驗總結:通過查資料完成程序,還是在調(diào)試程序的過程中出 現(xiàn)恩多的錯誤,除了一些基本的錯誤,也出現(xiàn)了判斷錯誤,比 如在進行先序,中序,后序遍歷的時候都沒加 i f ( k ! = NU LL)這個條件,導致在執(zhí)行程序的時候,只進行先序遍歷,然后 出錯退出。實驗三在交互方式下完成下列任務:1、根據(jù)教材上算法,完成圖的深度和廣度優(yōu)先遍歷,要求任意給定起始 點,輸出結果;2、根據(jù)教材上算法,完成圖的單源最短路徑的算法,要求任意給定源 點,輸出結果;源程序如下:#i n cl ude <stdi o .

19、h># in elude <m a llo c. h> #def i ne Q 1 0 0 0 #d e f i ne VNum 6st r u ct GLink i nt No; in t Right;s t ruct G Lin k *Relat; ;int G VNu mVN um =/對圖進行初始化/ /0 ,23,1 6 ,Q , 4 5 , Q,? Q,0,15,5 0,1 0 ,Q,2 0,Q,0,1 5,Q,Q,Q,2 0,Q, 0,35,Q,? Q,Q,Q,30,0,Q,Q,;5 ,4,Q,Q, 0s truct GLink * GLV Num;int Vi

20、sitedVNum;v o id Creat e GLink(int GVNumVNum ) 立鄰接表/ in ti, j;f or ( i =0 ;i v VNum; i +)GLi =q = NULL;fo r (j = 0 ; j v VNu m ; j+)if (i != j)if (Gi: j > 0 )&&(Gij < K)/點存在有向路徑/? p = (struc t GL ink *)m al loc(si z eof(struct G L in k );p->N o = j ;/ /點加入鄰接表/?p->Righ t = G i: j;

21、if (GLi = = N ULL)?GLi = p;?e l se?q >Re l a t = p;q = p;/建該兩將該s tru ct G L i n k * p,* q;vo id DF S(int A V Nu m VN um, int V) 起點/用于進行深度優(yōu)先遍歷的子函數(shù) ,是訪問/ /int i;print f (" %d ", V);Vi s ited : V = 1;/for (i =0; i < VN um; 1 +)i > 0)&& (A Vi v K) && (V isite/將其標記為已1 f

22、 (A V/該結點未被訪問過d 1 != 1)DFS(A,);/訪問該點/ /fo r (1= 0;1 f (Visitedi 點,訪問該點/i < V Num; 1 +)=1) DFS(A,i);/ /仍有未必訪問過的/用于廣度v o 1 d B FS(int AVN u m VNum , i n t V)優(yōu)先遍歷的子函數(shù)/1 nt CQVNum ;i n t a 0,b,c;int i,k= 1;fo r (i= 0 ;i<VNu m ;i+)CQ1 =K;Vi s i t ed V = 1;志為訪問過/CQ : 0 = V;pr 1 ntf(" %d ",

23、V);放入隊列/while(k v 6&& a <k) /仍有結點未被訪問并且隊列中仍有結點的后繼結點未被/ 標/ /將該結點訪問/b=C Q a ;for(c=0 ; cv VNum; c+ +) /依次將隊列中以結點為首的鄰接表中的結點插入隊列/ /f(Vis 1 ted prCQ:c=0&& A b c<K&& A b c! = 0) in t f ("%d", c);+ +k=c;/未被訪問過,將其插入到隊列中/Vi sit ed c =1;/標志為訪問過/a+ ;for(i=0;if(V 1 sit e

24、di=0) BF S(A, i);v VNu m;i+)void Short(i nt A V NumV N um , i nt V) 起點/用于計算最短路徑的子函數(shù),V是 int Di s tV N um, PathVNum;int S = 0;in t i, k ;i n t wm, u;for (i=0;i<VN um; i + +) D ist i = A Vi;/ /默認這兩點之間即為最短路徑/if (Dis t i< K)P athi: = V;/ 存儲該路徑/S = S 1(1 VV V);for ( k = 0 ; k<VNum;k+ +) wm = K;u

25、= V;fo r (i = 0; iv VNu m; i + +)if (S &(1 v < i)=0)&& (Dis t : i < wm)/該兩點間存在路徑/ /u = i;?wm = Di st i;S = S |(1 << u);fo r (i = 0; iv VNum; i+)if (S &(1 v < i)=0) & & (D istu: + A ui) v Disti)? Di s t : i = Distu + Au: i ;/ 找到新的最短路徑 /P at h i = u;/更新路徑長度/? f o

26、 r (i = 0 ; iv VN um; i+)/ 輸出該源結點到其他各點的最短路徑 /if (S &(1 << i)!= 0)k = i;w h i le ( k !=V) p r in t f("%d <-" , k);?k = Pat hk;print f (" %d ",V);p r i n t f("n ", Di sti);else pr i ntf (" No Path : %d", i);m a in ()in t i, j, a, b;Cre ateGLin k (G)

27、;p rint f ("1 .深度優(yōu)先遍歷n");/打印菜單/ /p r i nt f ("2.廣度優(yōu)先遍歷n");p ri ntf(" 3 尋找單源最短路徑n ")prin tf ("4 .退出 n");/w h ile(1)完成菜單功能/ / print f ("n功能項選擇從1到 4 :”);scanf ("% dH, & a );if (a=1) fo r (i=0; i <VNum; i + +)Vis i ted : i = 0;?printf(”請輸入第一個節(jié)點:&q

28、uot;);sc anf("%d", & j);?prin t f n 深度遍歷 DFS是:");DFSG ,j);p rint f( "n");i f (a=2) fo r (i= 0 ; i<VNum ;i+)Visite d i = 0 ; p rin t f ("請輸入第一個節(jié)點:");?sc anf(” d ",& j);?p rint f ("n廣度遍歷結果 BFS是:");?BFS(G,j);pr in tf("、n");? if (a=3

29、)printf("請輸入源點:");sc a nf(" %d", &b);p r int f(" n單源最短路徑是:n ");Short(G,b);if (a= =4) bre a k;運行結果:jgDebugkjh7.exc,-search the grapl deejj first.sedvch tliebt'oad fIvst-find the sfiortest path-exitplease inyut a nun from 1 to 4 :1lease ih)put tlie F±Vst node

30、 = 1The Result of DFS is: 112 J 101413 J 51jpljease 土npu七 己 num £rom 1 to 4 :2lease infjut tlie first node = 1The Result of BFS is:lJ 12J 3J<J0J5Jplease input a num front 1 tn 4 S 3input tlie source node ;1The Eesult of Shortest path is:M <-2 <-1= 351 = 62 <-1= 153 <-2 <-1= 30

31、4 <-1- 10Ng Path - Eplease input a nunt From 1 to 4 ;4i*bss ani/ kev to continue實驗總結:1運行時,2表示深度優(yōu)先遍歷,3表示廣度優(yōu)先遍歷。碰到問題:1 建立圖時,指針太過復雜,很容易搞混;2.廣度優(yōu)先遍歷中,編寫時,打印輸出進入死循環(huán);解決辦法:1 通過單步調(diào)試找出賦值錯誤和參數(shù)調(diào)用的錯誤;2.將函數(shù)中的q移位移到循環(huán)外面。實驗四檢索和排序在交互方式下完成下類任務:1、任意給定無序系列,用快速排序法對其進行排序,并統(tǒng)計交換次數(shù)。2、任意給定的無序序列,用對半檢索法交互檢索任意給定的關鍵字K EY3、任意給定

32、無序系列,用冒泡排序法對其進行排序,并統(tǒng)計交換次數(shù)和排序的趟數(shù)源程序如下:# inclu d e vs td io .h>i n t ji;vo id duiban(int a10,i n t n,in t ke y)i nt l ow , h ig h ,mid ,f lag ;1 ow=0; h igh= n -1 ;flag=0;while( 1 ow<=hi gh)m id=(low+ h igh)/2;if(a mid = = key ) flag=1; b re a k;e lse i f( k e y >am i d) 1 ow= m i d+1 ;el se

33、high=mi d -1;i f(flag = = 1) pri n tf("是第 %d 個元素 ”,mid+1);el se pri n tf ("E");v oid mao pa o(i n t a10 ,in t n ) in t i,j,temp, f la g;int jia o ,ta n g;j i a o=ta n g=0;f o r (i= 0; i<n-1 ;i+ +) f la g=1;for(j=0;j<n_i_ 1; j+)?if (aj+1: <aj) tem p = aj:; a: j = aj+1;a : j+1=

34、t e mp;jiao + +;fl a g=0; ?ta ng+;if(fla g = 1 ) b r eak;pri nt f(”n交換次數(shù)和排序趟數(shù)分別是%d和小",jiao,t a ng);vo id kuaishu (i nt a : 10 ,in t 1 ow,i n t h ig h )in t i,j;i nt t e mp;i f (lo w>=h i gh ) retu rn(0);i=low;j=hig h ;temp=ai ;while(i! = j) w hil e(a j> = temp ) &&(j>i ) j;if(j

35、 > i) a i+= a j;ji+; while(a i <=temp)&&( j >i ) i + +;i f( j >i) a :j - -=ai;ji + +;ai = temp;kua is hu(a,low,i 1);kua i shu ( a ,i+ 1,h i gh);ma in ()int i, a 10,n, q, p, t ;d o prin t n 1用對半檢索法,交互檢索任意給定的關鍵字KEY");pri n tf (" n2用快速排序法對其進行排序,并統(tǒng)計交換次數(shù)");p ri ntf( n 3

36、.用冒泡排序法對其進行排序,并統(tǒng)計交換次數(shù)和排序的趟數(shù)");pr in plea se w rite th e or der:");scan f("%d", & i);w h il e(i< 1 |i>4)prin t f(" n T h e o r d e r you print is wron g! ,please print a g ai n!"); scan f("%d" , &i);swi t ch (i) cas e 1:p r int f ("please i n

37、 put : n");f o r( n =0;nv 10; n+ +) s ca nf (" %d" ,&an);dop ri n tf ("please inp u t : n");?sc anf ("%d",&q);duiban (a ,10, q);?p r int f (":n ");? sc an f ("% d ",& p );?while( p =1);b r eak ;case 2: j i=0;?pri n tf(" pl ease i np u t d a

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論