Java資料結構和演算法——棧__資料結構

來源:互聯網
上載者:User
簡介

棧(stack),是一種線性儲存結構,它有以下幾個特點:
1. 棧中資料是按照”後進先出(LIFO, Last In First Out)”方式進出棧的。
2. 向棧中添加/刪除資料時,只能從棧頂進行操作。

棧通常包括的三種操作:push、peek、pop。
push – 向棧中添加元素。
peek – 返回棧頂元素。
pop – 返回並刪除棧頂元素的操作。 棧的示意圖

棧中的資料依次是 30 –> 20 –> 10 出棧

出棧前:棧頂元素是30。此時,棧中的元素依次是 30 –> 20 –> 10
出棧後:30出棧之後,棧頂元素變成20。此時,棧中的元素依次是 20 –> 10 入棧

入棧前:棧頂元素是20。此時,棧中的元素依次是 20 –> 10
入棧後:40入棧之後,棧頂元素變成40。此時,棧中的元素依次是 40 –> 20 –> 10 棧的Java實現

Java中也提供了『棧』的實現,它就是集合架構中Stack類。

本部分給出2種Java實現
Java實現一:數組實現的棧,能儲存任意類型的資料。
Java實現二:Java的 Collection集合 中內建的”棧”(stack)的樣本。

1.數組實現的棧,能儲存任意類型的資料

實現代碼(GeneralArrayStack.java)

package com.qq.main;import java.lang.reflect.Array;public class GeneralArrayStack<T> {    private static final int DEFAULT_SIZE = 12;    private T[] mArray;    private int count;    public GeneralArrayStack(Class<T> type) {        this(type, DEFAULT_SIZE);    }    @SuppressWarnings("unchecked")    public GeneralArrayStack(Class<T> type, int size) {        mArray = (T[]) Array.newInstance(type, DEFAULT_SIZE);        count = 0;    }    public void push(T t) {        mArray[count++] = t;    }    public T peek() {        return mArray[count - 1];    }    public T pop() {        T t = mArray[count - 1];        count--;        return t;    }    public int size() {        return count;    }    // 返回“棧”是否為空白    public boolean isEmpty() {        return size() == 0;    }    // 列印“棧”    public void PrintArrayStack() {        if (isEmpty()) {            System.out.printf("stack is Empty\n");        }        System.out.printf("stack size()=%d\n", size());        int i=size()-1;        while (i>=0) {            System.out.println(mArray[i]);            i--;        }    }    public static void main(String[] args) {        String temp;        GeneralArrayStack<String> stack = new GeneralArrayStack<String>(String.class);        stack.push("10");        stack.push("20");        stack.push("30");        // 將“棧頂元素”賦值給tmp,並刪除“棧頂元素”        temp = stack.pop();        System.out.println("tmp="+temp);        // 只將“棧頂”賦值給tmp,不刪除該元素.        temp = stack.peek();        System.out.println("tmp="+temp);        stack.PrintArrayStack();    }}

運行輸出結果:

tmp=30tmp=20stack size()=3402010

結果說明:GeneralArrayStack是通過數組實現的棧,而且GeneralArrayStack中使用到了泛型。

2.Java的 Collection集合 中內建的”棧”(stack)的樣本

實現代碼(StackTest.java)

package com.qq.main;import java.util.Stack;public class StackTest {    public static void main(String[] args) {        Integer temp = 0;        Stack<Integer> stack = new Stack<Integer>();        stack.push(10);        stack.push(20);        stack.push(30);        temp = stack.pop();        System.out.printf("temp=%d\n", temp);        temp = stack.peek();        System.out.printf("temp=%d\n", temp);        stack.push(40);        while (!stack.isEmpty()) {            temp = stack.pop();            System.out.printf("temp=%d\n", temp);        }    }}

輸出結果;

temp=30temp=20temp=40temp=20temp=10

至此,對棧的瞭解有了一定的深入,基本上能使用了。

學習部落格:http://www.cnblogs.com/skywang12345/p/3562239.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.