public class MyStack {
        private int[] array;
        private int maxSize;
        private int top;
        public MyStack(int size) {
            this.maxSize = size;
            array = new int[size];
            top = -1;
        }
        //入栈
        public void push(int value) {
            if(top<maxSize-1) {
                array[++top] = value;
            }
        }
        //弹出栈顶数据
        public int pop() {
            return array[top--];
        }
        //访问栈顶数据
        public int peek() {
            return array[top];
        }
        //判断栈是否为空
        public boolean isEmpty() {
            return (top==-1);
        }
        //判断栈是否满了
        public boolean isFull() {
            return (top==maxSize-1);
            }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MyStack st = new MyStack(3);
        st.push(1);
        st.push(2);
        st.push(3);
        System.out.println(st.peek());
        while(!st.isEmpty()) {
            System.out.println(st.pop());
        }
    }
}
//

增强栈的功能 1、自动扩容 2、可以存储各种不同类型的数据

import java.util.Arrays;
import java.util.EmptyStackException;

public class ArrayStack {
    private Object[] elemenData;
    private int top;
    private int size;
    //创建一个初始大小为10的栈
    public ArrayStack(){
        this.elemenData = new Object[10];
        this.top = -1;
        this.size = 10;
    }
    public ArrayStack(int initialCapacity) {
        if(initialCapacity<0) {
            throw new IllegalArgumentException("栈初始容量不能小于"+initialCapacity);
        }
        this.elemenData = new Object[initialCapacity];
        this.top = -1;
        this.size = initialCapacity;
    }
    //向栈中放入元素\
    public Object push(Object item) {
        isGrow(top+1);
        elemenData[++top] = item;
        return item;
    }
    //弹出栈顶元素
    public Object pop() {
        Object obj = peek();
        remove(top);
        return obj;
    }
    //获取栈顶元素
    public Object peek() {
        if(top==-1) {
            throw new EmptyStackException();
        }
        return elemenData[top];
    }
    //判断栈是否为空
    public boolean isEmpty() {
        return (top==-1);
    }
    //删除栈顶元素
    public void remove(int top) {
        elemenData[top]=null;
        this.top--;
    }
    public boolean isGrow(int minCapacity) {
        int oldCapacity = size;
        if(minCapacity>=oldCapacity) {
            int newCapacity = 0;
            if((oldCapacity<<1)-Integer.MAX_VALUE>0) {
                newCapacity = Integer.MAX_VALUE;
            }else {
                newCapacity = (oldCapacity<<1);//左移一位,相当于放大2倍
            }
            size = newCapacity;
            int [] newArray = new int[size];
            elemenData = Arrays.copyOf(elemenData, size);
            return true;
        }else {
            return false;
        }
    }
    //利用栈实现字符串的逆序
    public static void testStringReversal() {
        ArrayStack stack = new ArrayStack();
        String str ="how are you";
        char [] ch =str.toCharArray();
        for(char c:ch) {
            stack.push(c);
        }
        while(!stack.isEmpty()) {
            System.out.println(stack.pop());
        }
    }    
    
    //分隔符匹配
    public static void testMatch() {
        ArrayStack stack = new ArrayStack(3);
        String str = "12<a[b{c}]>";
        char [] ch = str.toCharArray();
        for(char c:ch) {
            switch(c) {
            case'{':
            case'[':
            case'<':
                
                stack.push(c);
                break;
            case'}':
            case']':
            case'>':
                if(!stack.isEmpty()) {
                    char cha = stack.pop().toString().toCharArray()[0];
                    if(c=='}'&&cha!='{' || c==']'&&cha!='[' || c=='>'&&cha!='<') {
                        System.out.println("Error:"+ch+"-"+c);
                    }
                }
                break;
            default:
                break;
            }
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
//        ArrayStack stack = new ArrayStack(3);
//        stack.push(1);
//        stack.push(2);
//        stack.push(3);
//        stack.push("abc");
//        System.out.println(stack.peek());
//        stack.pop();
//        stack.pop();
//        stack.pop();
//        System.out.println(stack.peek());
        //testStringReversal();
        testMatch();
    }

}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值