java - Implementing a Stack<E> using Vector<E> -
i using stack in following way... have problem here in line 26:
import java.util.vector; public class stack<e> { private vector <e> v=new vector <e>(1); public int getsize() { return v.size(); } public boolean isempty() { return (v.isempty()); } public e gettop() { return v.lastelement(); } public e pop() { e p; if (!isempty()) { p = v.lastelement(); v.remove(v.size() - 1); } /*return p; here?? when stack empty how return , to?* / } public void push(e p) { v.add(p); } }
one way go although not advised return null:
public e pop() { e p = null; if (!isempty()) { p = v.lastelement(); v.remove(v.size() - 1); } return p;
it better if can provide default constructor(s) type(s) used within stack can check afterwards if object valid or not.
e p = new e(); // invalid object
you might want check out null object pattern provides more information on this.
Comments
Post a Comment