三分取基法

原栈

class Stack{
    int top;
    int[] elem;
    public Stack(){
        this(10);
    }
    public Stack(int size){
        this.elem = new int[size];
        this.top = 0;
    }
    /**
     * 栈是否已满
     */
    public boolean isFull(){
        if(this.top == this.elem.length){
            return true;
        }
        return false;
    }
    /**
     * 入栈
     */
    public boolean push(int val){
        if(isFull()){
            return false;
        }
        this.elem[top++] = val;
        //this.top()++;
        return true;
    }
    /**
     * 是否为空
     */
    public boolean isEmpty(){
        if(this.top == 0){
            return true;
        }
        return false;
        //return this.top = 0;直接代替
    }
    /**
     * 出栈
     */
    public int pop(){
        if(isEmpty()){
            return -1;
        }

        return elem[--this.top];
    } 
    /**
     * 得到栈的元素
     */
    public int getTop(){
        if(isEmpty()){
            return -1;
        }
        //不能进行--top,会将top值减掉
        return this.elem[this.top-1];
    }
    /**
     * 打印栈内元素
     */
    public void show(){
        for(int i =0;i<this.top;i++){
            System.out.println(this.elem[i]+" ");
        }
        System.out.println();
    }
}
/**
 * 
 * @author jhl
 *
 */
public class Test3stack {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Stack s1 = new Stack();
        int length = 10;
        for(int i = 0;i<length;i++){
            s1.push(i);
        }
        s1.show();
    }
}

泛型栈

class Stack<T>{
    private int top;
    private T[] elem = null;
    public Stack(){
        this(10);
    }
    public Stack(int size){
        this.elem = (T[])new Object[size];
        this.top = 0;
    }
    /**
     * 栈是否已满
     */
    public boolean isFull(){
        if(this.top == this.elem.length){
            return true;
        }
        return false;
    }
    /**
     * 入栈
     */
    public boolean push(T val){
        if(isFull()){
            return false;
        }
        this.elem[top++] = val;
        //this.top()++;
        return true;
    }
    /**
     * 是否为空
     */
    public boolean isEmpty(){
        if(this.top == 0){
            return true;
        }
        return false;
        //return this.top = 0;直接代替
    }
    /**
     * 出栈
     */
    public T pop(){
        if(isEmpty()){
            return null;
        }

        return elem[--this.top];
    } 
    /**
     * 得到栈的元素
     */
    public T getTop(){
        if(isEmpty()){
            return null;
        }
        //不能进行--top,会将top值减掉
        return this.elem[this.top-1];
    }
    /**
     * 打印栈内元素
     */
    public void show(){
        for(int i =0;i<this.top;i++){
            System.out.println(this.elem[i]+" ");
        }
        System.out.println();
    }
}
public class Tstack {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Stack<Integer> s1 = new Stack<Integer>();
        int length = 10;
        for (int i = 0; i < length; i++) {
            s1.push(i);
        }
        s1.show();
    }

}

运行结果

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值