package org.tanc.algorithm.stack.impl;

import org.tanc.algorithm.stack.BaseStack;

/**
 * 单链表实现栈（带头节点的单链表）
 */
public class LinkedStackImpl<E> implements BaseStack<E> {

    private Node<E> head;
    private int size;

    public LinkedStackImpl() {
        size = 0;
        head = new Node<>(null, null);
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void push(E element) {
        size++;
        head.next = new Node<>(element, head.next);
    }

    @Override
    public E top() {
        return head.next.item;
    }

    @Override
    public E pop() {
        if (size == 0) {
            return null;
        }
        size--;
        Node<E> top = head.next;
        head.next = top.next;
        top.next = null;
        return top.item;
    }

    private class Node<T> {
        T item;
        Node<T> next;

        Node(T element, Node<T> next) {
            this.item = element;
            this.next = next;
        }
    }
}

@@ -33,16 +33,79 @@
 .impl;%0A%0A
+import org.tanc.algorithm.exception.IllegalOperationException;%0A
 import o
@@ -709,19 +709,54 @@
     
-return null
+throw new IllegalOperationException(%22%E6%A0%88%E4%B8%AD%E6%B2%A1%E6%9C%89%E6%95%B0%E6%8D%AE%E3%80%82%22)
 ;%0A  

update dealing exception