堆棧單鏈表實(shí)現(xiàn)_第1頁(yè)
堆棧單鏈表實(shí)現(xiàn)_第2頁(yè)
堆棧單鏈表實(shí)現(xiàn)_第3頁(yè)
堆棧單鏈表實(shí)現(xiàn)_第4頁(yè)
堆棧單鏈表實(shí)現(xiàn)_第5頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

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

文檔簡(jiǎn)介

1、單鏈表實(shí)現(xiàn)堆棧如下:節(jié)點(diǎn)類(lèi):public class Node /節(jié)點(diǎn)類(lèi) public int element;public Node link;public Node( link=null;public Node(int newelement,Node newlink element=newelement;link=newlink;public void setElement(int newelement element=newelement;public int getElement(return element;public void setLink(Node newlinklink=n

2、ewlink;public Node getlink( return link;堆棧類(lèi):public class StackList /堆棧類(lèi)Node head;Node top;int size;public StackList( head=top=new Node(;size=0;public void index(int positionthrows Exception /定位棧頂 if(position<0|position>size throw new Exception("參數(shù)錯(cuò)誤!"top=head.link;int j=0;while(top!=

3、null&&j top=top.link;j+; public void push(int numberthrows Exception /壓棧 top.setLink(new Node(number,top.link;top=top.link;size+;public int getTop(throws Exception /取棧頂 if(size=0throw new Exception("堆棧已空!"return top.getElement(; public int pop(throws Exception /出棧if(size=0throw new

4、 Exception("堆棧已空!"index(size-1;int temp=top.getElement(;size-;return temp;public boolean notEmpty( /非空否if(size>0return true;return false;public int getData(int jthrows Exceptionif(j < -1 | j > size - 1throw new Exception("參數(shù)錯(cuò)誤!"index(j;return top.getElement(;測(cè)試程序:import

5、java.io.*;public class TestStackListpublic static void main(String argsthrows IOExceptiontryBufferedReader Inputnumber=new BufferedReader(new InputStreamReader(System.in;String numberstring;StackList list=new StackList(;System.out.print("請(qǐng)輸入要建立的堆棧區(qū)的大小:"numberstring=Inputnumber.readLine(;in

6、t number1=Integer.parseInt(numberstring;System.out.println("請(qǐng)輸入要入棧的數(shù)據(jù):"for(int i=0;i numberstring=Inputnumber.readLine(;int number2=Integer.parseInt(numberstring;list.push(number2;System.out.println(" 堆棧中的各數(shù)據(jù)為:"for(int i=0;i System.out.print(list.getData(i+" "System.out.println(;System.out.println("取棧頂元素為:"+"n"+list.getTop(;System.out.println("堆棧中出棧數(shù)據(jù)為:"if(list.notEmpty( for(int i=0

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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)論