算法與數據結構-棧(Stack)-Java實現

什麼是棧(Stack)

下壓棧(FIFO queue),或者說棧(queue),是一種基於先進後出策略的集合模型。java

使用場景

只要你留心,就會發現棧這種數據結構在生活中很是常見。算法

你在桌子上放了一摞文件,放文件和取文件就是簡單的棧操做。spring

你打開你的電子郵件帳戶,發現最新的郵件在最前面,若是這個時候有人給你發來新的郵件,你點擊收信,發現新來的郵件又在你未讀郵件列表的最上面,這就是入棧;你從上到下依次點開郵件閱讀,這些惟未讀郵件也就是一一從你的未讀郵件列表移除了,這就是出棧操做。express

你點開一個網頁,而後再點擊網頁中的連接,這樣一直點擊下去,直到你想回退到前面的網頁了,你開始點擊回退按鈕,前面的網頁又一一出棧。一樣的,編輯器的回退功能,也是入棧出棧的例子。數組

Java實現

Stack

咱們先定義棧的接口,一個完整的棧的接口,應該包含以下四個方法,即:bash

  • 入棧
  • 出棧
  • 棧是否爲空
  • 棧中元素數量

下面是棧接口的定義:數據結構

package net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack;

public interface Stack<Item> {

    /**
     * add an item
     *
     * @param item
     */
    void push(Item item);

    /**
     * remove the most recently added item
     *
     * @return
     */
    Item pop();

    /**
     * is the stack empty?
     *
     * @return
     */
    boolean isEmpty();

    /**
     * number of items in the stac
     */
    int size();

}

複製代碼

FixedCapacityStackOfStrings

首先咱們實現一個最簡單的棧:定容棧,即容量固定的棧,棧的元素都爲字符串。編輯器

一個棧的實現須要有盛棧元素的地方,咱們使用數組。ide

還要有標記當前棧元素數量的變量N。單元測試

package net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.impl;

import net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.Stack;

import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;

/**
 * String定容棧:
 * 固定容量的String類型棧
 */
public class FixedCapacityStackOfStrings {

    private String[] a; // stack entries

    private int N;      // size

    public FixedCapacityStackOfStrings(int cap) {
        a = new String[cap];
    }

    public boolean isEmpty() {
        return N == 0;
    }

    public int size() {
        return N;
    }

    public void push(String item) {
        a[N++] = item;
    }

    public String pop() {
        return a[--N];
    }

}

複製代碼
  • 測試
package net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.impl;

public class FixedCapacityStackOfStringsTests {

    public  static void main(String[] args){

        FixedCapacityStackOfStrings fixedCapacityStackOfStrings=new FixedCapacityStackOfStrings(10);
        System.out.println("fixedCapacityStackOfStrings : size="+fixedCapacityStackOfStrings.size()+",isEmpty="+fixedCapacityStackOfStrings.isEmpty());

        fixedCapacityStackOfStrings.push("A");
        fixedCapacityStackOfStrings.push("Aaha");
        System.out.println("fixedCapacityStackOfStrings : size="+fixedCapacityStackOfStrings.size()+",isEmpty="+fixedCapacityStackOfStrings.isEmpty());

        System.out.println("poped="+fixedCapacityStackOfStrings.pop());
        System.out.println("fixedCapacityStackOfStrings : size="+fixedCapacityStackOfStrings.size()+",isEmpty="+fixedCapacityStackOfStrings.isEmpty());

    }
}

複製代碼
  • 測試輸出
fixedCapacityStackOfStrings : size=0,isEmpty=true
fixedCapacityStackOfStrings : size=2,isEmpty=false
poped=Aaha
fixedCapacityStackOfStrings : size=1,isEmpty=false
複製代碼

FixedCapacityStack

FixedCapacityStackOfStrings的缺點是隻能處理String對象,接着咱們是使用泛型,讓咱們的棧實現能夠處理任意對象。

其中Item就是咱們泛型的類型參數。

