JAVA数组测试题(模拟栈)

JAVA数组测试题(模拟栈)

  • 使用一维数组,模拟栈数据结构
    1、这个栈可以存储java中的任何引用类型数据
    2、在栈中提供push方法模拟压栈(栈满了要有提示信息)
    3、在栈中提供pop方法模拟弹栈(栈空了也有提示信息)
    4、编写测试程序,new栈对象,调用push 和 pop方法来模拟压栈弹栈动作
/*
/*
使用一维数组,模拟栈数据结构
1、这个栈可以存储java中的任何引用类型数据
2、在栈中提供push方法模拟压栈(栈满了要有提示信息)
3、在栈中提供pop方法模拟弹栈(栈空了也有提示信息)
4、编写测试程序,new栈对象,调用push 和 pop方法来模拟压栈弹栈动作
 */
public class MyStack {
    public static void main(String[] args) {
        Animal a1 = new Cat();
        Animal a2 = new Bird();
        Animal[] animals = {a1, a2};
        Animal[] animals1 = {a1};
        Stack stack = new Stack();

        stack.push(animals);
        for (int i = 0; i < stack.elements.length; i++) {
            System.out.println(stack.elements[i]);
        }
        System.out.println("==============================================");
        //将栈压满
        stack.push(animals);
        stack.push(animals);
        stack.push(animals);
        stack.push(animals);
        stack.push(animals1);
        stack.push(animals1);
        for (int i = 0; i < stack.elements.length; i++) {
            System.out.println(stack.elements[i]);
        }
        System.out.println("==============================================");

        stack.pop(3);
        for (int i = 0; i < stack.elements.length; i++) {
            System.out.println(stack.elements[i]);
        }
        System.out.println("==============================================");

        stack.pop();
        for (int i = 0; i < stack.elements.length; i++) {
            System.out.println(stack.elements[i]);
        }
        //将栈全部弹出
        System.out.println("==============================================");
        stack.pop(5);
        for (int i = 0; i < stack.elements.length; i++) {
            System.out.println(stack.elements[i]);
        }

        System.out.println("==============================================");
        stack.pop();
        for (int i = 0; i < stack.elements.length; i++) {
            System.out.println(stack.elements[i]);
        }
    }
}

class Stack {       //栈类

    Object[] elements;   //栈
    int cnt;   //栈帧

    public Stack(){     //初始化栈
        elements = new Object[9];
        cnt = 0;
    }
    //压栈
    public void push (Object[] obj){

        if (cnt == elements.length){
            System.out.println("栈满");
            System.out.println("==============================================");
            return;
        }else if ((cnt + obj.length) > elements.length){
            System.out.println("栈溢出,压栈失败");
            System.out.println("==============================================");
            return;
        }

        System.arraycopy(obj, 0, elements, cnt, obj.length);
        cnt = cnt + obj.length;
    }

    //弹栈:每次只弹一个元素
    public void pop (){
        if (cnt == 0){
            System.out.println("栈空");
            System.out.println("==============================================");
            return;
        }

        elements[cnt - 1] = null;
        cnt--;
    }
    //进行重写,每次可弹出 i 个元素
    public void pop (int i){
        if (cnt == 0){
            System.out.println("栈空");
            System.out.println("==============================================");
            return;
        }
        for (int j = cnt; j > cnt - i; j--){
            elements[j - 1] = null;
        }
        cnt = cnt - i;
    }
}
//运行结果:
array.Cat@1d81eb93
array.Bird@7291c18f
null
null
null
null
null
null
null
==============================================
栈溢出,压栈失败
==============================================
栈满
==============================================
array.Cat@1d81eb93
array.Bird@7291c18f
array.Cat@1d81eb93
array.Bird@7291c18f
array.Cat@1d81eb93
array.Bird@7291c18f
array.Cat@1d81eb93
array.Bird@7291c18f
array.Cat@1d81eb93
==============================================
array.Cat@1d81eb93
array.Bird@7291c18f
array.Cat@1d81eb93
array.Bird@7291c18f
array.Cat@1d81eb93
array.Bird@7291c18f
null
null
null
==============================================
array.Cat@1d81eb93
array.Bird@7291c18f
array.Cat@1d81eb93
array.Bird@7291c18f
array.Cat@1d81eb93
null
null
null
null
==============================================
null
null
null
null
null
null
null
null
null
==============================================
栈空
==============================================
null
null
null
null
null
null
null
null
null

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值