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.
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.
Joined: Dec 06, 2009
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?