• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

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
0
pete420
Asked:
pete420
  • 9
  • 7
  • 2
  • +1
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 9
  • 7
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now