送交者: HL 于 April 22, 2002 11:02:38:
回答: I don't know where I can find java.util.Stack... 由 Wendy 于 April 22, 2002 10:23:00:
This one uses Vector to implement Stack. Make sure it compiles. Let me know if
you have any questions. It implements:
1. empty()
2. push()
3. pop()
You can optionally implement peek() and search(), as in java.util.Stack. Note you are dealing with Object (likely Double, not double in your case).
Note also that most of Vector's methods are synchronized.
import java.util.*;
public class MyStack {
private Vector stack ;
public MyStack() {
stack = new Vector() ;
}
public boolean empty() {
return stack.isEmpty() ;
}
public void push (Object obj) {
statck.addElement(obj) ;
}
public Object pop() {
Obejct obj = lastElement() ;
stack.removeElementAt( (stack.size() -1) ) ;
return obj ;
}
}