java - linked stack implementation -


my problem here pop , size stack. know pop removes top value , replaces next value. how do that?

also, what's right way size?

the code:

    public int pop()     {             if (isempty())             throw new runtimeexception("pop attempted on empty stack");         else         {             return  m_top.value  ;           }      }      // return size of stack     public int size()     {                   if (m_top == null)         return 0;         else         return m_top.value;      } 

a fine way able retrieve size of stack @ moment maintain internally.

as suggested @peter lawrey in comments, looking real implementing in jdk can great source of inspiration, if can scary @ beginning. example, see size method in linkedlist implemented way, internal size property maintained each linked list instance.

for pop method, need have precise idea of how implements stack. pop @ least need able retrieve previous item item @ top of stack. see use node object internally, did created class? part of homework? if closely linkedlist implementation again see use node object (certainly not coincidence...) stores reference next , previous object.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -