Java数组实现栈和队列

队列实现:

package com.example.javabase.test;

/**
 * @author XuJD
 * @create 2019-11-21 10:11
 * 基于数组实现队列
 **/
public class Queue3 {
    /**
     * 队列
     */
    private String[] data;
    /**
     * 队列最大长度
     */
    private int maxSize;
    /**
     * 队列头节点
     */
    private int first;
    /**
     * 队列尾节点
     */
    private int last;

    public Queue3(int maxSize){
        if(maxSize>=0){
            this.maxSize = maxSize;
            this.first = 0;
            this.last = 0;
            data = new String[maxSize];
        }else{
            throw new RuntimeException("队列大小不能小于0");
        }
    }

    /**
     * 判断队列是否已满
     * @return
     */
    public boolean isFull(){
        return maxSize==(last-first);
    }
    /**
     * 判断队列是否为空
     */
    public  boolean isEmpty(){
        return first==last;
    }

    /**
     * 添加数据到队列尾部
     * @param param
     */
    public void add(String param){
        if(isFull()){
            throw new RuntimeException("队列已满");
        }else{
            data[last++] = param;
        }
    }

    /**
     * 获取队列头部元素
     */
    public String getFirst(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }else{
            return data[first];
        }
    }
    /**
     * 获取尾部元素
     */
    public String getLast(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }else{
            return data[last-1];
        }
    }
    /**
     * 获取头部元素并移除
     */
    public String pop(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }else{
            String val = data[first];
            data[first++] = null;
            return val;
        }
    }
    /**
     * 返回队列长度
     * @return
     */
    public int length(){
        return last-first;
    }


    public static void main(String[] args) {
        Queue3 q = new Queue3(3);
        q.add("1");
        q.add("2");
        q.add("3");
        System.out.println(q.pop());
        System.out.println(q.getFirst());
        System.out.println(q.getLast());
        System.out.println(q.isEmpty());
        System.out.println(q.isFull());
        System.out.println(q.length());
    }
}

测试结果:
在这里插入图片描述

栈实现:

package com.example.javabase.test;

/**
 * @author XuJD
 * @create 2019-11-21 10:51
 * 基于数组实现栈
 **/
public class Stack3 {
    /**
     * 栈元素数组
     */
    private String[] data;
    /**
     * 栈大小
     */
    private int maxSize;
    /**
     * 栈顶节点
     */
    private int top;

    public Stack3(int maxSize){
        if(maxSize>=0){
            this.maxSize = maxSize;
            this.top = 0;
            data = new String[maxSize];
        }else{
            throw new RuntimeException("栈初始化小大不能小于0");
        }
    }

    /**
     * 判断是否为空栈
     */
    public boolean isEmpty(){
        return top==0;
    }

    /**
     * 判断栈是否已满
     */
    public boolean isFull(){
        return maxSize==top;
    }

    /**
     * 元素入栈
     */
    public void push(String param){
        if(isFull()){
            throw new RuntimeException("栈已满");
        }else{
            data[top++] = param;
        }
    }

    /**
     * 出栈
     * @return
     */
    public String pop(){
        if(isEmpty()){
            throw new RuntimeException("空栈");
        }else{
            String val = data[top-1];
            data[top-1] = null;
            top--;
            return val;
        }
    }

    /**
     * 返回栈顶元素
     * @return
     */
    public String peek(){
        return data[top-1];
    }

    public static void main(String[] args) {
        Stack3 s = new Stack3(3);
        s.push("1");
        s.push("2");
        s.push("3");
        System.out.println(s.pop());
        System.out.println(s.peek());
        System.out.println(s.isEmpty());
        System.out.println(s.isFull());
    }
}

测试结果:
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章