群體類和群體數(shù)據(jù)_第1頁
群體類和群體數(shù)據(jù)_第2頁
群體類和群體數(shù)據(jù)_第3頁
群體類和群體數(shù)據(jù)_第4頁
群體類和群體數(shù)據(jù)_第5頁
已閱讀5頁,還剩9頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、題 目 學(xué)生姓名 學(xué) 院 系 別 專 業(yè) 班 級(jí)學(xué)校代碼:10128學(xué) 號(hào):面向?qū)ο蟮某绦蛟O(shè)計(jì)實(shí)驗(yàn)報(bào)告群體類和群體數(shù)據(jù)理學(xué)院數(shù)學(xué)系信息與計(jì)算科學(xué)任課教師二o年十月一、實(shí)驗(yàn)?zāi)康?、了解節(jié)點(diǎn)類的聲明和實(shí)現(xiàn),學(xué)習(xí)其使用方法2、了解鏈表類的聲明和實(shí)現(xiàn),學(xué)習(xí)其使用方法3、了解棧類的聲明和實(shí)現(xiàn),學(xué)習(xí)其使用方法4、了解隊(duì)列類的聲明和實(shí)現(xiàn),學(xué)習(xí)其使用方法5、掌握對數(shù)組元素排序的方法6、掌握對數(shù)組元素查找的方法二、實(shí)驗(yàn)內(nèi)容1、編寫程序?qū)崿F(xiàn)例9-5的節(jié)點(diǎn)類,并編寫測試程序,實(shí)現(xiàn)鏈表的基本操作2、 編寫程序?qū)崿F(xiàn)例9-6的鏈表類,在測試程序中聲明兩個(gè)整型鏈表 A和B,分別插入5 元素,然后把B中的元素加入A的尾部3、

2、編寫程序,用鏈表實(shí)現(xiàn)隊(duì)列(或棧),在測試程序中聲明一個(gè)整型隊(duì)列(或棧)對象, 插入5個(gè)整數(shù),壓入隊(duì)列(或棧),再依次取出并顯示出來。4、將直接插入排序、直接選擇排序、冒泡排序、順序查找函數(shù)封裝到第九章的數(shù)組類中,作為成員函數(shù),實(shí)現(xiàn)并測試這個(gè)類。三、實(shí)驗(yàn)程序及結(jié)果1.程序一3 9124&623 5E B43L.s 1z :B&5235G弓12肯輸入一個(gè)需要?jiǎng)h除的整數(shù)書4Iii se t ;BGS 25615?9Pi* o e Kn* 1b Vcont; inuo H. endl;();while(!()();();();cout 此時(shí),鏈表 A的元素為:while(!()cout () ”;()

3、;cout l nsertAfter( newNode);保存其地址p = prevPtr-DeleteAfter();假亡力的兀烹再13SV1154=7 uf ri.i .g.i =246ezie乍匕時(shí),連表。的尢親妖i135795HPio=& OLfty Jo/ to &onI nsertAfter( newNode);保存其地址p = prevPtr-DeleteAfter();芾;匸十十上捫A真驗(yàn)K京笞案生盔4豐址糞和豐未散揺*Dubj屮.4X9 .3.隊(duì)列f的元秦:4、 3 5 Fi*e ct? an i/ kay a confc Inue接插入排序2.直接選擇排序3.冒泡排序:”;

4、cin SortType;switch(SortType)case 1:();break;case 2:();break;case 3:();break;default:cout 輸入錯(cuò)誤,程序退出!;exit(0);cout 排序后的數(shù)組為: endl;for(i=0;i10;i+)cout Ai ”;cout en dl;cout SearchNum;k= (SearchNum);if (k0)cout 沒有找到數(shù)字 SearchNum endl;elsecout SearchNum 是第 k+1 個(gè)數(shù)字 endl;/#ifndef ARRAY1_CLASS#defi ne ARRAY1_

5、CLASS#in clude #in clude using n amespace std;#ifndef NULLcon st int NULL = 0;#en dif / NULLenum ErrorTypein validArraySize, memoryAllocati on Error, i ndexOutOfRa nge;char *errorMsg=I nvalid array size, Memory allocati on error,In valid in dex:;template class Arrayprivate:T* alist;int size;/錯(cuò)誤處理函數(shù)v

6、oid Error(ErrorType error,i nt badl ndex=0) con st; public:/構(gòu)造函數(shù)和析構(gòu)函數(shù)Array(i nt sz = 50);Array(c onst Array& A);Array(void);/賦值,下標(biāo)和指針轉(zhuǎn)換函數(shù)Array& operator= (const Array& rhs);T& operator(i nt i);operator T* (void) con st;int ListSize(void) con st; /取數(shù)組大小void Resize(i nt sz);/修改數(shù)組大小/直接插入排序void In serti

7、 on Sort();/直接選擇排序void Selectio nSort();/冒泡排序void BubbleSort();順序查找int SeqSearch(T key);template cerr errorMsgerror;if (error = in dexOutOfRa nge) cerr bad In dex;cerr en dl;exit(1);template Array:Array(i nt sz)if (sz = 0)Error(i nvalidArraySize);size = sz;alist = new Tsize;if (alist = NULL)Error(me

8、moryAllocatio nError);template Array:Array(void)delete alist;template Array:Array(c onst Array& X) int n =;size = n;alist = new Tn;if (alist = NULL)Error(memoryAllocatio nError);T* srcptr =;T* destptr = alist;while (n-)*destptr+ = *srcptr+; template Array& Array:operator= (const Array& rhs)int n =;i

9、f (size != n)delete alist; alist = new Tn;if (alist = NULL)Error(memoryAllocatio nError);size = n;T* destptr = alist;T* srcptr =;while (n-)*destptr+ = *srcptr+;return *this;template if (n size-1)Error(i ndexOutOfRa nge, n);return alist n;template Array:operator T* (void) constreturn alist;template i

10、nt Array:ListSize(void) constretur n size;template void Array:Resize(i nt sz)if (sz = 0)Error(i nvalidArraySize);if (sz = size)return;T* n ewlist = new Tsz;if (n ewlist = NULL)Error(memoryAllocatio nError);int n = (sz = size) sz : size;T* srcptr = alist;T* destptr = n ewlist;while (n-)*destptr+ = *s

11、rcptr+;delete alist;alist = n ewlist;size = sz;template void Array:l nserti on Sort()int i, j;T temp;for (i = 1; i 0 & temp alistj-1)alistj = alistj-1;j-;alistj = temp;template int smallI ndex;int i, j;for (i = 0; i size-1; i+)smalll ndex = i;for (j = i+1; j size; j+)if (alistj alistsmalllndex) smal

12、l In dex = j;Swap(alisti, alistsmallI ndex);template void Swap (T &x, T &y)T temp;temp = x;x = y;y = temp;template void Array:BubbleSort()int i,j;int lastExcha ngeln dex;i = size-1;while (i 0) lastExchangelndex = 0;for (j = 0; j i; j+)if (alistj+1 alistj)Swap(alistj,alistj+1); lastExcha ngeln dex = j; i = lastExcha ngeln dex;template int Array:SeqSearch(T key)for(i nt i=0;i size;i+)if (alisti = key)return i;return -1;#

溫馨提示

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

評論

0/150

提交評論