狠狠干影院/欧美午夜电影在线观看/高黄文/国产精品一区二区在线观看完整版

數(shù)據(jù)結(jié)構(gòu)實驗報告全集

| 瀏覽次數(shù):

 數(shù)據(jù)結(jié)構(gòu)實驗報告全集

 實驗一

 線性表基本操作與簡單程序

 1。

 實驗?zāi)康?(1)掌握使用Visual C++ 6、0 上機調(diào)試程序得基本方法;)2( 本基得表性線握掌?操作:初始化、插入、刪除、取數(shù)據(jù)元素等運算在順序存儲結(jié)構(gòu)與鏈表存儲結(jié)構(gòu)上得程序設(shè)計方法。

 2. 實驗要求 (1)

 認真閱讀與掌握與本實驗相關(guān)得教材內(nèi)容。

 (2) 認真閱讀與掌握本章相關(guān)內(nèi)容得程序. (3)

 上機運行程序。

 (4)

 保存與打印出程序得運行結(jié)果,并結(jié)合程序進行分析. (5)

 按照您對線性表得操作需要,重新改寫主程序并運行,打印出文件清單與運行結(jié)果 實驗代碼:

 1) ) 頭文件模塊

 #inc l ude i ostr eam 、h h 〉 // 頭文件

 #inc lud d e< mallo c、h h 〉 // 庫頭文件- - - -- — 動態(tài)分配內(nèi)存空間

 typ ed d ef f

 i nt e lemtype ; // 定義數(shù)據(jù)域得類型

 typede f

 str uc c t

 linknode// 定義結(jié)點類型

 { {

 e e l em t ype dat a; // 定義數(shù)據(jù)域

 s tr u ct l ink no o de e

 *next ; // 定義結(jié)點指針

 }n od d e type ;

 2) 創(chuàng)建單鏈表

 n od e ty p e * cr e ate ()/ / /建立單鏈表, , 由用戶輸入各結(jié)點 a data 域之值, ,

 / / /以 0 0 表示輸入結(jié)束

 {

 elem t ype d ; // 定義數(shù)據(jù)元素 d d

 nodetype * h=NU LL L , *s ,*t t ; // 定義結(jié)點指針

 i i nt

 i=1;

 cou t <<" 建立一個單鏈表”〈 <end l;

 whi le e (1 1 )

 {

 cou t

 <<”

 輸入第”〈〈

 i < 〈" " 結(jié)點 a data 域值:”;

 c c i n 〉>

 d;

 if(d d ==0 )

  bre ak k ;/ / /以 0 0 表示輸入結(jié)束

 if (i i ==1)// 建立第一個結(jié)點

 {

 h h =( nodetype * )m al l l oc(sizeof( n odetype)) ; // 表示指針h

 h h- - >d a ta=d ;h h —> > ne xt =N UL L;t =h; //h就是頭指針

 } }

 els e/ / /建立其余結(jié)點

 { {

 s =( n odet y pe*) ma l loc(size of f ( node t ype )); ;

 s- - >d ata = d;s — 〉 next =NU U LL ;t — >nex x t =s ;

 t=s ;t //t 始終指向生成得 單鏈表得最后一個節(jié)點

 }

 i++ ;

 }

 retu r n h ;

 } }

 3)輸出單鏈表中得元素

 v oi d

 disp ( node ty y pe*h h )// / 輸出由h指向得單鏈表得所有daa ta 域之值

 {

 nodetype * p=h;

 c ou t< < 〈”輸出一個單鏈表 :"<<en dl<<”

 ";

 i f(p== N ULL )co o ut t <〈”空表”;

 w w h il e( p!=NU LL L )

 { {

 cou t< < 〈p p- - 〉da ta< 〈 ”

 ” ;p p =p- - 〉 nex t;

 } }

 c c out t 〈<e e n dl ;

 }

 4 4 )計算單鏈表得長度

 int len(no de ty p e *h)/ / /返回單鏈表得長度

 {

 int i =0 0 ;

 nod d e type * p=h;

 w w h ile(p!=NULL)

 { {

 p p =p p — 〉 nex t ;i++ ;

 }

 re t urn

 i i ;

 } }

 5) 尋找第 i i 個節(jié)點

 node type

 *find(n o detype *h , int i)// 返回第 i i 個節(jié)點得指針

 { {

 nod e ty pe e

 * p=h ;

 i nt j=1 1 ;

 if(i 〉l en ( h) | |i<=0)

 r r e tu r n NUL L;/ / /i i 上溢或下溢 c c

 els e

 { {

 while ( p!=N ULL && & j〈 1)// 查找第 i i 個節(jié)點,并由 p p 指向該節(jié)點

 { {

 j++; p =p- - 〉 next ;

 } }

 retu rn

 p;

 }

 } }

 6 6 )單鏈表得插入操作

 n n ode e ty pe * i ns ( node typ p e

 * * h ,i n t i , el e mtype

 x x )// / 在單鏈表he e ad d 中第 第 i i 個節(jié)點

 // / (i i 〉 =0) 之后插入一個 dat a域為 x x 得節(jié)點

 { {

 nodetype * p,* s;

 s =(nodet y pe *) ) m alloc(si z eo f( no d ety pe e )); // 創(chuàng)建節(jié)點 s s

 s s — 〉d ata =x;s- >n e xt =N N U LL;

 i i f( i==0)//i=0 :s s 作為該單鏈表得第一個節(jié)點

 { {

 s — >nex t =h ;h =s;

 }

 else

  { { p= fi nd(h h ,i i )

 ;// 查找第 i i 個節(jié)點,并由 p p 指向該節(jié)點

 if (p p !

 =NUL L) )

 {

 s- - > next=p — 〉 ne xt t ;

 p p — 〉 next=s;

 } }

 r r e tu r n h;

 }

 } }

 7 7 )單鏈表得刪除操作

 node t ype * del ( nod et yp e

 *h, int

 i)// 刪除第 i i 個節(jié)點

 { {

 n odetype * p=h, *s s ;

 int

 j= = 1; ;

 if (i= = =1 1 )//刪除第 1 1 個節(jié)點

 { {

 h= = h — >next ; free ( p) ;

 } }

 else

 { {

 p p = fi n d(h ,i i -1 1 )

 ;// 查找第i-1 1 個節(jié)點, , 并由 p p 指向該節(jié)點

 if(p != = NU U LL L & &p - >n e xt !

 =NULL)

 {

 s =p -〉 next; //s s 指向要刪除得節(jié)點

 p p - >ne x t=s - >n e xt ;

 free ( s) ;

 } }

 el l se

 co ut t < <" 輸入i得值不正確 ”< < < endl ;

 }

 retu r n h; ;

 }

 8 8 )釋放節(jié)點空間

 void di spose (n n o det y pe *h)// 釋放單鏈表得所有節(jié)點占用得空間

 {

 no de typ e

 *p p a =h ,*p b;

 if (pa a !

 =N UL L L) )

 { {

 pb=p a — 〉n ex t; ;

 if(pb= = NUL L)// / 只有一個節(jié)點得情況

 fr e e( p a);

 el s e

  { {

 whil e

 (p p b!=N N U LL )//有 兩個及以上節(jié)點得情況

 { {

 f f r ee ( pa );p p a =pb ; pb = pb — 〉 next ;

 } }

 free ( pa);

 }

 }

 }

 9 9 )主程序模塊:

 #i nc l ud e"s s li i nk、h h ”/ / /包含頭文件 sli nk k

 v v o id main( )

 {

 no de e typ p e

 * hea d; // 定義節(jié)點指針變量

 he ad d =c c reat(yī) t e () ; // 創(chuàng)建一個單鏈表

 disp ( hea d); ; // / 輸出單鏈表

 c c out 〈 <" 單鏈表長度:”< < 〈l en(h ead d )〈〈end d l;

 ins( h ea d, ,

 2,0 ); // 在第二個節(jié)點之后插入以 0 0 為元素得節(jié)點

 d isp ( he a d) ;// / 輸出新鏈表

 del (h h e ad ,2) ) ;/ / /刪除第二個節(jié)點

 d isp(he ad d ); // 輸出新鏈表

 }

 5 5 。實驗結(jié)果

 建立一個單鏈表:

 輸入第 1 1 結(jié)點 d d aa ta 域值:1 1

 輸入第2結(jié)點 dat a域值:2 2

 輸入第 3 3 結(jié)點da ata 域值 :3

 輸入第 4 4 結(jié)點 da ta a 域值:4

 輸入第 5 5 結(jié)點da a ta域值 :5

 輸入第 6 6 結(jié)點 d d at(yī) t a域值:6

 輸入第 7 7 結(jié)點 d d at(yī) t a域值:7

 輸入第 8 8 結(jié)點 d d aa ta 域值:8

 輸入第9結(jié)點 d d at(yī) t a域值: : 9

 輸入第 0 10 結(jié)點 a data 域值0: :

 輸出一個單鏈表:

 1

 2

 3 4 5 6

 7 8

 9

 單鏈表長度:9 9

 輸出一個單鏈表:

 1 0

 2

 3 4

 5

 6

 7 8

 9 9

 輸出一個單鏈表:

 1 2 3 4 5 6 7 8

 實驗二

 順序棧得實現(xiàn)

 1、實驗?zāi)康?掌握順序棧得基本操作:初始化棧、判棧空否、入棧、出棧、取棧頂數(shù)據(jù)元素等運算以及程序?qū)崿F(xiàn)方法。

 2、實驗要求 (1)

 認真閱讀與掌握與本實驗相關(guān)得教材內(nèi)容。

 (2) 分析問題得要求,編寫與調(diào)試完成程序. (3)

 保存與打印出程序得運行結(jié)果,并分析程序得運行結(jié)果。

 3、實驗內(nèi)容

 是就號括方、號括圓含包中式達表術(shù)算斷判個一現(xiàn)實作操本基得棧用利?否正確配對得程序。具體完成如下:

 (1)

 定義棧得順序存取結(jié)構(gòu)。

 (2) 分別定義順序棧得基本操作(初始化棧、判棧空否、入棧、出棧等)。

 (3)

 定義一個函數(shù)用來判斷算術(shù)表達式中包含圓括號、方括號就是否正確配對。其中,括號配對共有四種情況:左右括號配對次序不正確;右括號多于左括號;左括號多于右括號;左右括號匹配正確.

 (4)

 設(shè)計一個測試主函數(shù)進行測試。

 (5) 對程序得運行結(jié)果進行分析. 實驗代碼:

 #include <stdio、h >#?001 eziSxaM enifed?

 typedef struct {

  int data[MaxSize];

  int top; }SqStack; void InitStack(SqStack *st)

 //初始化棧 {

  ;1—=pot>—ts?}?int StackEmpty(SqStack *st) //判斷棧為空 {

  ;)1—==pot〉-ts( nruter?}?void Push(SqStack *st,int x)

 //元素進棧

 ?{pot>-ts(fi?==MaxSize-1)

  printf("棧上溢出!\n");

  esle?{

 ?

 st-〉top++;

  ;x=]pot〉-ts[atad>-ts?

 } }?void Pop(SqStack *st)

 //退棧

 ?{)1-==pot>-ts(fi?

  ;)”n\出溢下棧"(ftnirp?

 esle?

 st—〉top-—; } int Gettop(SqStack *st)

 //獲得棧頂元素 {

  if(st—〉top==—1) {

 ?

 printf("棧空\n”);

  ;0 nruter?

 }

  esle?

 return st—〉data[st->top]; } void Display(SqStack *st)

 //打印棧里元素 {?

 int i;

  printf("棧中元素:");

  for(i=st-〉top;i>=0;--i)

  d%”(ftnirp?",st->data[i]);

 ?;)"n\”(ftnirp?}測// )(niam tni?試

 ?{ ;L kcatSqS?

 ;L&=ts* kcatSqS?

  nI(lǐng)?itStack(st);

 ;))ts(ytpmEkcat(yī)S,"n\d%:空棧"(ftnirp?

  )i++;01<i;1=i tni(rof?

  ;)i,ts(hsuP?

  ;)ts(yalpsiD?

 printf("退一次棧\n");

  Pop(st);

 printf("棧頂元素:%d\n",Gettop(st));

 poP?(st);

  ;)ts(yalpsiD?

  ?};0 nruter?

 實驗結(jié)果: :

  實驗三

 串得基本操作與簡程序 1。

 實驗?zāi)康?掌握串基本操作:初始化、聯(lián)接、替換、子串等運算在堆分配存儲儲結(jié)構(gòu)上得程序設(shè)計方法。

 2. 實驗要求 (1) 認真閱讀與掌握與本實驗相關(guān)得教材內(nèi)容. (2)

 認真閱讀與掌握本章相關(guān)內(nèi)容得算法并設(shè)計程序序。

 (3)

 上機運行程序。

 (4) 保存與打印出程序得運行結(jié)果,并結(jié)合程序進行分析. 實驗代碼:

 #inc l ude < stdio 、 h>

 #d d e fi ne e

 MaxSize 50

 type d ef s tr u ct

 { {

 ch h a r data [ Ma x Siz e];

 // 存放字符串

 int len gth h ;

 // 字符串 長度

 } SqString ;

 // / 將一個字符串常量賦給串 s s

 v oid

 StrAssign(SqStr in n g

 & s,c ha a r

 c c st r[])

 {

 i i nt t

 i i ;

 for(i =0 ;cstr[i] != = '\ \ 0";i++ )

 // / 這個'\ \0 0 '代表字符串結(jié)束標志, , 編譯系統(tǒng)自動加上得

 s、 dat a [i]=cs t r[i];

 s s 、 len g th=i;

 }

 // 字符串得復(fù)制

 vo i d StrCopy (S qStr i ng & s,S S q St ri i n g t)

 { {

 in t i;

 f or(i=0;i 〈t、le ngt h;i ++ )

 s s 、d at a [i ] =t 、 data[i];

 s、 le n gt h =t 、l ength ;

 p p r intf( "字符串復(fù)制成功了\ \n n ”) ) ;

 }

 // 判斷字符串就是否相等

 vo id

 St rEq q ual (SqStrin g

 s s ,S qSt ri i n g t)

 {

 in t

 i, s ame= 1;

 if f (s s 、l ength!=t 、l en g th )

 sa m e=0 ;

 el se

 { {

 for ( i= 0;i i 〈s s 、 length;i++ )

 if( s、 da t a[i ] !=t 、da a ta a [i])

 {

 sa me e =0; ;

 b reak ;

 }

 }

 i f(same==0 )

 p p rin n t f(" 這兩個字符串不相等\ \ n" ); ;

 else

 p p r int f(" " 這兩個字符串相等\ \n n ”) ) ;

 }

 / / /字符串得長度

 v oid

 S trLe n gth(SqS tr r in n g

 s)

 {

 pr in n tf f (”此字符串長度為:%d d\ \ n" ,s s 、l eng th) ); ;

 }

 // 合并字符串

 Sq S tring Co n cat (S qString

 s,Sq S tring t)

 { {

 S qS t ri ng

 s s tr;

 in t

 i i ;

 s tr 、len gth=s 、 len g th+t 、l en gt t h;

 for( i =0 ;i<s s 、 le n gt h; i++ )

 str 、d d at(yī) t a[ i] =s s 、 data [i i ]; ;

 for(i=0 ;i〈t t 、l eng t h;i++)

 str 、da a ta a [s、l l e ngt h+ i] =t、 data [ i];

 r eturn str;

 }

 / / /求子字符串

 void

 SubStr (SqS S t ri n g s , in t

 i i ,in n t

 j)

 {

 SqStr i ng

 st t r; ;

 i nt

 k;

 s tr 、l l e ngth= 0;

 i i fi (i 〈 =0| |i i〉 〉s s、 、 len gt t h| | | j<0 | |i+j- - 1> > s、le e ngth)

 pri n tf(" 子字符串復(fù)制失敗\n n " );

 f or ( k= i- 1;k 〈 i+j- -1 1 ;k k ++ + )

 st r、 dat a[k k —i i + 1]=s 、d d a ta[k] ;

 str r 、l ength=j;

 printf (" " 子字符串復(fù)制成功

 長度為:

 %d\ \ n", , j) ) ;

 print f( ( ”下面輸出此子字符串:\n n " );

 f f or r (i =0 ;i <j ; i++)

 pr r i ntf ( ” %c ”, st t r、 data[i ]) ) ;

 printf(”\ \ n ” );

 } }

 // 插入字符串

 S qStri n g I ns e rStr(S q Str ing

 s1 , int i ,SqS tring s 2) )

 { {

 int

 j j ;

 Sq S tr in n g

 str;

 s tr 、 length=0 ;

 if (i i 〈= = 0|| i>s1 、 length+1)

 { {

 pr in n tf f (”字符串插入失敗\ n" ); ;

 retu rn

 str ;

 } }

 for ( j= 0; ; j <i- -1 1 ;j ++ )

 str r 、d ata [j]= s1 、da ta[j];

 for ( j=0; j〈s2 2 、 lengt h; j++)

 s s tr r 、 dat a [i- -1 1 +j j ] =s2 、d d a ta[j ];

 fo o r(j =i -1 1 ;j j <s s 1、l eng t h;j++ )

 st r、 da ta [s2 、le ngt h +j] = s1 、 data [ j];

 s tr 、 leng th h =s1 1 、 le ng th+ s2 2 、l l eng th ;

 printf (" " 插入字符串成功

 長度為:%d d\ \n n ”,s s tr、length );

 retu r n st r; ;

 }

 / / /刪除字符串

 Sq St ring DeleS tr( SqStri n g s , int i,i nt

 )

 j)

 {

 i nt k ;

 Sq q St t ri i ng

 s s tr r ;

 s s tr r 、l eng th h = 0;

 if ( i<=0 | |i>s 、l engt h|| i+ j >s 、 le ngt h+1)

 { {

 pri i n tf ("字符串刪除失敗\n n ”);

 return

 str;

 }

 f f o r( k= = 0;k k 〈i i- - 1;k k + +)

 s tr 、 da t a[k] =s s 、d ata [k ];

 for ( k=i+j — 1;k〈s、 leng th h ; k++ )

 s s tr、 data [k k -j ]=s 、 data[k ];

 str 、 length=s 、l l e ngt h —j j ;

 pri nt t f( ( "刪除子字符串成功

 剩余長度為: : %d d \ n", str 、 le n gth) ;

 re tu rn str ;

 }

 // 替換字符串

 voi d

 R R ep Str(SqStr in g s,int

 i i ,i nt t

 j, S qString t)

 {

 i i nt t

 k;

 S S q String

 str ;

 s s tr、 length =0 0 ;

 if(i<= 0||i i 〉s s 、 le ng th||i+j — 1>s 、l eng th h )

 pri n tf (" " 字符串替換失敗了\ \n n "); ;

 f or(k=0;k <i i — 1; k++)

 st r、 dat a [k ]= = s、dat(yī) t a[k k ];

 for( k =0;k<t 、l l eng g th h ;k++ + )

 str 、d at a[ i+k- - 1] =t 、d d a ta [k k ];

 for ( k=i +j j -1; ; k <s 、 le n gt h; k++)

 str 、d d ata [t t 、l ength+k- -j j ] =s 、d ata[k ];

 s s tr、l l eng g t h= s、 le n gth- - j+t 、l l en n g th ;

 p p ri i nt t f( ( ”替換字符串成功

 新字符串長度為:%d d\ \n n ”,str 、 length) ;

 }

 // 字符串得輸出

 v oi d

 Di sp St r( SqString

 s s )

 {

 int i ;

 if( s、le ng t h> 0) )

 {

 pri nt f("下面輸出這個字符串\ \n n ") ) ;

 fo r(i i = 0;i 〈s s 、le e ng g t h;i ++ + )

 p p r intf(" %c",s、 da ta a [i]); ;

 p ri nt t f( ( "\ \ n”) ;

 } }

 el se

 print f("目前空字符串

 無法輸出\ n" );

 } }

 void main ()

 { {

 S S qS S t ring s;

 char a [] ] ={ { " we n

 x x ian n

 lia ng "} } ;

 // 字符串常量a

 St t rA ssig n(s s ,a);

 D ispS t r(s );

 StrL engt t h( s);

 SqS tr r i ng

 s s 1 ,s2,t;

 1 //s1 就是待復(fù)制得字符串變量

 pr r in n t f(" 請輸入一個字符串t:\ \n n ”) ) ;

 scan f (" %s s " ,t 、d ata );

 StrAssi gn (t ,t t 、da a t a);

 S S t rCopy(s1 ,t t );

 // 復(fù)制字符串

 St t rLe e ngth h ( s1) ;

 Dis p Str(s 1) ) ;

 p p r in t f("串 下面判斷字符串 1 s1 串 與字符串 s s 就是否相等\n");

 StrE q ua l(s s , s1) ;

 printf(" 下面將字符串 s s 1與字符串 s s 合并一起\n" " );

 Sq S trin g

 str ;

 s s tr r = Concat(s , s1) ;

 // / 合并字符串

 DispStr(st r);

 StrLength ( str) ;

 Su u bS tr ( st r, , 22 2 ,7 7 );

 / / /求子字符串

 s s tr r = DeleS tr (st r ,1 5 ,4) ;

 // 刪除字符串

 Di i sp p S tr ( st r); ;

 St r Length ( str) ;

 p p ri i n tf(" 請插入一個字符串s2 2\ \n n ”); ;

 s s c anf ( ” %s ",s 2、d d at(yī) t a) ) ;

 St r As s ig n (s2, s2、 dat a );

 str= Ins e rS t r(s tr, 15 , s2 )

 ;

 // / 插入字符串

 D D i spS t r(st r);

 Str L ength ( st r); ;

 printf( "順序字符串得基本運算到此結(jié)束了\ \ n”) ) ;

 }

 實驗結(jié)果:

 實驗四

 編程建立二叉樹, 對樹進行插入刪除及遍歷得程序

 1、實驗?zāi)康?(1)

 進一步掌握指針變量得用途與程序設(shè)計方法。

 (2)

 掌握二叉樹得結(jié)構(gòu)特征,以及鏈式存儲結(jié)構(gòu)得特點及程序設(shè)計方法。

 (3)

 掌握構(gòu)造二叉樹得基本方法。

 (4)

 掌握二叉樹遍歷算法得設(shè)計方法. 3. 實驗要求 (1)認真閱讀與掌握與本實驗相關(guān)得教材內(nèi)容。

 (2)掌握一個實際二叉樹得創(chuàng)建方法. (3)掌握二叉鏈存儲結(jié)構(gòu)下二叉樹操作得設(shè)計方法與遍歷操作設(shè)計方法。

 4。

 實驗內(nèi)容 (1)定義二叉鏈存儲結(jié)構(gòu). (2)設(shè)計二叉樹得基本操作(初始化一棵帶頭結(jié)點得二叉樹、左結(jié)點插入、右結(jié)點插入、中序遍歷二叉樹等)。

 (3)按照建立一棵實際二叉樹得操作需要,編寫建立二叉樹、遍歷二叉樹得函數(shù). (4)編寫測試主函數(shù)并上機運行。打印出運行結(jié)果,并結(jié)合程序運行結(jié)果進行分析。

 實驗代碼: #i nclu d e<i os s t re am m 、h h >

 t ypede f

 s s t ruc t

 bi t nod e{

 ch h a r da a ta; ;

 s tr uc t bitnod e

 * * l chi l d, * rchi ld d ;

 }bino de e ,* * b itre e;

 void createbit r ee(bi tr e e *T){

 ch ar c h;

 c c i n>>ch ;

 if(ch = ="0 ')

 (*T )= NULL ;

 else {

 *(=)T? (*T)= ne w

 b it n ode;

 ( *T)- -> > data= = ch;

 crea t ebitree ( &( * T) — 〉l l c hild) ;

 create bi tre e( ( &(*T T )

 — 〉r r c hil d); ;

 } }?}

 void in n o rd e rout (b itr e e T) {

 if (T ){

 droni? inord e ro u t(T- -> > lc c hild d ); ;

 oc c?? o ut <<T T- - >data < <end d l; ;

 tuoredroni?? inorderout (T- - 〉 rchi ld d );

 } }?} }

 void p os s t ord er r ( bitree T) {

 if(T) {

 po s torder(T- - >l c hi l d) ;

 postor de e r (T — >r r c hil d) ) ;

 cout<<T -〉 data 〈 <endl ;

 }

 }

 in n t

 countleaf ( bit r ee

 bt ){

 i i f( !bt)

 er? re t urn 0;

 if f (b b t- >lc hi i ld ==N U LL&& bt t- - >rchild==NUL)

 L)

 ret urn n

 1 1 ;

 r r e turn ( c oun t lea f(bt- - 〉 lc hi i ld d )+ + co untl eaf (b b t- - >rc c hi ld )); ;

 } }

 voi d

 m ai n( ( ){ {

 b it ree

 b t;

 creat e bit r ee (&bt t );

 in o rd er out( b t) ;

 cou t <<" "< < 〈 endl ;

 postorder ( bt) ;

 countlea f( ( bt t );

 }

  實驗五 建立有序表并進行折半查找 1、 實驗?zāi)康?掌握遞歸算法求解問題得基本思想與程序?qū)崿F(xiàn)方法. 2.實驗要求)1(

 .想思法算得驗實本握掌與讀閱真認?(2)編寫與調(diào)試完成程序. (3)保存與打印程序得運行結(jié)果,并對運行結(jié)果進行分析。

 3、實驗內(nèi)容 (1) 分析掌握折半查找算法思想,在此基礎(chǔ)上,設(shè)計出遞歸算法與循環(huán)結(jié)構(gòu)兩種實現(xiàn)方法得折半查找函數(shù). (2)

 編寫程序?qū)崿F(xiàn):在保存于數(shù)組得 1000個有序數(shù)據(jù)元素中查找數(shù)據(jù)元素x就是否存在。數(shù)據(jù)元素x要包含兩種情況:一種就是數(shù)據(jù)元素 x 包含在數(shù)組中;另一種就是數(shù)據(jù)元素x 不包含在數(shù)組中 (3)

 數(shù)組中數(shù)據(jù)元素得有序化既可以初始賦值時實現(xiàn),也可以設(shè)計一個排序函數(shù)實現(xiàn)。

 (4) 根據(jù)兩種方法得實際運行時間,進行兩種方法時間效率得分析對比。

 實驗代碼: #include<stdio、h> #include<stdlib、h> #define MAX_LENGTH

 100 typedef int KeyType;

 typedef struct {

 int key; }ElemType; typedef struct {

 出空元單號 0 //

  ;]HTGNEL_XAM[mele epyTmelE?

 int length; }SSTable; int Search_Bin(SSTable ST,KeyType key) {

  int low,high,mid;

 low = 1;high = ST、length;

  while(low <=high)

 {

  mid = (low+high)/2;

  )yek、]dim[mele、TS== yek(fi??

 ?

  return mid;

  esle? ?

  )yek、]dim[mele、TS〈yek(fi? ?

  ;1—dim = hgih? ??

  else

 ?

  low=mid+1;

 }

 ?

 return 0; } void main() {

 int i,result;

 ;TS elbaTSS? ;yek epyTyeK? printf("please input length:");

 scanf("%d",&ST、length);

 )++i;htgnel、TS=<i;1=i(rof? {?

 ;)":mele、TS tupni esaelp"(ftnirp?

 scanf("%d",&ST、elem[i]);

 }

 printf("please input keyword:");

  scanf("%d",&key);

 result=Search_Bin(ST,key);

 )0==tluser(fi?

 printf(”Don"t find\n");

 esle?

 printf(”Find the key,the position is %d\n",result); } 實驗結(jié)果:

 實驗六 建立一組記錄并進行插入排序 1、 實驗?zāi)康?(1)

 掌握插入排序算法得思想. (2)

 掌握順序隊列下插入排序算法得程序設(shè)計方法。

 2、 實驗要求 )1(

 。想思得法算序排入插中材教握掌與讀閱真認?(3) 編寫基于順序隊列得插入排序排序算法并上機實現(xiàn)。

 3、 實驗內(nèi)容 (1) 編寫基于順序隊列得插入排序函數(shù)。

 (2) 設(shè)計一個測試主函數(shù),實現(xiàn)對基于順序隊列結(jié)構(gòu)得插入排序算法得測試. (3)

 分析程序得運行結(jié)果。

 實驗代碼: #include〈stdio、h> #include〈stdlib、h> #define MAXSIZE 100 typedef struct

 {

 int key; }sortkey; typedef struct {

 sortkey elem[MAXSIZE];

 int length; }sortelem; void InsertSort(sortelem *p) {

 int i,j;

 for(i=2;i<=p-〉length;i++)

 {

 if(p->elem[i]、key<p->elem[i-1]、key)/*小于時,需將 elem[i]插入有序表*/

 {

 p—>elem[0]、key=p—>elem[i]、key; /*為統(tǒng)一算法設(shè)置監(jiān)測*/

 for(j=i—1;p->elem[0]、key〈p->elem[j]、key;j—-)

  p->elem[j+1]、key=p—〉elem[j]、key;/*記錄后移*/

  p-〉elem[j+1]、key=p->elem[0]、key; /*插入到正確位置*/

 }

 } } void main() {

 sortelem *p; int i; int b[6]={45,23,54,6,4,46}; p=(sortelem *)malloc(sizeof(sortelem)); p—〉length=0; for(i=1;i<7;i++)

 {

 p—>elem[i]、key=b[i—1];

 p->length++; } InsertSort(p); for(i=1;i<7;i++) {

 printf(”%d

 ",p—>elem[i]、key); } system("pause"); } 實驗結(jié)果:

  實驗七

 建立一組記錄并進行快速排序 1、 實驗?zāi)康?(1)

 掌握快速排序算法得思想. (2) 掌握順序隊列下快速排序算法得程序設(shè)計方法。

 2、 實驗要求 )1(

 。想思得法算序排速快中材教握掌與讀閱真認?(3)

 編寫基于順序隊列得快速排序排序算法并上機實現(xiàn)。

 3、 實驗內(nèi)容 (1) 編寫基于順序隊列得快速排序函數(shù)。

 (2) 設(shè)計一個測試主函數(shù),實現(xiàn)對基于順序隊列結(jié)構(gòu)得快速排序算法得測試. (3)

 分析程序得運行結(jié)果。

 實驗代碼: #include〈iostream、h〉 void quick_sort(int a[],int low, int high)

 {

 int i,j,pivot;

 if (low < high)

 {

 pivot = a[low];

 i = low;

 j = high;

 while (i < j)

 {

 //從頂端開始比較值,如果小于標準值,停止

 while (i < j && a[j] >= pivot)

 {

 j--;

 }

 //將比 pivot 小得元素移到低端,下標加加

 if (i < j)

 a[i++] = a[j];

 //從底端開始比較值,如果大于標準值,停止

 while (i < j && a[i] <= pivot)

 {

 i++;

 }

 //將比 pivot 大得元素移到頂端,下標減減

  if (i 〈 j)

 a[j--] = a[i];

 }

 //pivot 移動到最終位置

 a[i] = pivot;

 //對左區(qū)間進行遞歸排序

 quick_sort(a, low, i—1);

 //對右區(qū)間進行遞歸排序

 quick_sort(a, i+1, high);

 }

 }

 void print_array(int a[], int n)

 {

 for(int i = 0; i 〈 n; i++)

 {

 cout <〈 a[i] 〈< ",";

 }

 }

 int main()

 {

 int data[9] = {54,38,96,23,15,72,60,45,83};

 quick_sort(dat(yī)a, 0, 8);

 print_array(data,9);

 return 0;

 }

  實驗結(jié)果:

推薦訪問: 數(shù)據(jù)結(jié)構(gòu) 全集 實驗

【數(shù)據(jù)結(jié)構(gòu)實驗報告全集】相關(guān)推薦

工作總結(jié)最新推薦

NEW