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());
    }
}

測試結果:
在這裏插入圖片描述

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