用java代码实现一个自己的栈.

  1. //转载raozhiyong11
  2. package com.rao.util;   
  3.   
  4. import java.util.EmptyStackException;   
  5. import java.util.Stack;   
  6.   
  7. public class MyStack<E> {   
  8.        
  9.     private E[] eleDates;   //数组对象   
  10.     private int topIndex;   //最上一个元素(栈顶元素)的下标   
  11.     private int count;      //元素的个数   
  12.        
  13.     public MyStack(int size) {   
  14.         this.eleDates = (E[])new Object[size];   //初始化数组长度   
  15.         this.topIndex=-1;        //初始化最后一个元素的下边   
  16.         this.count = 0;           //初始化元素的个数   
  17.     }   
  18.        
  19.     public MyStack() {   
  20.         this(10);     //提供一个默认的构造函数,默认初始化数组的长度只能装10个元素   
  21.     }   
  22.        
  23.     //压入   
  24.     public void push(E obj){   
  25.         if (isFull()) {   
  26.             throw new ArrayIndexOutOfBoundsException("栈已经满了,不能再放入元素");   
  27.         }else {   
  28.             count++; //栈中元素的个数+1   
  29.             this.eleDates[++topIndex]=obj;  //在最后一个元素的后面增加当前元素   
  30.         }   
  31.     }   
  32.        
  33.     //弹出栈,并且弹出栈顶元素   
  34.     public E pop(){   
  35.         if (isEmpty()) {   
  36.             throw new EmptyStackException();   
  37.         }else {   
  38.             count--;   //栈中元素的个数-1   
  39.             E movedObj = this.eleDates[topIndex];   
  40.             this.eleDates[topIndex]=null//设置被移除的对象为空   
  41.             topIndex--; //栈的元素下标-1   
  42.             return movedObj;   
  43.         }   
  44.     }   
  45.        
  46.     //弹出栈顶元素   
  47.     public E peek(){   
  48.         if (isEmpty()) {   
  49.             throw new EmptyStackException();   
  50.         }else {   
  51.             return this.eleDates[topIndex];   
  52.         }   
  53.     }   
  54.        
  55.     //判断栈是否为空   
  56.     public boolean isEmpty(){   
  57.         return this.topIndex==-1;   
  58.     }   
  59.        
  60.     //判断栈是否已满   
  61.     public boolean isFull(){   
  62.         return this.topIndex==(this.eleDates.length-1);   
  63.     }   
  64.        
  65.     //获取栈的长度   
  66.     public int getStackSize(){   
  67.         return this.eleDates.length;   
  68.     }   
  69.        
  70.     //  获取栈中元素的个数   
  71.     public int getStackCount(){   
  72.         return this.count;   
  73.     }   
  74.        
  75.     @Override  
  76.     public String toString() {   
  77.         String stackString = "";   
  78.         for (int i = 0; i < eleDates.length; i++) {   
  79.             if (eleDates[i]!=null) {   
  80.                 stackString+=eleDates[i]+" | ";   
  81.             }   
  82.         }   
  83.         return stackString;   
  84.     }   
  85.        
  86.     public static void main(String[] args) {   
  87.         MyStack<Integer> myStack = new MyStack<Integer>(5);   
  88.         myStack.push(2);   
  89.         myStack.push(100);   
  90.         myStack.push(5);   
  91.         myStack.push(12);   
  92.         myStack.push(33);   
  93.            
  94.         System.out.println(myStack);         
  95.         System.out.println(myStack.pop());    
  96.         System.out.println(myStack);         
  97.         System.out.println(myStack.pop());         
  98.         System.out.println(myStack);         
  99.         System.out.println(myStack.pop());       
  100.         System.out.println(myStack);         
  101.   
  102.     }   
  103.   
  104. }  

 

运行结果:

 

Java代码 复制代码
  1. 2 | 100 | 5 | 12 | 33 |    
  2. 33  
  3. 2 | 100 | 5 | 12 |    
  4. 12  
  5. 2 | 100 | 5 |    
  6. 5  
  7. 2 | 100 |   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值