Java 類比棧結構

來源:互聯網
上載者:User

標籤:UI   i++   out   obj   print   時間複雜度   link   ring   void   

棧和隊列:

通常是作為程式猿的工具,用於輔助構思演算法。生命週期較短,執行時才被建立

訪問受限。在特定時刻,僅僅有一個資料可被讀取或刪除

是一種抽象的結構。內部的實現機制。對使用者不可見。比方用數組、鏈表來實現棧

棧:

同一時候,僅僅同意一個資料被訪問,後進先出

對於入棧和出棧的時間複雜度都為O(1),即不依賴棧內資料項目的個數,操作比較快

例,使用數組作為棧的儲存結構

public class StackS<T> {private int max;private T[] ary;private int top;//指標,指向棧頂元素的下標public StackS(int size) {this.max = size;ary = (T[]) new Object[max];top = -1;}// 入棧public void push(T data) {if (!isFull())ary[++top] = data;}// 出棧public T pop() {if (isEmpty()) {return null;}return ary[top--];}// 查看棧頂public T peek() {return ary[top];}//棧是否為空白public boolean isEmpty() {return top == -1;}//棧是否滿public boolean isFull() {return top == max - 1;}//sizepublic int size() {return top + 1;}public static void main(String[] args) {StackS<Integer> stack = new StackS<Integer>(3);for (int i = 0; i < 5; i++) {stack.push(i);System.out.println("size:" + stack.size());}for (int i = 0; i < 5; i++) {Integer peek = stack.peek();System.out.println("peek:" + peek);System.out.println("size:" + stack.size());}for (int i = 0; i < 5; i++) {Integer pop = stack.pop();System.out.println("pop:" + pop);System.out.println("size:" + stack.size());}System.out.println("----");for (int i = 5; i > 0; i--) {stack.push(i);System.out.println("size:" + stack.size());}for (int i = 5; i > 0; i--) {Integer peek = stack.peek();System.out.println("peek:" + peek);System.out.println("size:" + stack.size());}for (int i = 5; i > 0; i--) {Integer pop = stack.pop();System.out.println("pop:" + pop);System.out.println("size:" + stack.size());}}}

上面的範例。有一個maxSize的規定。由於數組是要規定大小的,若想無限制,能夠使用其它結構來做儲存,當然也能夠new一個新的長度的數組。

例。使用LinkedList儲存來實現棧

/** * 使用LinkedList儲存來實現棧 * @author stone * * @param <T> */public class StackSS<T> {private LinkedList<T> datas;public StackSS() {datas = new LinkedList<T>();}// 入棧public void push(T data) {datas.addLast(data);}// 出棧public T pop() {return datas.removeLast();}// 查看棧頂public T peek() {return datas.getLast();}//棧是否為空白public boolean isEmpty() {return datas.isEmpty();}//sizepublic int size() {return datas.size();}public static void main(String[] args) {StackS<Integer> stack = new StackS<Integer>(3);for (int i = 0; i < 5; i++) {stack.push(i);System.out.println("size:" + stack.size());}for (int i = 0; i < 5; i++) {Integer peek = stack.peek();System.out.println("peek:" + peek);System.out.println("size:" + stack.size());}for (int i = 0; i < 5; i++) {Integer pop = stack.pop();System.out.println("pop:" + pop);System.out.println("size:" + stack.size());}System.out.println("----");for (int i = 5; i > 0; i--) {stack.push(i);System.out.println("size:" + stack.size());}for (int i = 5; i > 0; i--) {Integer peek = stack.peek();System.out.println("peek:" + peek);System.out.println("size:" + stack.size());}for (int i = 5; i > 0; i--) {Integer pop = stack.pop();System.out.println("pop:" + pop);System.out.println("size:" + stack.size());}}}

例,單詞逆序,使用Statck結構

public class WordReverse {public static void main(String[] args) {reverse("株式會社");}static void reverse(String word) {if (word == null) return;StackSS<Character> stack = new StackSS<Character>();char[] charArray = word.toCharArray();int len = charArray.length;for (int i = 0; i <len; i++ ) {stack.push(charArray[i]);}StringBuilder sb = new StringBuilder();while (!stack.isEmpty()) {sb.append(stack.pop());}System.out.println("反轉後:" + sb.toString());}}
列印:

反轉後:社會式株



Java 類比棧結構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.