How many in a stack..

i want to find out how many elements i have in a stack

if i say something like:

  public int size()
   {
           int stackSize = 0;
      while (top.getNext() != the last one)
      {
                  stackSize++;
      }
}

i am not sure how to know if i am on the last linear node or not

thanks

pete
pete420Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSoftware engineerCommented:
This maybe?

   public int size() {
          int stackSize = 0;
          Element e = top;
          while (e!=null) {
              stackSize++;
              e = e.getNext();
          }
   }
0
zzynxSoftware engineerCommented:
and not to forget:

          return stackSize;

at the end
0
petmagdyCommented:
suppose to be:
      while (! top.getNext().equals(the last one))
assuming the last one is based on Object and implements ( public boolean equals(); )
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

pete420Author Commented:
zzynx: when i added in your code to size() it said :
cannot resolve symbol class element



petmagdy: thanks for the reply, the bit "the last one" is just english i put in 2 show i had no idea how to say how to get to the last linear node. :(
0
CEHJCommented:
All you need to do is to keep track of the count in push and pop and return the variable in size:

public int size() {
    return count;
}
0
pete420Author Commented:
cehj, thanks for reply, thats the way i did have it but i have to re-write the method without a count variable.

i want to loop through all the linear nodes, counting how many as i go along.

ta pete
0
CEHJCommented:
Why so? That's a much less efficient approach
0
zzynxSoftware engineerCommented:
>>zzynx: when i added in your code to size() it said :
>>cannot resolve symbol class element

Of course, that was just a fictive class.

replace Element by Object
0
zzynxSoftware engineerCommented:
So:
   public int size() {
          int stackSize = 0;
          Object e = top;
          while (e!=null) {
              stackSize++;
              e = e.getNext();
          }
   }

But I agree with CEHJ, that it isn't efficient at all
0
pete420Author Commented:
yes i didnt think it was an effecient way myself,

im confused by this, why do i create another object instead ofjust using top?
also when i added in that code i got the error message 'cannot resolve symbol method getNext()'
0
pete420Author Commented:
0
zzynxSoftware engineerCommented:
>> 'cannot resolve symbol method getNext()'
Since you wrote that in your post I thought that you got a getNext() function
0
zzynxSoftware engineerCommented:
What class is StackADT?
0
pete420Author Commented:
i do have a getNext() method in my LinearNode class
0
zzynxSoftware engineerCommented:
And LinearNode?
So LinearNode doesn't have a getNext()?
Strange since it has a setNext()
0
zzynxSoftware engineerCommented:
>> i do have a getNext() method in my LinearNode class
Then try

 public int size()  {

         int stackSize = 0;
         LinearNode e = top;
         while (e!=null) {
              stackSize++;
              e = e.getNext();
         }
         return stackSize;
   }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pete420Author Commented:
it does have a getNext()

i have just uploaded all of my source code to:

http://www.newwavesound.co.uk/java

i cant understand why its not picking up getNext()


ta
0
pete420Author Commented:
that worked, thank you! :-D

pete
0
zzynxSoftware engineerCommented:
Thanks for accepting
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.