Java實戰(六)用鏈表實現棧(自定義異常、接口)

定義棧接口,並用鏈表實現,並採用自定義異常處理棧滿和棧空時的錯誤操作。

(1)定義接口
(2)定義異常類
(4)採用鏈表實現棧類
(5)定義測試類

代碼示例:

package xiaoji.xiaohan;

/*
    定義棧接口,並分別採用數組和鏈表實現,並採用自定義異常處理棧滿和棧空時的錯誤操作。
    (1)定義接口
    (2)定義異常類
    (4)採用鏈表實現棧類
    (5)定義測試類
 */

//自定義異常類,繼承Exception類
class StackException extends Exception
{
    int index = 0;

    public StackException(int index) {
        this.index = index;
    }

    @Override
    public String toString() {
        return "StackExceptionAt{ " + "index=" + index + " }";
    }
}

//定義接口
interface MyStack
{
    int pop() throws StackException;
    void push(int value) throws StackException;
    int size();
}



//實現接口,定義棧類
class ArrayStack implements MyStack
{
    //定義棧大小
    int size = 0;

    class Element
    {
        //節點的值
        public int value = 0;
        //指向下一個節點
        private Element next = null;
    }

    //頭結點
    private Element header = null;

    //初始化鏈表
    public ArrayStack(int size) {
        header = new Element();
        header.value = 0;
        header.next = null;
        this.size = size;
    }

    //重寫接口方法
    @Override
    public int pop() throws StackException {
        //判斷棧是否爲空
        if (size() == 0) throw new StackException(-1);

        //若不爲空
        Element temp = header;
        while(temp.next.next != null)
        {
            temp=temp.next;
        }

        int value = temp.next.value;
        temp.next = null;
        return value;
    }

    @Override
    public void push(int value) throws StackException {
        //若棧滿,拋出異常
        if (this.size == size()) throw new StackException(size+1);

        //若未滿,在尾部壓棧
        //創建一個新的節點
        Element e = new Element();
        e.value = value;
        //若插入到頭節點後
        if (header.next == null)
        {
            header.next = e;
        }
        //不是插入到頭節點後
        else
        {
            Element temp = header;
            //尋找最後一個元素
            while (temp.next != null)
            {
                temp = temp.next;
            }
            temp.next = e;
        }
    }
    /**
     * 鏈表長度
     * */
    public int size()
    {
        Element temp = header;
        int size=0;
        while(temp.next!=null)
        {
            size++;
            temp=temp.next;
        }
        return size;
    }
}


public class Test6 {
    public static void main(String[] args) {
        System.out.println("鏈表實現:");

        //定義一個長度大小爲3的棧
        ArrayStack arrayStack = new ArrayStack(3);

        //此時隊列爲空,取出元素會產生異常
        try {
            arrayStack.pop();
        } catch (StackException e) {
            System.out.println(e.toString());
        }

        //此時加入插入四個元素(>3)隊列會產生異常
        try {
            arrayStack.push(1);
            System.out.println("棧頂壓棧的值爲:1");
            arrayStack.push(2);
            System.out.println("棧頂壓棧的值爲:2");
            arrayStack.push(3);
            System.out.println("棧頂壓棧的值爲:3");
            arrayStack.push(4);
            System.out.println("棧頂壓棧的值爲:4");
        } catch (StackException e) {
            System.out.println(e.toString());
        }

        //嘗試取出棧頂元素
        try {
            System.out.println("取出棧頂元素的值爲:"+arrayStack.pop());
            System.out.println("取出棧頂元素的值爲:"+arrayStack.pop());
            System.out.println("取出棧頂元素的值爲:"+arrayStack.pop());
            System.out.println("取出棧頂元素的值爲:"+arrayStack.pop());
        } catch (StackException e) {
            System.out.println(e.toString());
        }
    }
}

運行結果:

在這裏插入圖片描述

參考文章:https://www.cnblogs.com/lixiaolun/p/4643886.html

在這裏插入圖片描述

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