基于java的数据结构学习手记3——栈

 三.栈

       栈只允许访问一个数据项:即最后插入的数据项。移除这个数据项后才能访问倒数第二个插入的数据项,以此类推。这种机制在不少编程环境中都很有用。

      大部分微处理器运用基于栈的体系结构。当调用一个方法时,把它的返回地址和参数压入栈,当方法结束返回时,那些数据出栈。栈操作就是嵌入在微处理器中。

     以下代码是最基本的栈数据结构的java实现,之后还会有更多关于栈的其它应用,每个数据和方法都有注释说明。

Code:
  1. package Stack;  
  2. //stack.java  
  3. //demostrates stacks  
  4. //  
  5. public class stack  
  6. {  
  7.     private int maxSize;           //size of stack array  
  8.     private long[] stackArray;  
  9.     private int top;               //top of the stack  
  10. //........................................................  
  11.     public stack(int s)            //constructor  
  12.     {  
  13.         maxSize=s;                 //set array size  
  14.         stackArray=new long [maxSize];//create array  
  15.         top=-1;                       //no item yet  
  16.     }  
  17. //........................................................  
  18.     public void push(long j)      //put item on top of stack  
  19.     {  
  20.         stackArray[++top]=j;      //increment too,insert item  
  21.     }  
  22. //........................................................  
  23.     public long pop()             //take item from top of stack  
  24.     {  
  25.         return stackArray[top--];  
  26.     }  
  27. //........................................................    
  28.     public long peek()                 //peek the top of stack  
  29.     {  
  30.         return stackArray[top];   
  31.     }  
  32. //........................................................  
  33.     public boolean isEmpty()      //true if stack is empty  
  34.     {  
  35.         return(top==-1);  
  36.     }  
  37. //........................................................  
  38.     public boolean isFull()       //true if stack is full  
  39.     {  
  40.         return(top==maxSize-1);  
  41.     }  
  42. //........................................................    
  43. //end class stack  

类的实例化调用:

     

Code:
  1. package Stack;  
  2.   
  3. public class stackApps {  
  4.   
  5.     /** 
  6.      * @param args 
  7.      */  
  8.     public static void main(String[] args)  
  9.     {  
  10.         // TODO Auto-generated method stub  
  11.         stack theStack=new stack(10);//make the new stack  
  12.         theStack.push(20);           //push items onto stack  
  13.         theStack.push(40);   
  14.         theStack.push(60);   
  15.         theStack.push(80);   
  16.           
  17.         while(!theStack.isEmpty())   //until it's empty  
  18.         {  
  19.             long value=theStack.pop();//delete item from stack  
  20.             System.out.print(value);  
  21.             System.out.print (" ");  
  22.         }//end while  
  23.         System.out.println();     
  24.     }//end main  
  25.               
  26. }//end stackApps  

3.运行结果

Code:
  1. 80 60 40 20   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值