數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)報(bào)告 棧和隊(duì)列_第1頁
數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)報(bào)告 棧和隊(duì)列_第2頁
數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)報(bào)告 棧和隊(duì)列_第3頁
數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)報(bào)告 棧和隊(duì)列_第4頁
數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)報(bào)告 棧和隊(duì)列_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1/11數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)報(bào)告計(jì)科090909

數(shù)據(jù)及實(shí)驗(yàn)二棧和隊(duì)列:迷宮問題求解實(shí)驗(yàn)?zāi)康恼莆諚:完?duì)列的順序存儲(chǔ)結(jié)構(gòu);掌握棧和隊(duì)列的操作特性;掌握基于順序棧和鏈隊(duì)列的基本操作的實(shí)現(xiàn)方法。實(shí)驗(yàn)內(nèi)容設(shè)計(jì)數(shù)據(jù)結(jié)構(gòu)存儲(chǔ)迷宮;設(shè)計(jì)存儲(chǔ)結(jié)構(gòu)保存從入口到出口的通路;設(shè)計(jì)算法完成迷宮問題的求解;分析算法的時(shí)間復(fù)雜度。實(shí)驗(yàn)實(shí)現(xiàn)1、用棧實(shí)現(xiàn)求迷宮從出口到入口的一條路徑(不一定是最短路徑)(1)順序棧結(jié)構(gòu)SeqStack.hconstintStackSize=64;//10只是示例性的數(shù)據(jù),可以根據(jù)實(shí)際問題具體定義template<classT>//定義模板類SeqStackclassSeqStack{public:SeqStack();//構(gòu)造函數(shù),棧的初始化 ~SeqStack(){}//析構(gòu)函數(shù)voidPush(Tx);//將元素x入棧 TGetTop();TPop();//將棧頂元素彈出 intEmpty();//判斷棧是否為空 voidPrint();//輸出棧中元素private:Tdata[StackSize];//存放棧元素的數(shù)組inttop;//棧頂指針,指示棧頂元素在數(shù)組中的下標(biāo)};template<classT>SeqStack<T>::SeqStack(){ top=-1;}template<classT>voidSeqStack<T>::Push(Tx){if(top==StackSize-1)throw"上溢";top++;data[top]=x;}template<classT>TSeqStack<T>::Pop(){Tx;if(top==-1)throw"下溢";x=data[top--];returnx;}template<classT>TSeqStack<T>::GetTop(){ if(top!=-1)returndata[top];}template<classT>intSeqStack<T>::Empty(){ if(top==-1)return1; elsereturn0;}template<classT>voidSeqStack<T>::Print(){ while(top!=-1) { Ttemp; temp=Pop(); cout<<temp.x<<'\t'<<temp.y<<'\t'<<temp.d<<'\n'; }}主函數(shù)實(shí)現(xiàn)四方向查找#include<iostream.h>#include"SeqStack.h"constintN=10;typedefstruct{ intx,y,d;}DataType;structDirectInc{ intdx,dy;};constDirectIncmove[4]={{0,1},{1,0},{0,-1},{-1,0}};voidmazepath(intmaze[N][N]){ DataTypetemp; intg,h,v,i,j; SeqStack<DataType>S;//棧(*S)已建立 temp.x=1;temp.y=1;temp.d=-1;//入口位置及方向 maze[1][1]=-1; S.Push(temp); while(!S.Empty()) { temp=S.GetTop();i=temp.x;j=temp.y;v=temp.d+1; if(v==4)S.Pop(); while(v<4) { g=i+move[v].dx;h=j+move[v].dy; if(maze[g][h]==0) { temp.x=g;temp.y=h;temp.d=v; S.Push(temp); maze[i][j]=-1;i=g;j=h; if(i==N-2&&j==N-2) { S.Print(); return; } elsev=0; } else { v++; if(v==4)S.Pop(); } }//endwhile(v<4) }//endwhile(!S.Empty())}voidmain(){ intmaze[N][N]={{1,1,1,1,1,1,1,1,1,1}, {1,0,0,1,0,0,0,1,0,1}, {1,0,0,1,0,0,0,1,0,1}, {1,0,0,0,0,1,1,0,0,1}, {1,0,1,1,1,0,0,0,0,1}, {1,0,0,0,1,0,0,0,0,1}, {1,0,1,0,0,0,1,0,0,1}, {1,0,1,1,1,0,1,1,0,1}, {1,1,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1}}; cout<<"從出口到入口的路徑為\n(xy代表坐標(biāo)d代表方向(東0,南1,西2,北3))\nx\ty\td"<<endl; mazepath(maze);}運(yùn)行結(jié)果:b、八方向查找#include<iostream.h>#include"SeqStack.h"constintN=10;typedefstruct{ intx,y,d;}DataType;structDirectInc{ intdx,dy;};constDirectIncmove[8]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};voidmazepath(intmaze[N][N]){ DataTypetemp; intg,h,v,i,j; SeqStack<DataType>S;//棧(*S)已建立 temp.x=1;temp.y=1;temp.d=-1;//入口位置及方向 maze[1][1]=-1; S.Push(temp); while(!S.Empty()) { temp=S.GetTop();i=temp.x;j=temp.y;v=temp.d+1; if(v==8)S.Pop(); while(v<8) { g=i+move[v].dx;h=j+move[v].dy; if(maze[g][h]==0) { temp.x=g;temp.y=h;temp.d=v; S.Push(temp); maze[i][j]=-1;i=g;j=h; if(i==N-2&&j==N-2) { S.Print(); return; } elsev=0; } else { v++; if(v==8)S.Pop(); } }//endwhile(v<8) }//endwhile(!S.Empty())}voidmain(){ intmaze[N][N]={{1,1,1,1,1,1,1,1,1,1}, {1,0,0,1,0,0,0,1,0,1}, {1,0,0,1,0,0,0,1,0,1}, {1,0,0,0,0,1,1,0,0,1}, {1,0,1,1,1,0,0,0,0,1}, {1,0,0,0,1,0,0,0,0,1}, {1,0,1,0,0,0,1,0,0,1}, {1,0,1,1,1,0,1,1,0,1}, {1,1,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1}}; cout<<"從出口到入口的路徑為\n(xy代表坐標(biāo)d代表方向(東0,東南1,南2,西南3,西4,西北5,北6,東北7))\nx\ty\td"<<endl; mazepath(maze);}運(yùn)行結(jié)果:2、用隊(duì)列實(shí)現(xiàn)求迷宮問題從入口到出口的最短路徑鏈隊(duì)列結(jié)構(gòu)CirQueue.hconstintQueueSize=100;//定義存儲(chǔ)隊(duì)列元素的數(shù)組的最大長度template<classT>//定義模板類CirQueueclassCirQueue{public:CirQueue();//構(gòu)造函數(shù),置空隊(duì)~CirQueue();//析構(gòu)函數(shù)voidEnQueue(Tx);//將元素x入隊(duì)TDeQueue();//將隊(duì)頭元素出隊(duì)TGetQueue();//取隊(duì)頭元素(并不刪除)intEmpty();//判斷隊(duì)列是否為空 TGet(inti); intGetFront(){returnfront;} intGetRear(){returnrear;}private:Tdata[QueueSize];//存放隊(duì)列元素的數(shù)組intfront,rear;//隊(duì)頭和隊(duì)尾指針,分別指向隊(duì)頭元素的前一個(gè)位置和隊(duì)尾元素的位置};template<classT>CirQueue<T>::CirQueue(){ front=rear=0;}template<classT>CirQueue<T>::~CirQueue(){}template<classT>voidCirQueue<T>::EnQueue(Tx){if((rear+1)%QueueSize==front)throw"上溢";rear=(rear+1)%QueueSize;//隊(duì)尾指針在循環(huán)意義下加1data[rear]=x;//在隊(duì)尾處插入元素}template<classT>TCirQueue<T>::DeQueue(){if(rear==front)throw"下溢";front=(front+1)%QueueSize;//隊(duì)頭指針在循環(huán)意義下加1returndata[front];//讀取并返回出隊(duì)前的隊(duì)頭元素,注意隊(duì)頭指針}template<classT>//指向隊(duì)頭元素的前一個(gè)位置TCirQueue<T>::GetQueue(){inti;if(rear==front)throw"下溢";i=(front+1)%QueueSize;//注意不要給隊(duì)頭指針賦值returndata[i];}template<classT>intCirQueue<T>::Empty(){if(front==rear) return1; else return0;}template<classT>TCirQueue<T>::Get(inti){ if(i>=0&&i<=rear)returndata[i];}主函數(shù)實(shí)現(xiàn)(八方向查找)#include<iostream.h>#include"CirQueue.h"constintM=6,N=8;typedefstruct{intx,y,pre;}DataType;structDirectInc{ intdx,dy;};DirectIncmove[8]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};voidPrintShortPath(CirQueue<DataType>Q){ inti=Q.GetRear(); do{ DataTypee=Q.Get(i); cout<<'('<<e.x<<','<<e.y<<')'<<"pre:"<<e.pre<<endl; i=e.pre; }while(i!=-1); cout<<endl<<"WholeQueue:"<<endl; for(intu=1;u<=Q.GetRear();u++) { DataTypee=Q.Get(u); cout<<'('<<e.x<<','<<e.y<<')'<<"pre:"<<e.pre<<endl; }}voidRestore(intmaze[M+2][N+2]){ inti,j; for(i=1;i<=M;i++) for(j=1;j<=N;j++) if(maze[i][j]==-1)maze[i][j]=0; for(i=1;i<=M;i++) { for(j=1;j<=N;j++) cout<<maze[i][j]<<""; cout<<endl; }}voidShortPathQue(CirQueue<DataType>*Q,intmaze[M+2][N+2]){ inti,j,g,h,v,find=0; DataTypee={1,1,-1}; Q->EnQueue(e); maze[1][1]=-1; while(!Q->Empty()&&!find) { e=Q->DeQueue(); i=e.x; j=e.y; for(v=0;v<8;v++) { g=i+move[v].dx; h=j+move[v].dy; if(maze[g][h]==0) { e.

溫馨提示

  • 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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論