// gets you access to System.out import java.io.*; // gets you access to the LinkedList class import java.util.*; public class Stack2{ // where we'll store the data. // notice we don't need a count anymore // since the linked list will grow and shrink as we // add or delete things. private LinkedList data; // the constructor public Stack2(){ data = new LinkedList(); } public void push(int thing){ data.add(new Integer(thing)); // since LinkedLists can only store objects(see API docs) // we have to wrap the primitive type in an object reference. // we'll cover this in the next lecture. } public int pop(){ // get the information back from the LinkedList. // the LinkedList is 0 based but the size method // returns us the count of items and that is why // we have to subtract one. Integer retVal = (Integer)data.remove(data.size()-1); // return the user the data of type int return retVal.intValue(); } // the code that gets run when you type "java Stack2". public static void main (String[] args){ Stack2 s = new Stack2(); s.push(1); s.push(2); s.push(3); System.out.println(s.pop()); System.out.println(s.pop()); } }