栈和队列

1.栈

public class MyStack {

    private int arr[];
    private int top;

    public MyStack() {
        arr = new int[50];
        top = -1;
    }

    public MyStack(int maxSize) {
        arr = new int[maxSize];
        top = -1;
    }

    /**
     * 
     * @Title: push   
     * @Description: 压栈  
     * @param value
     */
    public void push(int value) {
        arr[++top] = value;
    }

    /**
     * 
     * @Title: pop   
     * @Description: 出栈  
     * @return
     * @throws Exception
     */
    public int pop() throws Exception {
        if(isEmpty()) {
            throw new Exception("The stack is Empty!");
        }else {
            return arr[top--];
        }

    }

    /**
     * 
     * @Title: isEmpty   
     * @Description: 判断是否为空  
     * @return
     */
    public boolean isEmpty() {
        return top == -1;
    }

    /**
     * 
     * @Title: isFull   
     * @Description: 判断是否已满  
     * @return
     */
    public boolean isFull() {
        return top == arr.length - 1;
    }

    /**
     * 
     * @Title: peek   
     * @Description: 查看   
     * @return
     * @throws Exception
     */
    public int peek() throws Exception {
        if(isEmpty()) {
            throw new Exception("The stack is Empty!");
        }else {
            return arr[top];
        }
    }

}

2.队列

public class MyQueue {
    private int arr [];
    private int elements;
    private int front;
    private int end;

    public MyQueue() {
        arr = new int[50];
        elements = 0;
        front = 0;
        end = -1;
    }

    public MyQueue(int maxSize) {
        arr = new int[maxSize];
        elements = 0;
        front = 0;
        end = -1;
    }

    /**
     * 
     * @Title: add   
     * @Description: 添加数据  
     * @param value
     */
    public void add(int value) {
        arr[++end] = value;
        elements ++;
    }


    /**
     * 
     * @Title: remove   
     * @Description: 删除数据  
     * @return
     */
    public int remove() {
        elements --;
        return arr[front++];

    }

    /**
     * 
     * @Title: peek   
     * @Description: TODO  
     * @return
     */
    public int peek() {
        return arr[front];
    }

    /**
     * 
     * @Title: isEmpty   
     * @Description: 判断是否为空  
     * @return
     */
    public boolean isEmpty() {
        return elements == 0;
    }

    /**
     * 
     * @Title: isFull   
     * @Description: 判断是否已满  
     * @return
     */
    public boolean isFull() {
        return elements == arr.length;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值