因爲歷史緣由,Java的數組通常狀況下是不支持泛型的,所以咱們用強轉的方式將Object類型的數組轉爲泛型中的數組類型。

package net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.impl;

import net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.Stack;

/**
 * 支持泛型的定容棧
 * @param <Item>
 */
public class FixedCapacityStack<Item> implements Stack<Item> {

    private Item[] a;

    private int N;

    public FixedCapacityStack(int cap){
        a = (Item[]) new Object[cap];
    }

    @Override
    public void push(Item item) {
        a[N++] = item;
    }

    @Override
    public Item pop() {
        return a[--N];
    }

    @Override
    public boolean isEmpty() {
        return N == 0;
    }

    @Override
    public int size() {
        return N;
    }

}

複製代碼
  • 測試
package net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.impl;

public class FixedCapacityStackTests {

    public  static void main(String[] args){
        FixedCapacityStack<Double> fixedCapacityStack=new FixedCapacityStack<>(10);
        System.out.println("fixedCapacityStack : size="+fixedCapacityStack.size()+",isEmpty="+fixedCapacityStack.isEmpty());

        fixedCapacityStack.push(new Double(10.01));
        fixedCapacityStack.push(new Double(202.22));
        System.out.println("fixedCapacityStack : size="+fixedCapacityStack.size()+",isEmpty="+fixedCapacityStack.isEmpty());

        System.out.println("poped="+fixedCapacityStack.pop());
        System.out.println("fixedCapacityStack : size="+fixedCapacityStack.size()+",isEmpty="+fixedCapacityStack.isEmpty());

    }
}

複製代碼
  • 測試輸出
fixedCapacityStack : size=0,isEmpty=true
fixedCapacityStack : size=2,isEmpty=false
poped=202.22
fixedCapacityStack : size=1,isEmpty=false
複製代碼

ResizingArrayStack

FixedCapacityStack的最大缺點就是容量固定,這就要求咱們在使用棧以前必須估計棧的最大容量,很不方便。

下面咱們就實現容量可變的棧。

咱們用一個新的數組來替換老的數組,從而實現棧的容量擴展。這裏要注意如兩點:

  • 當進行入棧操做的時候,若是棧滿,則將其容量增大一倍,保證接下來能夠屢次入棧。由於頻繁擴展容量也是很耗費內存的。
  • 當進行出棧操做的時候,若是發現只用了棧容量的四分之一,則將棧的容量縮小一半。由於數組若是空着不用,會白白耗費內存。

另外特別注意的是,出棧之後要將指定位置的元素賦值爲null,以防止對象遊離。

Java的垃圾回收策略是回收全部沒法被訪問的對象的內存,若是出棧之後,不將指定位置的元素賦值爲null,那麼保存這樣一個不須要的對象的引用,就稱爲對象的遊離。

經過賦值已經出棧的位置爲null,咱們覆蓋了無效的引用,好讓GC回收這部份內存。

package net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.impl;

import net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.Stack;

/**
 * 容量可變的棧
 *
 * @param <Item>
 */
public class ResizingArrayStack<Item> implements Stack<Item> {

    private Item[] a = (Item[]) new Object[1];

    private int N = 0;

    /**
     * 改變棧的容量大小
     *
     * @param max
     */
    private void resize(int max) {
        // Move stack to a new array of size max.
        Item[] temp = (Item[]) new Object[max];
        for (int i = 0; i < N; i++) {
            temp[i] = a[i];
        }
        a = temp;
    }

    @Override
    public void push(Item item) {
        //若是棧滿,則將其容量增大一倍
        if (N == a.length) {
            resize(2 * a.length);
        }
        a[N++] = item;
    }

    @Override
    public Item pop() {
        Item item = a[--N];
        // 防止對象遊離(loitering)
        a[N] = null;
        //若是棧中已用的容量只佔總容量的1/4,則將棧容量縮小一半
        if (N > 0 && N == a.length / 4) {
            resize(a.length / 2);
        }
        return item;
    }

    @Override
    public boolean isEmpty() {
        return N == 0;
    }

