What is top good for? Isn't the first element always the top? |
Definitely not. If you made the first item the top then pushing and popping would require moving all items in the stack. Instead, you make the top of the stack at the end of the array so that pushing and popping doesn't move the other items, unless you have to reallocate.
StoneJax, I think the problem here is that your prof hasn't really explained how the array is supposed to grow and shrink. I'll try to do that here.
Of course one way to store the data is to an array that is always exactly the right size for the number of items in the stack. But since the number of items changes each time you push or pop, you'd have to allocate a new array each time you pushed or popped. Performance would be terrible.
To avoid this, you instead allocate extra space in the array. When you push enough items to fill that array, you allocate a new array with more space.
But now there's a problem: suppose you have space in the array for 10 items and the stack has 4 items in it. You need to keep track of
both values. By convention, we call the total number of items that the array can hold the
capacity. We call the number of items that are currently in use in the array, the
size.
A bunch of the code deals with managing the size and capacity and adjusting them when needed.
Now let's look at that capacity and answer some questions.
Q1: How big should the capacity be initially?
A1: The DEFAULT_CAPACITY
Q2: When you have to expand the capacity, by how much should you do it?
A2: I think you said in another thread that it should double.
Now be careful in the copy constructor. You want to copy right.size items, not right.capacity.
Hope this helps.