乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      用JAVA實(shí)現(xiàn)堆棧(鏈表篇)

       Java修煉館 2011-09-27
      下面是用java 鏈表 實(shí)現(xiàn)堆棧

      1. /** 
      2.  * 表示鏈表的一個(gè)節(jié)點(diǎn) 
      3.  * @author Adair 
      4.  */  
      5. public class Node{  
      6.   Object element;  
      7.   Node next;  
      8.     
      9.   public Node(Object element) {  
      10.       this(element, null);  
      11.   }  
      12.   
      13.   public Node(Object element, Node n) {  
      14.       this.element = element;  
      15.       next = n;  
      16.   }  
      17.   
      18.   public Object getElement() {  
      19.       return element;  
      20.   }  
      21.   
      22.   public void setElement(Object element) {  
      23.       this.element = element;  
      24.   }  
      25.   
      26.   public Node getNext() {  
      27.       return next;  
      28.   }  
      29.   
      30.   public void setNext(Node next) {  
      31.       this.next = next;  
      32.   }  
      33. }  
      34.   
      35.   
      36.   
      37.   
      38. /** 
      39.  * 用鏈表實(shí)現(xiàn)堆棧 
      40.  * @author Adair 
      41.  */  
      42. public class ListStack  
      43. {  
      44.   Node header;  //棧頂元素   
      45.   
      46.   int elementCount;// 棧內(nèi)元素個(gè)數(shù)   
      47.   
      48.   int size;// 棧的大小   
      49.   
      50.   /** 
      51.    * 構(gòu)造函數(shù),構(gòu)造一個(gè)空的堆棧 
      52.    */  
      53.   public ListStack()  
      54.   {  
      55.     header = null;  
      56.     elementCount = 0;  
      57.     size = 0;  
      58.   }  
      59.   
      60.   /** 
      61.    * 通過(guò)構(gòu)造器 自定義棧的大小 
      62.    * @param size 棧的大小 
      63.    */  
      64.   public ListStack(int size)  
      65.   {  
      66.     header = null;  
      67.     elementCount = 0;  
      68.     this.size = size;  
      69.   }  
      70.   
      71.   /** 
      72.    * 設(shè)置堆棧大小 
      73.    * @param size 堆棧大小 
      74.    */  
      75.   public void setSize(int size)  
      76.   {  
      77.     this.size = size;  
      78.   }  
      79.   
      80.   /** 
      81.    * 設(shè)置棧頂元素 
      82.    * @param header 棧頂元素 
      83.    */  
      84.   public void setHeader(Node header)  
      85.   {  
      86.     this.header = header;  
      87.   }  
      88.   
      89.   /** 
      90.    * 獲取堆棧長(zhǎng)度 
      91.    * @return 堆棧長(zhǎng)度 
      92.    */  
      93.   public int getSize()  
      94.   {  
      95.     return size;  
      96.   }  
      97.   
      98.   /** 
      99.    * 返回棧中元素的個(gè)數(shù) 
      100.    * @return 棧中元素的個(gè)數(shù) 
      101.    */  
      102.   public int getElementCount()  
      103.   {  
      104.     return elementCount;  
      105.   }  
      106.   
      107.   /** 
      108.    * 判斷棧是否為空 
      109.    * @return 如果棧是空的,返回真,否則,返回假 
      110.    */  
      111.   public boolean isEmpty()  
      112.   {  
      113.     if (elementCount == 0)  
      114.       return true;  
      115.     return false;  
      116.   }  
      117.   
      118.   /** 
      119.    * 判斷棧滿 
      120.    * @return 如果棧是滿的,返回真,否則,返回假 
      121.    */  
      122.   public boolean isFull()  
      123.   {  
      124.     if (elementCount == size)  
      125.       return true;  
      126.     return false;  
      127.   }  
      128.   
      129.   /** 
      130.    * 把對(duì)象入棧 
      131.    * @param value 對(duì)象 
      132.    */  
      133.   public void push(Object value)  
      134.   {  
      135.     if (this.isFull())  
      136.     {  
      137.       throw new RuntimeException("Stack is Full");  
      138.     }  
      139.     header = new Node(value, header);  
      140.     elementCount++;  
      141.   }  
      142.   
      143.   /** 
      144.    * 出棧,并返回被出棧的元素 
      145.    * @return 被出棧的元素 
      146.    */  
      147.   public Object pop()  
      148.   {  
      149.     if (this.isEmpty())  
      150.     {  
      151.       throw new RuntimeException("Stack is empty");  
      152.     }  
      153.   
      154.     Object obj = header.getElement();  
      155.   
      156.     header = header.getNext();  
      157.   
      158.     elementCount--;  
      159.     return obj;  
      160.   }  
      161.   
      162.   /** 
      163.    * 返回棧頂元素 
      164.    * @return 棧頂元素 
      165.    */  
      166.   public Object peek()  
      167.   {  
      168.     if (this.isEmpty())  
      169.     {  
      170.       throw new RuntimeException("Stack is empty");  
      171.     }  
      172.   
      173.     return header.getElement();  
      174.   }  

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類(lèi)似文章 更多