    @Override
    public int size() {
        return N;
    }
}

複製代碼
  • 測試
package net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.impl;

import java.math.BigDecimal;

public class ResizingArrayStackTests {
    public  static void main(String[] args){

        ResizingArrayStack<BigDecimal> resizingArrayStack=new ResizingArrayStack<>();

        System.out.println("resizingArrayStack : size="+resizingArrayStack.size()+",isEmpty="+resizingArrayStack.isEmpty());

        resizingArrayStack.push(new BigDecimal(100.001));
        resizingArrayStack.push(new BigDecimal(202.022));
        System.out.println("resizingArrayStack : size="+resizingArrayStack.size()+",isEmpty="+resizingArrayStack.isEmpty());

        System.out.println("poped="+resizingArrayStack.pop());
        System.out.println("resizingArrayStack : size="+resizingArrayStack.size()+",isEmpty="+resizingArrayStack.isEmpty());

    }
}

複製代碼
  • 測試輸出
resizingArrayStack : size=0,isEmpty=true
resizingArrayStack : size=2,isEmpty=false
poped=202.02199999999999135980033315718173980712890625
resizingArrayStack : size=1,isEmpty=false
複製代碼

IterableResizingArrayStack

下面咱們將爲咱們的棧實現增長迭代器的特性。

事實上,foreach不只僅是for的簡寫形式語法糖這麼簡單,以下foreachwhile循環是等效的:

for(String s:collection){
    s ...
}
複製代碼
while(collection.hasNext()){
    collection.next();
    ...
}
複製代碼

從上面例子能夠看出,迭代器其實就是一個實現了hasNext()next()方法的對象。

若是一個類可迭代,那麼第一步就要聲明實現Iterable接口。

而後咱們經過一個內部類來實現Iterator的hasNext()next()方法從而支持迭代操做。

package net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.impl;

import net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.Stack;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * 可迭代的可變長的基於數組存儲的棧實現
 *
 * @param <Item>
 */
public class IterableResizingArrayStack<Item> implements Stack<Item>, Iterable<Item> {

    private Item[] a = (Item[]) new Object[1];

    private int N = 0;

    /**
     * 改變棧的容量大小
     *
     * @param max
     */
    private void resize(int max) {
        // Move stack to a new array of size max.
        Item[] temp = (Item[]) new Object[max];
        for (int i = 0; i < N; i++) {
            temp[i] = a[i];
        }
        a = temp;
    }

    @Override
    public void push(Item item) {
        //若是棧滿,則將其容量增大一倍
        if (N == a.length) {
            resize(2 * a.length);
        }
        a[N++] = item;
    }

    @Override
    public Item pop() {
        Item item = a[--N];
        // 防止對象遊離(loitering)
        a[N] = null;
        //若是棧中已用的容量只佔總容量的1/4,則將棧容量縮小一半
        if (N > 0 && N == a.length / 4) {
            resize(a.length / 2);
        }
        return item;
    }

    @Override
    public boolean isEmpty() {
        return N == 0;
    }

    @Override
    public int size() {
        return N;
    }

    @Override
    public Iterator<Item> iterator() {
        return new ReverseArrayIterator();
    }

    //支持迭代方法,實如今內部類裏
    private class ReverseArrayIterator implements Iterator<Item> {
        // Support LIFO iteration.
        private int i = N;

        @Override
        public boolean hasNext() {
            return i > 0;
        }

