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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -