美文网首页
用链表实现一个栈

用链表实现一个栈

作者: xin激流勇进 | 来源:发表于2019-04-05 15:48 被阅读0次
    package structures;
    
    public class LinkedListStack<E> implements Stack<E> {
    
        private LinkedList<E> list;
    
        public LinkedListStack() {
            list = new LinkedList<>();
        }
    
        @Override
        public int getSize() {
            return list.getSize();
        }
    
        @Override
        public boolean isEmpty() {
            return list.isEmpty();
        }
    
        @Override
        public E pop() {
            return list.removeFirst();
        }
    
        @Override
        public E peek() {
            return list.getFirst();
        }
    
        @Override
        public void push(E e) {
            list.addFirst(e);
        }
    
        @Override
        public String toString() {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("LinkedListStack: top ");
            stringBuilder.append(list);
    
            return stringBuilder.toString();
        }
    }
    
    

    相关文章

      网友评论

          本文标题:用链表实现一个栈

          本文链接:https://www.haomeiwen.com/subject/hgxkiqtx.html