class Node<E>{ Node<E>next=null; E data; public Node(E data){ this.data = data; } } public class MystackList<E>{ Node<E>top=null; public boolean Empty(){ return top==null; } public void push(E data){ Node<E>newNode=new Node<E>(data); newNode.next = top; top = newNode; } public E pop(){ if(this.Empty()){ return null; } E data = top.data; top = top.next; return data; } public E peek(){ if(this.Empty()){ return null; } return top.data; } public static void main(String[] args){ MystackList<Integer>s=new MystackList<Integer>(); s.push(1); s.push(2); System.out.println("栈顶元素为:"+s.pop()); System.out.println("栈顶元素为:"+s.pop()); } }
原文:https://www.cnblogs.com/jocelynD-9/p/11262790.html