        @Override
        public Item next() {
            if(i<=0){
                throw new NoSuchElementException();
            }

            return a[--i];
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }
}

複製代碼
  • 測試
package net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.impl;

import java.math.BigDecimal;

public class IterableResizingArrayStackTests {
    public static void main(String[] args) {

        IterableResizingArrayStack<Float> resizingArrayStack = new IterableResizingArrayStack<>();

        System.out.println("resizingArrayStack : size=" + resizingArrayStack.size() + ",isEmpty=" + resizingArrayStack.isEmpty());

        resizingArrayStack.push(new Float(100.001));
        resizingArrayStack.push(new Float(202.022));
        System.out.println("resizingArrayStack : size=" + resizingArrayStack.size() + ",isEmpty=" + resizingArrayStack.isEmpty());

        System.out.println("resizingArrayStack all items:");
        for (Float f:resizingArrayStack) {
            System.out.println(f);
        }

        System.out.println("poped=" + resizingArrayStack.pop());
        System.out.println("resizingArrayStack : size=" + resizingArrayStack.size() + ",isEmpty=" + resizingArrayStack.isEmpty());

    }
}

複製代碼
  • 測試輸出
resizingArrayStack : size=0,isEmpty=true
resizingArrayStack : size=2,isEmpty=false
resizingArrayStack all items:
202.022
100.001
poped=202.022
resizingArrayStack : size=1,isEmpty=false
複製代碼

應用示例

判斷括號是否爲成對出現

要求一個字符串中,若是有括號的話,全部括號,必須是成對出現的。

寫一個檢查器檢查指定字符串是否符合上面的原則。

根據TDD(測試驅動開發)的開發方法,先把單元測試寫好:

package net.ijiangtao.tech.algorithms.algorithmall.algorithm.stack.evaluation;

import net.ijiangtao.tech.algorithms.algorithmall.algorithm.stack.checker.LegalParenthesesChecker;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * 括號必須成對出現,此程序基於棧結構,用於檢測經常使用括號是否爲成對出現,並表達式輸出是否合法。
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class LegalParenthesesCheckerTests {

    @Test
    public void testChecker(){

        Assert.assertFalse(LegalParenthesesChecker.check("1}"));
        Assert.assertFalse(LegalParenthesesChecker.check("[1}"));
        Assert.assertFalse(LegalParenthesesChecker.check("[1]}"));
        Assert.assertFalse(LegalParenthesesChecker.check("(((1+1)+2)+3"));
        Assert.assertFalse(LegalParenthesesChecker.check("<((1+1)+2)+3"));

        Assert.assertTrue(LegalParenthesesChecker.check(""));
        Assert.assertTrue(LegalParenthesesChecker.check(" "));
        Assert.assertTrue(LegalParenthesesChecker.check("1"));
        Assert.assertTrue(LegalParenthesesChecker.check("[]"));
        Assert.assertTrue(LegalParenthesesChecker.check("[1]"));
        Assert.assertTrue(LegalParenthesesChecker.check("{(『((<1+1>)+【2】)+』3)}"));
    }

}

複製代碼

下面開始寫實現。

package net.ijiangtao.tech.algorithms.algorithmall.algorithm.stack.checker;

import net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.Stack;
import net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.impl.IterableResizingArrayStack;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 括號必須成對出現,此程序基於棧結構,用於檢測經常使用括號是否爲成對出現,並表達式輸出是否合法。
 */
public class LegalParenthesesChecker {

    private static final String BRACKET="<>()()〈〉‹›﹛﹜『』〖〗[]《》﹝﹞〔〕{}「」【】︵︶︷︸︿﹀︹︺︽︾﹁﹂﹃﹄︻︼";

