Java中棧結構的自我實現

來源:互聯網
上載者:User

標籤:myarray   ddd   ret   stat   i++   ++   sem   void   new   

package com.pinjia.shop.common.collection;/** * Created by wangwei on 2017/1/3. */public class MyLinkedStack<T> {    //定義節點資料結構    private class Node{        public T data;        public Node next;        public Node(){}        public Node(T data,Node next){            this.data = data;            this.next = next;        }    }    //棧頂元素    private Node top;    //元素個數    private int size;    //插入元素    public void push(T element){        top = new Node(element,top);        size++;    }    //出棧操作    public T pop(){        Node oldNode = top;        top = top.next;        //釋放引用        oldNode.next = null;        size--;        return oldNode.data;    }    //返回棧頂元素,但不出棧    public T peek(){        return top.data;    }    //棧長度    public int length(){        return size;    }    //判斷鏈棧是否為空白棧    public boolean empty(){        return size == 0;    }    public String toString(){        //鏈棧為空白時        if(empty())            return "[]";        else{            StringBuilder sb = new StringBuilder("[");            for(Node current = top;current != null;current = current.next){                sb.append(current.data.toString()+", ");            }            int len = sb.length();            return sb.delete(len-2,len).append("]").toString();        }    }    public static void main(String[] args) {        MyLinkedStack<String> stack = new MyLinkedStack<String>();        // 不斷地入棧        stack.push("aaaa");        stack.push("bbbb");        stack.push("cccc");        stack.push("dddd");        System.out.println(stack);        // 訪問棧頂元素        System.out.println("訪問棧頂元素:" + stack.peek());        // 彈出一個元素        System.out.println("第一次彈出棧頂元素:" + stack.pop());        // 再次彈出一個元素        System.out.println("第二次彈出棧頂元素:" + stack.pop());        System.out.println("兩次pop之後的棧:" + stack);    }}

  

以下是基於數組的實現:

package com.pinjia.shop.common.collection;/** * Created by wangwei on 2017/1/3. */public class MyArrayStack<T> {    private Object[] objs = new Object[16];    private int size = 0;    public boolean isEmpty() {        return size == 0;    }    public void clear() {        // 將數組中的資料置為null, 方便GC進行回收        for (int i = 0; i < size; i++) {            objs[size] = null;        }        size = 0;    }    public int length() {        return size;    }    public boolean push(T data) {        // 判斷是否需要進行數組擴容        if (size >= objs.length) {            resize();        }        objs[size++] = data;        return true;    }    /**     * 數組擴容     */    private void resize() {        Object[] temp = new Object[objs.length * 3 / 2 + 1];        for (int i = 0; i < size; i++) {            temp[i] = objs[i];            objs[i] = null;        }        objs = temp;    }    public T pop() {        if (size == 0) {            return null;        }        return (T) objs[--size];    }    public String toString() {        StringBuilder sb = new StringBuilder();        sb.append("MyArrayStack: [");        for (int i = 0; i < size; i++) {            sb.append(objs[i].toString());            if (i != size - 1) {                sb.append(", ");            }        }        sb.append("]");        return sb.toString();    }    public static void main(String[] args) {        MyArrayStack<Integer> stack = new MyArrayStack<Integer>();        stack.push(12);        stack.push(13);        System.out.println(stack.length());        System.out.println(stack);    }}

  

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.