1. 程式人生 > >java資料結構——棧

java資料結構——棧

棧是一種特殊的線性表,僅能線上性表的一端操作,棧頂允許操作,棧底不允許操作,遵循“後進先出”的特點。資料結構圖如下:

基本操作包括建立棧、入棧、出棧、獲取棧頂元素、獲取棧的大小等。棧的實現分為兩種,一種是通過陣列實現;一種是通過連結串列來實現。二者的區別是陣列實現棧簡單簡潔,而使用連結串列實現比較複雜;組數的實現容量有限,需要指定初始容量,而連結串列的實現棧的容量是無限的,不需要指定初始容量。接下來分別介紹:

一、陣列實現棧

棧的構造方法:

public class ArrayStack {

    private int[] stack;
    //棧的大小
    private int size;
    //棧頂資料
    private int top;

    /**
     * 預設棧的大小
     */
    public ArrayStack() {
        stack = new int[5];
        top = -1;
        size = 0;
    }

    /**
     * 指定棧的大小
     */
    public ArrayStack(int size) {
        stack = new int[size];
        top = -1;
        size = 0;
    }
}

1、入棧:棧頂插入元素

    /**
     * 存入資料
     */
    public void push(int data) {
        if (size == stack.length) {
            throw new RuntimeException("stack is full");
        }
        stack[size++] = data;
        top = data;
    }

2、出棧:棧頂刪除元素

    /**
     * 刪除資料
     */
    public int pop() {
        if (isEmpty()) {
            top = -1;
            return -1;
        }
        int data = stack[size - 1];
        top = stack[--size];
        return data;
    }

3、檢視棧內元素

    /**
     * 檢視棧內元素
     */
    public void display() {
        if (isEmpty()) {
            System.out.println("棧內元素為空");
            return;
        }
        for (int i = 0; i < size; i++) {
            System.out.print(stack[i] + " ");
        }
        System.out.println();
    }

4、檢視棧頂元素

    /**
     * 檢視棧頂元素
     */
    public int getTop() {
        return top;
    }

5、檢視棧內元素大小

    /**
     * 棧的實際大小
     */
    public int size() {
        return size;
    }

6、棧是是否為空

    /**
     * 棧是否為空
     */
    public boolean isEmpty() {
        return size() == 0;
    }

7、棧是否滿了

    /**
     * 棧是否滿了
     */
    public boolean isFull() {
        return size() == stack.length;
    }

二、連結串列實現棧

連結串列的節點:

public class Node {
    //下一個節點
    public Node next;
    //當前節點的資料
    public String data;

    public Node(String data) {
        this.data = data;
        this.next = null;
    }
}

棧的構造方法:

public class LinkedListStack {

    //棧頂
    private Node top;
    //棧底元素
    private Node bottom;
    //棧的長度
    private int size;

    public LinkedListStack() {
        top = null;
        size = 0;
    }
}

1、入棧:棧頂插入元素

    /**
     * 插入資料
     */
    public void push(String data) {
        Node node = new Node(data);
        if (isEmpty()) {
            //第一次進來棧底元素和棧頂元素一致
            bottom = node;
            top = node;
        } else {
            //棧頂元素指向下一個,並且棧頂重置
            top.next = node;
            top = node;
        }
        size++;
    }

2、出棧:棧頂刪除元素

    /**
     * 刪除元素
     */
    public void pop() {
        if (isEmpty()) {
            System.out.println("棧內資料為空,沒有可刪除的資料");
            return;
        }
        Node lastNode = null;
        Node currentNode = bottom;
        //當前元素的下一個元素為空跳出迴圈
        while (currentNode.next != null) {
            //上一個元素指向下一個元素
            lastNode = currentNode;
            //向下迴圈走一步
            currentNode = currentNode.next;
        }
        //執行完畢上一個元素指向下一個元素設定為null
        lastNode.next = null;
        //棧頂元素重置
        top = lastNode;
        size--;
    }

3、檢視棧內元素

    /**
     * 檢視棧內元素
     */
    public void display() {
        if (isEmpty()) {
            System.out.println("棧內元素為空");
            return;
        }
        Node currentNoe = bottom;
        while (currentNoe != null) {
            System.out.print(currentNoe.data + " ");
            currentNoe = currentNoe.next;
        }
        System.out.println();
    }

4、檢視棧頂元素

    /**
     * 檢視棧頂元素
     */
    public String getTop() {
        if (top == null){
            return null;
        }
        return top.data;
    }

5、檢視棧內元素大小

    /**
     * 棧的實際大小
     */
    public int size() {
        return size;
    }

6、棧是否為空

    /**
     * 棧是否為空
     */
    public boolean isEmpty() {
        return size() == 0;
    }