...  
 1   // Fig.  21.9: Stack.java
 2   // Stack uses a composed List object.
 3   package com.deitel.datastructures;
 4
 5   import java.util.NoSuchElementException;
 6
 7   public class Stack<E> {
 8      private List<E> stackList;
 9
10      // constructor
11      public Stack() {stackList = new List<E>("stack");} 
12
13      // add object to stack
14      public void push(E object) {stackList.insertAtFront(object);}
15
16      // remove object from stack
17      public E pop() throws NoSuchElementException {
18         return stackList.removeFromFront();
19      }
20
21      // determine if stack is empty
22      public boolean isEmpty() {return stackList.isEmpty();}
23
24      // output stack contents
25      public void print() {stackList.print();}
26   }

Stack<E> Methods

Class Stack<E> has four methods—push, pop, isEmpty and ...

Get Java How To Program, Late Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.