File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Performance and the fly likes Help-Drop out stack Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Help-Drop out stack " Watch "Help-Drop out stack " New topic

Help-Drop out stack

ben niu

Joined: Dec 06, 2009
Posts: 2
I'm using a circular array to implement a drop-out stack, but I've found some problems that made me crazy. Here is my code, and I hope someone could help me out. I wonder how to make the size() and isEmpty() methods. If possible, I also want to know how to make the toString() method. Thanks in advance. BTW, I'm from China and I'm not very good at English. So maybe I used some english words wrongly.

Hope someone can help me. Thanks a million.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Let's do the easy one first:

Now for the other two: you could implement the size() method by going through the stack and counting the elements. The problem with that is, all you know about the elements is that there's a pointer to the first one, unless the stack is empty. You have no way to tell whether the other array entries are part of the stack or not.

So I would recommend keeping track of the size as you go along. Initialize it to zero, add one when you add an entry to the stack, subtract one when you remove an entry from the stack.
ben niu

Joined: Dec 06, 2009
Posts: 2
Thank you, Paul Clapham. You realy helped me. I remerber I tried several ways to work it out, but what I've tried failed many times. After sevaral days I found myself still cannot figure it out. I'm very frustrated. The problem is really killing me. I have to confess I'm new in data structures and algorithms, although I've been a programer one year, using the J2EE technology to develop websites. Because my major is not Computer Science (SC) when I was in university, so I decided to learn the basic of SC with myself and started from last month. I think learning it well can help me in my futrue career, am I right?

My code goes here:

Thank you again
I agree. Here's the link:
subject: Help-Drop out stack
It's not a secret anymore!