




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、實(shí)訓(xùn)題三 棧和隊(duì)列的運(yùn)用一、實(shí)訓(xùn)目的1.掌握棧的數(shù)據(jù)類型描述及棧的特點(diǎn)。2.掌握棧的順序和鏈?zhǔn)絻煞N存儲(chǔ)結(jié)構(gòu)的特點(diǎn)及算法描述。3.掌握棧的5種基本運(yùn)算及算法在不同存儲(chǔ)結(jié)構(gòu)上的實(shí)現(xiàn)。4.掌握隊(duì)列的數(shù)據(jù)類型描述及鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)的特點(diǎn)和算法描述。5.掌握隊(duì)列的5種基本運(yùn)算及在鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)上的實(shí)現(xiàn)。二、實(shí)訓(xùn)內(nèi)容1. 設(shè)車輛廠生產(chǎn)了硬座車廂和軟座車廂共n節(jié)(混合在一起),要求用順序棧的5種運(yùn)算使所有的硬座車廂排列到所有軟座車廂的前面。2. 利用棧將中綴表達(dá)式轉(zhuǎn)換成等價(jià)的后綴表達(dá)式。3. 將一個(gè)正整數(shù)n轉(zhuǎn)換成十六進(jìn)制,要求用鏈棧的5種運(yùn)算來實(shí)現(xiàn)。4. 停車場管理。設(shè)有一個(gè)可以停放n輛汽車的狹長停車場(先進(jìn)后出
2、),它只有一個(gè)大門可以供車輛進(jìn)出。車輛按達(dá)到停車場時(shí)間的先后依次從停車場最里面向大門口停放(最先到達(dá)的第一輛車停放在停車場的最里面)。如果停車場已放滿n輛車,則后來的車輛只能在停車場大門外的便道上等待,一旦停車場內(nèi)有車離開,則排在便道上的第一輛車就可以進(jìn)入停車場。停車場內(nèi)如有有某輛車要離開,在它之后進(jìn)入停車場的車都必須先退出停車場為它讓路,待其開出停車場后,這些車再按原來的次序進(jìn)停車場。每輛車在離開停車場時(shí),都應(yīng)根據(jù)它在停車場內(nèi)停留的時(shí)間長短交費(fèi)。如果停留在便道上的車沒進(jìn)停車場就要離開,允許其離開,不受停車費(fèi),并且仍然保持在便道上的車輛次序。是編程模擬停車場管理。三、算法描述1.第1題的算法實(shí)
3、現(xiàn)#include#define elemtype charconst maxsize=1000;class seqstackpublic:elemtype stackmaxsize;int top;void inistack()top=0;void push(elemtype x)if(top=maxsize-1)coutoverflow;elsetop+;stacktop=x;void pop()if(top=0)coutunderflow;else top-;elemtype gettop()if(top=0)coutunderflow;return 0;else return stac
4、ktop;bool empty()if(top=0)return true;else return false;void main()int n,i;elemtype x;seqstack S;S.inistack();coutn;cout請(qǐng)輸入n節(jié)車廂代號(hào)(H代表硬座車廂,S代表軟座車廂);for(i=1;ix;if(x=H)coutx;else S.push(x);while(!S.empty()x=S.gettop();coutx;S.pop();coutendl;程序的結(jié)果:2第2題的算法實(shí)現(xiàn)#include#defineelemtypecharconstmaxsize=1000;el
5、emtypes1maxsize,s2maxsize;classseqstackpublic:elemtypestackmaxsize;inttop;voidinistack()top=0;voidpush(elemtypex)top+;stacktop=x;voidpop()top-;elemtypegettop()returnstacktop;boolempty()if(top=0)returntrue;elsereturnfalse;intoper(elemtypeop)switch(op)case+:case-:return1;case*:case/:return2;case(:retu
6、rn0;default:return0;voidchange()elemtypey,ch;top=0;inistack();push();inti=0,j=0;ch=s1i;while(ch!=)if(ch=)ch=s1+i;elseif(ch=()push(ch);ch=s1+i;elseif(ch=)y=gettop();while(y!=()s2j+=y;s2j+=;pop();y=gettop();pop();ch=s1+i;elseif(ch=+)|(ch=-)|(ch=*)|(ch=/)y=gettop();if(oper(ch)oper(y)push(ch);ch=s1+i;el
7、sey=gettop();s2j+=y;s2j+=;pop();elsewhile(ch=0)&(ch=9)|(ch=.)s2j+=ch;ch=s1+i;s2j+=;y=gettop();while(y!=)s2j+=y;s2j+=;pop();y=gettop();s2j=;voidmain()seqstacks;elemtypech;inti=0;coutch;while(ch!=)s1i+=ch;cinch;s1i=;cout中綴表達(dá)式為:endl;for(intj=0;ji;j+)couts1j;coutendl;s.change();cout后綴表達(dá)式為:endl;j=0;while
8、(s2j!=)couts2j;j+;coutendl;程序的結(jié)果3第3題的算法運(yùn)算#include#define elemtype charclass linkpublic:elemtype data;link *next;class linkstackpublic:link *top;void inistack()top=new link;top-next=NULL;void push(elemtype x)link *s=new link;s-data=x;s-next=top-next;top-next=s;void pop()link *s=top-next;if(s!=NULL)to
9、p-next=s-next;delete s;elemtype gettop()if(top-next!=NULL)return(top-next-data);else return NULL;bool empty ()if(top-next=NULL) return true;else return false;void decTOhex(int n)elemtype x;inistack();while(n);int j=n%16;if(j10) x=j+48;else x=j+55;push(x);n=n/16;while(!empty()x=gettop();coutx;pop();c
10、outendl;void main()int n;linkstack s;coutn;cout十進(jìn)制數(shù)n的十六進(jìn)制為;s.decTOhex(n);程序的結(jié)果:4第4題的算法實(shí)現(xiàn)#includeconst int N=5;const int M=6;typedef structint num;int arrtime; elemtype;struct seqstackelemtype stackN+1;int top;s1,s2;struct linkelemtype data;link *next;struct linkqueuelink *front,*rear;q;seqstack inis
11、tack(seqstack S)S.top=0;return S;seqstack push(seqstack S,elemtype x)S.top+;S.stackS.top=x;return S;seqstack pop(seqstack S)S.top-;return S;elemtype gettop(seqstack S)return S.stackS.top;int empty(seqstack S)if(S.top=0)return 1;else return 0;linkqueue iniqueue(linkqueue s)link *p;p=new link;p-next=N
12、ULL;s.front=s.rear=p;return s;linkqueue enqueue(linkqueue s,elemtype x)link *p;p=new link;p-data=x;p-next=s.rear-next;s.rear-next=p;s.rear=p;return s;linkqueue dlqueue(linkqueue s)link *p=s.front;s.front=p-next;delete p;return s;elemtype gethead(linkqueue s)return s.front-next-data;int emptyqueue(li
13、nkqueue s)if(s.front=s.rear)return 1;else return 0;void arrive(elemtype x)if(s1.top=N)q=enqueue(q,x);else s1=push(s1,x);void delive(elemtype x)int f=1;elemtype y;link *r;while(!empty(s1)&(f=1)if(s1.stacks1.top.num!=x.num)y=gettop(s1);s1=pop(s1);s2=push(s2,y);elsef=0;y=gettop(s1);s1=pop(s1);cout停車場中有
14、編號(hào)為x.num的車endl;cout該車將離開,應(yīng)收停車費(fèi):(x.arrtime-y.arrtime)*M元data.num!=x.num)r=p;p=p-next;if(p!=NULL)cout便道上有編號(hào)為x.num的車輛endl;cout該車將離開,應(yīng)收停車費(fèi)0.00元:next=p-next;delete p;elsecout便道上沒有編號(hào)為x.num的車輛,輸入的車輛不存在!endl;void pr1()int t=s1.top;cout停車場中的車輛編號(hào)和到達(dá)時(shí)間endl;while(t!=0)couts1.stackt.num s1.stackt.arrtimeendl;t-;coutnext;cout便道中的車輛編號(hào)和到達(dá)時(shí)間endl;while(p!=NULL)coutdata.num data.arrtimenext;coutendl;void main()int n;elemtype x;s1=inistack(s1);s2=inistack(s2);q=iniqueue(q);while(1)coutendl;cout請(qǐng)輸
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(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ǔ)空間,僅對(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 房屋租賃合同履約金補(bǔ)充協(xié)議
- 線上世博會(huì)保證金合同
- 小學(xué)英語集體備課與創(chuàng)意教學(xué)計(jì)劃
- 職業(yè)生涯規(guī)劃個(gè)人總結(jié)范文
- 2025年護(hù)士倫理學(xué)專項(xiàng)考試題庫:護(hù)理倫理學(xué)在護(hù)理倫理法規(guī)中的應(yīng)用試題
- 2025年電子商務(wù)師職業(yè)資格考試題庫:電子商務(wù)項(xiàng)目管理與團(tuán)隊(duì)協(xié)作試題集錦試卷
- 2025年消防執(zhí)業(yè)資格考試題庫:消防應(yīng)急救援裝備原理與性能分析試題
- 化妝品成分采購計(jì)劃
- 獨(dú)立門市出租合同樣本
- 綜合性苗木購銷合同
- 工會(huì)制度牌模板
- 2024年高級(jí)統(tǒng)計(jì)實(shí)務(wù)考試真題及答案解析
- 《日語零基礎(chǔ)學(xué)習(xí)》課件
- 打樣中心管理制度
- 前列腺癌護(hù)理個(gè)案查房課件
- 美團(tuán)外賣平臺(tái)轉(zhuǎn)讓協(xié)議
- 2023年1月自考11466現(xiàn)代企業(yè)人力資源管理概論試題及答案含解析
- 外研版(三年級(jí)起點(diǎn))三年級(jí)下冊(cè)英語單詞表-
- 幼兒園食譜播報(bào)
- 全國導(dǎo)游考試(面試)200問及面試內(nèi)容(附答案)
- 旋挖鉆機(jī)施工安全操作規(guī)程與注意事項(xiàng)
評(píng)論
0/150
提交評(píng)論