// // Linked list bases Stack // public class Stack2 { private ListNode top; private int length; public Stack2() { length = 0; top = null; } public boolean isEmpty() { return top == null; } public int getLength() { return length; } public void push(String item) { ListNode temp = new ListNode(item, null); if(isEmpty()) { top = temp; length ++; } else { temp.setLink(top); top = temp; length ++; } System.out.println("\n" + item + " was pushed in."); } public String pop() // assume not empty { String str = " "; length --; str = top.getData(); top = top.getLink(); System.out.println("\n" + str + " was poped out."); return str; } public void Print() { ListNode temp = top; System.out.println("\n\nThe Stack T->B is:"); while(temp != null) { System.out.println(temp.getData()); temp = temp.getLink(); } } public void makeEmpty() { top = null; } public static void main(String[] args) { Stack2 s = new Stack2(); s.push("Jon"); s.Print(); s.push("Sim"); s.Print(); s.push("Lesa"); s.Print(); s.push("Lee"); s.Print(); s.pop(); s.Print(); s.pop(); s.Print(); } }