


下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、實驗報告三 棧和隊列一、 實驗目的:1掌握棧的根本操作的實現(xiàn)方法。2利用棧先進后出的特點,解決一些實際問題。3掌握鏈式隊列及循環(huán)隊列的根本操作算法。4應用隊列先進先出的特點,解決一些實際問題。 二、 實驗內容:1、 使用一個棧,將一個十進制轉換成二進制。 粘貼源程序: package Q1;public class SeqStack publicintelement ;publicinttop ;publicstaticSeqStack p;publicSeqStack( int size)this . element =new int size; this . top =-1;public
2、void push( int x)this . top +;this . element this . top =x;public int pop(). top -;. top ;return this . top =-1 ? -1: (int )this . element thispublic int get()return this . top =-1 ? -1: (int )this . element thispublic static void disp(SeqStack p)int t = -2;while (t!=-1) t=p.pop(); if (t!=-1)System.
3、 out .printf( "%d" ,t);public static void fun( int x)int t;while (x!=1)t=x%2;x=x/2; p.push(t);if (x=1)p.push(x);public static void main(String args) p=new SeqStack(13);fun (99);disp ( p);粘貼測試數(shù)據(jù)及運行結果:<terminated110&011|2、回文是指正讀反讀均相同的字符序列,如"acdca、“dceecd 均是回文,但"book不是回文。利用1中
4、的根本算法,試寫一個算法判定給定的字符串是否為回文。提示:將半字符入棧,依次彈出與另一半逐個比擬 粘貼源程序:package Q2;public classSeqStack publicint element ;publicint top ;publicstatic SeqStackp;publicSeqStack( intsize)this.element =newint size;this.top =-1;public void push( int x)this . top +;this . element this . top =x;public int pop()return this
5、 . top =-1 ? -1:(public int get()return this . top =-1 ? -1:(int ) this . element this . top -;int ) this . element this . top ;public static void input(String str)int i=0;int t=str.le ngth();if (t%2=0)for (i=0;i<t/2;i+)p.push(str.charAt(i); else for (i=0;i<=t/2;i+)p.push(str.charAt(i);public
6、staticboolea n compare(Str ing str,SeqStack p)boolean flag = true ;char t;int len gth=str.le ngth();if (length%2=0)for (int i=0;i<length/2;i+)t=str.charAt(le ngth/2+i);if (t!=p.pop()flag= false ; break ; else for (int i=0;i<length/2+1;i+)t=str.charAt(le ngth/2+i);if (t!=p.pop()flag= false ; br
7、eak ;return flag;public static void main( Str in g args) boolea n flag;p=new SeqStack(100);Stri ng str = new String( "acbca");p. input (str):flag= p pare (str. p):if (flag= true )System. out .println("yes");else System. out .println( "No");粘貼測試數(shù)據(jù)及運行結果:3、使用3個隊列分別保存上最近10個
8、“未接來電、“已接來電、“已撥粘貼源程序:package Q3;public class SeqQueue<T> publicObject element ;publicint front,rear ;publicSeqQueuep;publicSeqQueue(int len gth)this.element=new Objectle ngth;this.front =this . rear =0;public boolea n isEmpty()return this . front = this . rearpublicboolea n isFull()boolea n fl
9、ag= false ;if (this .front != this . rear && ( this . rear +1)%this . eleme nt this . front )flag= true ;return flag;public void enquere(T x)this . element this . rear =x;this . rear =( this . rear +1)%this . element . length ;public T dequeue()if (isEmpty()return n ull ;T temp=(T) this _ele
10、ment Lthis .front 丄this . front =( this . front +1)%this . element . length ;return temp;public static void disp(SeqQueue p)int i=1;.length =while (p.isEmpty()!= true )System. out .println(i+"."+p.dequeue();i+;System. out .println();public static void fun() long num=0;new SeqQueue(11);new
11、SeqQueue(11);int t=0;SeqQueue missedCall =SeqQueue receivedCall =SeqQueue outgo in gCall =new SeqQueue(11);while (!(missedCall.isFull() && receivedCall.isFull() && outgoi ngCall.isFull()num=( int )(Math.random ()*100000000);t=( int )(Math. random ()*101);if (t%3=0)missedCall.e nquere
12、 (n um);if (t%3=1)receivedCall.e nquere( num);if (t%3=2)outgo in gCall.e nquere (n um);"missedCall" );"receivedCall")"outgoi ngCall")main( Stri ng args) System. out .println( disp (missedCall);System. out .println( disp (receivedCall);System. out .println( disp (outgoingCall);public static voidfun ();粘貼測試數(shù)據(jù)及運行結果:4.41ME2E百用7W27A6127S82631824«57rsceiyedcall&47S*,4697426.7783 &05 37.92174S25均眈弼旳outgpi ngCall1-B324BM72.m3tl9353.274277285u673107S56-777&&
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 進口摩托轉賣合同范本
- 藝術教師兼職合同范本
- 私人裝修房合同范本
- 內架外架合同范本
- 轉讓店鋪簡易合同范本
- 廈門訂車合同范例
- 企業(yè)居間合同范例
- 農機服務作業(yè)合同范例
- 廠地購買合同范例
- 公積金貸款買房合同范例
- 工程項目竣工交接單模板
- 2024年無錫宜興市人民檢察院聘用合同制檢察書記員招考易考易錯模擬試題(共500題)試卷后附參考答案
- 馬克思主義中國化的歷史進程
- 北京市順義一中2023-2024學年高一下學期3月月考生物試題2
- 內蒙古包頭市2024年中考數(shù)學試卷
- 應征公民體格檢查表
- 青海交通運輸廳事業(yè)單位筆試真題2024
- 2024發(fā)電企業(yè)安全風險分級管控和隱患排查治理管理辦法
- 祛斑簽約合同
- 環(huán)保設備檢測報告
- 測速記載及流量計算表二
評論
0/150
提交評論