zoukankan      html  css  js  c++  java
  • java栈实现

    public class ReversStack<T> {
    
        class Node<U> {
            private U itemValue; //结点的值
    
            private Node<U> nextItem; //下一个节点地址
    
            Node() {
                itemValue = null;
                nextItem = null;
            }
    
            public Node(U itemValue, Node<U> nextItem) {
                this.itemValue = itemValue;
                this.nextItem = nextItem;
            }
    
            boolean end() {
                return itemValue == null && nextItem == null;
            }
        }
    
        private Node<T> top = new Node<>();//最后一个节点
    
        //进栈
        public void put(T itemValue) {
            top = new Node<T>(itemValue, top);
        }
        
        //出栈
        public T pop(){
            T result = top.itemValue;
            if(!top.end()){
                top=top.nextItem;
            }
            return result;
        }
        
        public static void main(String[] args) {
            ReversStack<String> rs=new ReversStack<String>();
            rs.put("tst1");
            rs.put("tst2");
            rs.put("tst3");
            System.out.println(rs.pop());
            System.out.println(rs.pop());
            System.out.println(rs.pop());
        }
    }
  • 相关阅读:
    m113
    无题
    m102 SE赛
    m101 真*sb($huge 全场最瞎$)
    m100 的坑
    m99 然而并没有想出来标题!
    m98 lsc rp-- 赛
    csps2019AFO祭
    csps考前的一些总结(然而可能并没有用)
    低错总结
  • 原文地址:https://www.cnblogs.com/zyf-yxm/p/10986334.html
Copyright © 2011-2022 走看看