File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

i dont ubderstand why i get a bug

 
johny doe
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i wrote a function that pops a member from a stack
my logic saya that first you get that member
then decrease the place index by 1
and this way i get a bug that says "code is not reachable"

but my book tells to do it other wise
first decrease then pop the member
i cant ubderstand this
because this way we get the previos member
and not the last member

this is how i do:
==============================
public Object pop()
{
if (!isEmpty())
{
return data[sp];
sp = sp - 1; //the problem is here

}
Else
{
throw new EmptyStackException("Trying to pop from an empty Stack");
}
}

===========================================
and here is the answer of the book

public Object pop()
{
if (!isEmpty())
{
sp = sp - 1;
return data[sp];
}
Else
{
throw new EmptyStackException("Trying to pop from an empty Stack");
}
}
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kindly make use of the Code Tags whenever you paste your java code so that it looks neatly.

Conceptually, "popping an element from a stack" involves

  • Getting an element from the TOS (Top of the Stack)
  • Adjust the TOS accordingly (here, you decrease it by one).



  • If you do it the otherway, you will NOT get the *actual element* in the TOS rather the one which is next to TOS.

    The problem why you get code is not reachable is, you have a return statement prior to decrementing sp by 1. Once the control reaches a "return" statement, all the other following statements will NOT get executed.

    Put the return statement as the last statement in the method! It should work
     
    johny doe
    Ranch Hand
    Posts: 78
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    so if i right the pop code
    like that:

     
    Raghavan Muthu
    Ranch Hand
    Posts: 3381
    Mac MySQL Database Tomcat Server
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Exactly.
     
    Bill Shirley
    Ranch Hand
    Posts: 457
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I would guess that the code from the example is using the convention that the index as the next open location in the array to place something into the stack. Thus:



    Your (corrected) code would be the one in use if the index was pointing at the top most element in the stack.

    The former convention is often used because it simplifies (the appearance of) some of the code.
     
    Rahim Vindhani
    Greenhorn
    Posts: 10
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Even shorter use,

     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic