1 7 8 package com.sun.corba.se.impl.orbutil ; 9 10 import java.util.EmptyStackException ; 11 12 public class StackImpl { 17 private Object [] data = new Object [3] ; 19 private int top = -1 ; 20 21 public final boolean empty() { 23 return top == -1; 24 } 25 26 public final Object peek() { 29 if (empty()) 30 throw new EmptyStackException (); 31 32 return data[ top ]; 33 } 34 35 public final Object pop() { 38 Object obj = peek() ; 39 data[top] = null ; 40 top-- ; 41 return obj; 42 } 43 44 private void ensure() 45 { 46 if (top == (data.length-1)) { 47 int newSize = 2*data.length ; 48 Object [] newData = new Object [ newSize ] ; 49 System.arraycopy( data, 0, newData, 0, data.length ) ; 50 data = newData ; 51 } 52 } 53 54 public final Object push( Object item ) { 56 ensure() ; 57 top++ ; 58 data[top] = item; 59 return item; 60 } 61 } 62 | Popular Tags |