Hi everybody;
I just started to learn stacks. How can I count the number of Items in a stack by using ADT stack operations?? Can u help me?

12 Years
Discussion Span
Last Post by thehakan

What operations do you have available? If you can only use push, pop, and empty then you're stuck with removing every item until the stack is empty and counting them. Any decent stack implementation will provide you with some form of size operation that will tell you how many items there are, so you need to be more specific.


>empty auxiliary stacks are available.
Getting information out of you people is like pulling teeth. Did you know that? I'll assume that you can only use push, pop, and empty. In that case, pop every item in the stack, increment a counter and push that item onto a second stack. When the first stack is empty, do the reverse but don't increment the counter and you'll have the same stack that you started with.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.