数据结构 用数组模拟栈 回纹字符串

public class ArrayStack {
    //数组的大小
    private int mayStack;
    //模拟栈的数组
    private int[] stack;
    //指向栈顶的指针
    private int top=-1;
    public ArrayStack(int mayStack){
        this.mayStack=mayStack;
        stack= new int[mayStack];//数组在使用时我们必须先初始化其大小 知识点1
    }
    //栈是否为空
    public boolean Empty(){
        return this.top==-1;
    }
    //栈是否为满
    public boolean Full(){
        return this.top==this.mayStack-1;
    }
    //进行压栈
    public void Push(int value){
        if(Full()){
            throw new RuntimeException("栈已经满了"); //知识点2 运行时异常
        }
        top++;
        stack[top]=value;
    }
    //进行弹栈
    public int Pop(){
        if(Empty()){
            throw new RuntimeException("此栈已空");
        }
        int value = stack[top];
        top--;
        return value;
    }
    //进行栈元素的遍历
    public void Search(){
        if(Empty()){
            throw new RuntimeException("此栈已空");
        }
        for(int i=0;i<stack.length;i++){
            System.out.print("stack["+i+"]="+stack[i]+"\t");
        }
    }
    //返回栈的元素的个数
    public int Length(){
        return this.top+1;
    }
}

public class Test {

    public static void main(String[] args) {
//        System.out.println(arrayStack.Empty());
        arrayStack.Pop();
//        arrayStack.Push(0);
//        arrayStack.Push(1);
//        arrayStack.Push(2);
//        arrayStack.Push(3);
//        arrayStack.Push(4);
//        arrayStack.Push(5);
//        arrayStack.Push(6);
//        arrayStack.Push(7);
//        arrayStack.Push(8);
//        arrayStack.Push(9);
        arrayStack.Push(9);
//        System.out.println(arrayStack.Full());
        boolean test=huiwen("hahhaha");
        System.out.println(test);
    }

    private static boolean huiwen(String str) {
        ArrayStack arrayStack= new ArrayStack(10);
        if(!arrayStack.Full()){
            for(int i=0;i<str.length();i++){
                arrayStack.Push(str.charAt(i));//知识点3
            }
        }
        String string="";//知识点4
        int length = arrayStack.Length();
        for(int j=0;j<length;j++){
            char pop = (char)arrayStack.Pop();//知识点6
            string+=pop;
        }
        if(str.equals(string)){//知识点5
            return true;
        }else {
            return false;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值