数组实现栈(Java)

1.1.  栈的数据结构

栈是一种先进后出的数据结果,只能在一端(称为栈顶(top))对数据项进行插入和删除。

1.2.  Java实现

StackTest

  1. package ch04;  
  2.    
  3. public class StackTest {  
  4.    
  5.     public static void main(String[] args) {  
  6.    
  7.        ArrayStack stack = new ArrayStack(10);  
  8.        System.out.println("isEmpty: "+stack.isEmpty());  
  9.        for(int i = 0;i<10;i++){  
  10.            stack.push(i);  
  11.        }  
  12.        System.out.println("isFull: "+stack.isFull());  
  13.         
  14.        while(!stack.isEmpty()){  
  15.            System.out.println(stack.pop());  
  16.        }  
  17.         
  18.     }  
  19.    
  20. }  
  21.    
  22. class ArrayStack{  
  23.     private int[] arrInt;//内置数组  
  24.     private int top;//栈顶指针  
  25.      
  26.     public ArrayStack(int size){  
  27.        this.arrInt = new int[size];  
  28.        top = -1;  
  29.     }  
  30.      
  31.     /** 
  32.      * 判断栈是否为空 
  33.      * @return 
  34.      */  
  35.     public boolean isEmpty(){  
  36.        return -1 == top;  
  37.     }  
  38.      
  39.     /** 
  40.      * 判断栈是否已满 
  41.      * @return 
  42.      */  
  43.     public boolean isFull(){  
  44.        return arrInt.length -1 == top;  
  45.     }  
  46.     /** 
  47.      * 向栈顶插入一个元素 
  48.      * @param item 
  49.      */  
  50.     public void push(int item){  
  51.        if(isFull()){  
  52.            throw new RuntimeException("栈已满");  
  53.        }  
  54.        arrInt[++top] = item;  
  55.     }  
  56.      
  57.     /** 
  58.      * 从栈顶取出一个元素 
  59.      * @return 
  60.      */  
  61.     public int pop(){  
  62.        if(isEmpty()){  
  63.            throw new RuntimeException("栈为空");  
  64.        }  
  65.        return arrInt[top--];  
  66.     }  
  67. }  
package ch04;
 
public class StackTest {
 
    public static void main(String[] args) {
 
       ArrayStack stack = new ArrayStack(10);
       System.out.println("isEmpty: "+stack.isEmpty());
       for(int i = 0;i<10;i++){
           stack.push(i);
       }
       System.out.println("isFull: "+stack.isFull());
      
       while(!stack.isEmpty()){
           System.out.println(stack.pop());
       }
      
    }
 
}
 
class ArrayStack{
    private int[] arrInt;//内置数组
    private int top;//栈顶指针
   
    public ArrayStack(int size){
       this.arrInt = new int[size];
       top = -1;
    }
   
    /**
     * 判断栈是否为空
     * @return
     */
    public boolean isEmpty(){
       return -1 == top;
    }
   
    /**
     * 判断栈是否已满
     * @return
     */
    public boolean isFull(){
       return arrInt.length -1 == top;
    }
    /**
     * 向栈顶插入一个元素
     * @param item
     */
    public void push(int item){
       if(isFull()){
           throw new RuntimeException("栈已满");
       }
       arrInt[++top] = item;
    }
   
    /**
     * 从栈顶取出一个元素
     * @return
     */
    public int pop(){
       if(isEmpty()){
           throw new RuntimeException("栈为空");
       }
       return arrInt[top--];
    }
}

 

结果如下:

isEmpty: true

isFull: true

9

8

7

6

5

4

3

2

1

0

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值