Package Stack;import Linklist.singlylinklist;public class Linkliststack {private singlylinklist<integer> singlylinklist;/** * default constructor, used to initialize stack */public linkliststack () {singlylinklist=new singlylinklist<integer> ();} /* * Press stack operation */public void push (int value) {singlylinklist.addfromhead (value);} Public Integer Peek () {if (IsEmpty ()) {//If the stack is empty throw new RuntimeException ("element in stack is empty");} return Singlylinklist.getfirst ();} /** * out of stack operation * @return */public Integer pop () {if (IsEmpty ()) {//If the stack is empty throw new RuntimeException ("element in stack is empty");} return Singlylinklist.removefromhead ();} /** * Determine if the stack is empty * @return */public boolean isEmpty () {return singlylinklist.isempty ();}}
Java Data Structure Series-Stack (2): Chain storage structure of stack and its operation