    public static boolean check(String expression) {

        //若是傳入的表達式爲空則不須要進行括號成對判斷
        if (null == expression || expression.length() < 1) {
            return true;
        }

        //將傳入的表達式拆分爲char數組
        char[] expressionsChars = expression.toCharArray();

        //將全部須要判斷的括號拆分爲char數組並進行sort,其中index爲偶數的永遠是左半個括號,index爲奇數的是右半個括號
        char[] brackets = BRACKET.toCharArray();
        //使用binarySearch以前,須要sort數組
        Arrays.sort(brackets);

        //用於保存棧容器和括號之間的對應關係
        Map<Character, Stack> map = new HashMap<>();

        //遍歷表達式的每一個字符
        for (char c : expressionsChars) {
            //判斷該字符是否爲括號
            int index = Arrays.binarySearch(brackets, c);
            //負數,不是括號,不須要處理
            if (index < 0) {
                continue;
            }
            //偶數,是左括號,則放入棧中
            if (index % 2 == 0) {
                //取出map中該左括號對應的棧容器
                Stack<Character> stack = map.get(c);
                //若是該左括號對應的key是第一次存入map,則建立一個棧容器
                if (null == stack) {
                    stack = new IterableResizingArrayStack<>();
                }
                stack.push(c);
                map.put(c, stack);
            } else {
                //奇數,是右括號,則先找到該右括號對應的左括號
                char left = brackets[index - 1];
                //取出左括號對應的棧容器中的值
                Stack<Character> stack = map.get(left);
                //若是該右括號沒有對應的左括號與之匹配,則表示此表達式中的括號不成對,不合法
                if (null == stack || stack.size() < 1) {
                    return false;
                } else {
                    stack.pop();
                    continue;
                }

            }

        }

        //若是map中還有左側括號,表示左側括號比右側多,則返回false
        for (char k : map.keySet()) {
            if(!map.get(k).isEmpty()){
                return false;
            }
        }

        return true;
    }
}
複製代碼

通過單元測試驗證,該檢查器知足要求。

雙棧算術表達式求值算法

Dijkstra的雙棧算術表達式求值算法(Dijkstra's two-stack algorithm for expression evaluation)是由E.W.Dijkstra在上個世紀60年代發明的一個很簡單的算法,用兩個棧:一個用來保存運算符、一個用來保存操做數,來完成對一個表達式的運算。

其實整個算法思路很簡單:

  • 無視左括號
  • 將操做數壓入操做數棧
  • 將運算符壓入運算符棧
  • 在遇到右括號的時候,從運算符棧中彈出一個運算符,再從操做數棧中彈出所需的操做數,而且將運算結果壓入操做數棧中
package net.ijiangtao.tech.algorithms.algorithmall.algorithm.stack.evaluation;

import net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.Stack;
import net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.impl.IterableResizingArrayStack;

public class DijkstrasTwoStackAlgorithmForExpressionEvaluation {

    public Double cal(String expression) {

        String[] expressionArr = expression.split(" ");

        Stack<String> ops = new IterableResizingArrayStack<String>();
        Stack<Double> vals = new IterableResizingArrayStack<Double>();

        for (String s : expressionArr) {
            // Read token, push if operator.
            if (s.equals("(")) {
                ;
            } else if (s.equals("+")) {
                ops.push(s);
            } else if (s.equals("-")) {
                ops.push(s);
            } else if (s.equals("*")) {
                ops.push(s);
            } else if (s.equals("/")) {
                ops.push(s);
            } else if (s.equals("sqrt")) {
                ops.push(s);
            } else if (s.equals(")")) {
                // Pop, evaluate, and push result if token is ")"
                String op = ops.pop();
                double v = vals.pop();

                if (op.equals("+")) {
                    v = vals.pop() + v;
                } else if (op.equals("-")) {
                    v = vals.pop() - v;
                } else if (op.equals("*")) {
                    v = vals.pop() * v;
                } else if (op.equals("/")) {
                    v = vals.pop() / v;
                } else if (op.equals("sqrt")) {
                    v = Math.sqrt(v);
                }

                vals.push(v);
            }
            // Token not operator or paren: push double value.
            else {
                vals.push(Double.parseDouble(s));
            }
        }

        return vals.pop();
    }

}

複製代碼
  • 測試
package net.ijiangtao.tech.algorithms.algorithmall.algorithm.stack.evaluation;

public class DijkstrasTwoStackAlgorithmForExpressionEvaluationTests {

    public  static void main(String[] args){
        DijkstrasTwoStackAlgorithmForExpressionEvaluation expressionEvaluation=new DijkstrasTwoStackAlgorithmForExpressionEvaluation();
        System.out.println(expressionEvaluation.cal("( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )"));
    }

}
複製代碼
  • 測試輸出:
101.0
複製代碼
相關文章
相關標籤/搜索