java stack实现

什么是堆栈,关于这个名词,我在百度,google搜索了半天,也没有发现一个比较权威的解释,还有许多资料语焉不详,就以维基百科的解释为准吧,和我记忆中的一致。

堆栈(英文:stack),中国大陆作堆栈,台湾作堆叠,在计算机科學中,是一種特殊的串列形式的資料結構,它的特殊之處在於只能允許在鏈結串列或陣列的一端(稱為堆疊頂端指標,英文為top)進行加入資料(push)和輸出資料(pop)的運算。另外堆疊也可以用一維陣列或連結串列的形式來完成。堆疊的另外一個相對的操作方式稱為佇列。 由於堆疊資料結構只允許在一端進行操作,因而按照後進先出(LIFO,  Last In First Out)的原理運作。

堆疊資料結構使用兩種基本操作:推入(push)和彈出(pop): 推入(push)  :將數據放入堆疊的頂端(陣列形式或串列形式),堆疊頂端top指標加一。 彈出(pop)  :將頂端數據資料輸出(回傳),堆疊頂端資料減一。

 

下面是用java数组实现堆栈

 

  1. /**
  2. * 使用数组实现堆栈,包括入栈、出栈、获取堆栈长度、
  3. * @author Adair
  4. */ 
  5. publicclass Stack {       
  6.    
  7.   Object[] data; 
  8.    
  9.   int maxSize;   
  10.   //栈顶位置 
  11.   int top;       
  12.    
  13.   public Stack(int maxSize) {       
  14.       this.maxSize = maxSize;       
  15.       data = new Object[maxSize];       
  16.       top = -1;       
  17.   }       
  18.     
  19.   /**
  20.    * 获取堆栈长度
  21.    * @return 堆栈长度
  22.    */ 
  23.   publicint getSize() 
  24.   { 
  25.     return maxSize; 
  26.   } 
  27.    
  28.   /**
  29.    * 返回栈中元素的个数
  30.    * @return 栈中元素的个数
  31.    */ 
  32.   publicint getElementCount() 
  33.   { 
  34.     return top; 
  35.   } 
  36.    
  37.   /**
  38.    * 判断栈空
  39.    * @return 栈空
  40.    */ 
  41.   publicboolean isEmpty() 
  42.   { 
  43.     return top == -1
  44.   } 
  45.    
  46.   /**
  47.    * 判断栈满
  48.    * @return 栈满
  49.    */ 
  50.   publicboolean isFull() 
  51.   { 
  52.     return top+1 == maxSize; 
  53.   } 
  54.    
  55.   /**   
  56.    * 依次加入数据   
  57.    * @param data 要加入的数据通信   
  58.    * @return 添加是否成功   
  59.    */       
  60.   publicboolean push(Object data) {       
  61.     if(isFull())  
  62.     {       
  63.         System.out.println("栈已满!");       
  64.         returnfalse;       
  65.     }       
  66.     this.data[++top] = data;       
  67.     returntrue;       
  68.   }       
  69.          
  70.   /**   
  71.    * 从栈中取出数据   
  72.    * @return 取出的数据   
  73.    */       
  74.   public Object pop() throws Exception{       
  75.     if(isEmpty())  
  76.     {       
  77.         thrownew Exception("栈已空!");       
  78.     }       
  79.     returnthis.data[top--];       
  80.   }       
  81.    
  82.   /**
  83.    * 返回栈顶元素
  84.    * @return
  85.    */ 
  86.   public Object peek() 
  87.   { 
  88.     returnthis.data[getElementCount()];   
  89.   } 
  90.  
  91.  
  92.   publicstaticvoid main(String[] args) throws Exception {       
  93.       Stack stack=new Stack(1000);       
  94.       stack.push(new String("1"));       
  95.       stack.push(new String("2"));       
  96.       stack.push(new String("3"));       
  97.       stack.push(new String("4"));       
  98.       stack.push(new String("5"));   
  99.       System.out.println(stack.peek());  
  100.              
  101.       while(stack.top>=0)       
  102.       {       
  103.           System.out.println(stack.pop());       
  104.       }              
  105.   }       
  106. }      

posted @ 2012-02-03 11:17  淡泊名利  阅读(9152)  评论(1编辑  收藏  举报