Source of Stack.java


  1: //Stack.java

  3: class Stack
  4: {
  5:     private LinkedList linkedList;

  7:     Stack()
  8:     {
  9:         linkedList = new LinkedList();
 10:     }

 12:     public void push(int newData)
 13:     {
 14:         // Create a new node and prepend
 15:         Node newNode = new Node(newData);
 16:         linkedList.prepend(newNode);
 17:     }

 19:     public int pop()
 20:     {
 21:         // Copy list head's data
 22:         int poppedItem = linkedList.getHeadData();

 24:         // Remove list head
 25:         linkedList.removeAfter(null);

 27:         // Return popped item
 28:         return poppedItem;
 29:     }

 31:     public void print()
 32:     {
 33:         linkedList.printList();
 34:     }
 35: }