Stacks

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 65
Reputation: grisha83 is an unknown quantity at this point 
Solved Threads: 0
grisha83's Avatar
grisha83 grisha83 is offline Offline
Junior Poster in Training

Stacks

 
0
  #1
May 18th, 2009
Hello,
I was wondering why do we need to assign value of -1 to the top of the stack?
topOfStack = -1;
Thank you
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 468
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training

Re: Stacks

 
0
  #2
May 18th, 2009
The top of the stack is -1 when the stack is empty. When you fill it using push(), the top will change. A return value of -1 lets the user know that the stack is empty.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 10
Reputation: Mubo is an unknown quantity at this point 
Solved Threads: 3
Mubo's Avatar
Mubo Mubo is offline Offline
Newbie Poster

Re: Stacks

 
0
  #3
May 19th, 2009
hi dear, let me try to say some about you question.

I think ur question is why stacks are assigned -1 when determining if they are empty();
i think , it is because the index position of -1 is the empty position of the stack. they follow the integer rule, which begins from 0 to n Numbers.So is useful to assign -1, so u can easily determine that there is no elements on the stack
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 32
Reputation: hardik.rajani is an unknown quantity at this point 
Solved Threads: 9
hardik.rajani's Avatar
hardik.rajani hardik.rajani is offline Offline
Light Poster

Re: Stacks

 
0
  #4
May 19th, 2009
Originally Posted by grisha83 View Post
Hello,
I was wondering why do we need to assign value of -1 to the top of the stack?
topOfStack = -1;
Thank you
No dear it is not compulsory to set topOfStack by -1.

But there is some tradition as well some concept for predefined. If you want to follow the tradition you should set it to -1. Else you can also set it 0. But if you sets it to 0 then you have to make relative changes to the traditional code of stack.

Just for advise don't go beyond the tradition if it is not essential. Because in future you may have to work with a big team at that time it will be hard for other. This is my own experience.
Anything that can go wrong, goes anyway.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Stacks

 
0
  #5
May 19th, 2009
As hardik.rajani mentions there's is little programming "tradition" to this.

The concept of the Top of Stack is that the Top should always point to the uppermost/topmost element in the Stack.

Now to follow this concept, it would be a logical thing that you first need to increment the top counter by one so that it points to an empty position in the Stack (the new top) and then enter an element at this position, so again after the completion of this operation our view of the Stack is consistent of the Top pointing to a the topmost element.

This is in the case of the Array implementation of Stack, and since we know arrays have a beginning index as 0 we cannot have the top pointing to 0 as in that case it would wrongly be conveyed that there exists a single element in the Stack. So we keep the top pointed to something that is one position lower than 0 (-1) so that the operation of adding elements to the Stack performs as mentioned above:
1. Increment top by 1
2. Add element to top

The remove/pop operation proceeds this way:
1. Copy element at top position
2. Decrement top by 1
3 Return copied element.

Following this procedure we can be sure that if the top points to -1, the Stack must certainly be emtpy, thats the reason we check the top being equal to -1 for the "Stack Empty" condition.

To address the question whether the top can be made equal to 0 initially, then in this case we would have modify the add operation this way:
1. Put element at position top.
2. Increment top by 1.

But in this case, as you must certainly have gathered the top would never point to the topmost element but at a position one above the topmost element. While we can certainly work this way, by modifying our pop operation accordingly, one problem that such a method might cause to our implementation would be of memory.

To explain this, consider that whenever you reach the max size of the array the array capacity is doubled. Now in the latter implementation, the capacity would be doubled when there is still a position vacant, which has a possibility of not being filled at all.

There's one more way in which the latter implemenatation can be modified in a way such that the top still points to the topmost element.

1. If top == 0
1.1 Put element at top.
2. Else
2.1 Increment top by 1
2.2 Put element at top.

But then we would have lost the simplicity of the push/pop operations for absolutely no gains.

So it's found better (which I mentioned as programming "tradition") to keep the top at one position below the start position which is -1.

NOTE : This is in the case of an array implementation of the Stack, LinkedLists implementation will always have the top as null when the stack is empty.
Last edited by verruckt24; May 19th, 2009 at 6:07 am.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 65
Reputation: grisha83 is an unknown quantity at this point 
Solved Threads: 0
grisha83's Avatar
grisha83 grisha83 is offline Offline
Junior Poster in Training

Re: Stacks

 
0
  #6
May 20th, 2009
Thanks to everyone for explanations.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 357 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC