• 定义


    栈是一种基于后进先出(LIFO)策略的集合类型


    Java


    • 源码(动态调整数组大小版本)
    import java.util.Iterator;
    
    public class Stack<Item> implements Iterable<Item> {
        private Item[] a = (Item[]) new Object[1];// 栈元素
        private int N = 0;// 元素数量
    
        public boolean isEmpty() {
            return N == 0;
        }
    
        public int size() {
            return N;
        }
    
        // 动态调整数组大小
        private void resize(int max) {
            // 将栈移动到一个大小为max的新数组
            // 由于某些历史和技术原因,创建泛型数组在Java中是不允许的
            Item[] temp = (Item[]) new Object[max];
            for (int i = 0; i < N; i++)
                temp[i] = a[i];
            a = temp;
        }
    
        public void push(Item item) {
            // 将元素添加到栈顶
            if (N == a.length) resize(2 * a.length);
            a[N++] = item;
        }
    
        public Item pop() {
            // 从栈顶删除元素
            Item item = a[--N];
            a[N] = null;// 避免对象游离
            if (N > 0 && N == a.length / 4) resize(a.length / 2);
            return item;
        }
    
        public Iterator<Item> iterator() {
            return new ReverseArrayIterator();
        }
    
        private class ReverseArrayIterator implements Iterator<Item> {
            // 支持后进先出的迭代
            private int i = N;
    
            public boolean hasNext() {
                return i > 0;
            }
    
            public Item next() {
                return a[--i];
            }
    
            public void remove() {
            }
        }
    }
    
    • 源码(链表实现版本)
    import java.util.Iterator;
    
    public class Stack<Item> implements Iterable<Item> {
        private Node first;// 栈顶(最近添加的元素)
        private int N;// 元素数量
    
        public boolean isEmpty() {
            return null == first;
        }// 或N == 0
    
        public int size() {
            return N;
        }
    
        public void push(Item item) {
            // 向栈顶添加元素
            Node oldFirst = first;
            first = new Node();
            first.item = item;
            first.next = oldFirst;
            N++;
        }
    
        public Item pop() {
            // 从栈顶删除元素
            Item item = first.item;
            first = first.next;
            N--;
            return item;
        }
    
        public Iterator<Item> iterator() {
            return null;
        }
    
        private class Node {
            // 定义了结点的嵌套类
            Item item;
            Node next;
        }
    
        private class ListIterator implements Iterator<Item> {
            private Node current = first;
    
            public boolean hasNext() {
                return null != current;
            }
    
            public Item next() {
                Item item = current.item;
                current = current.next;
                return item;
            }
    
            public void remove() {
            }
        }
    }
    
    • 测试用例
    public class Test {
        public static void main(String[] args) {
    
            String[] ss = {"to", "be", "or", "not", "to", "-", "be", "-", "-", "that", "-", "-", "-", "is"};
            Stack<String> stack = new Stack<>();
            for (String s : ss) {
                if (!s.equals("-")) {
                    stack.push(s);
                } else if (!stack.isEmpty()) {
                    System.out.print(stack.pop() + " ");
                }
            }
        }
    }
    
    • 测试结果
    to be not that or be
    
  • 相关阅读:
    my live health
    network / switchboard / jiaohuanji / switch
    my live boadband
    proxyServer Squid 3.5.5 / 20181111
    db mysql / mysql cluster 5.7.19 / performance
    MPTCP v0.92 Release
    MPTCP
    外国专家:区块链是新的Linux 而非新的互联网
    为什么用 Unity 3D 开发游戏是用 C# || JS 开发而不是用C++
    树莓派、 Arduino 、传统单片机开发板该如何选择?
  • 原文地址:https://www.cnblogs.com/freelancy/p/8021961.html
Copyright © 2020-2023  润新知