tags:

views:

44

answers:

1
+3  Q: 

pop()ing element

In C, when making a pop function for a stack, do I need to rearrange every index, or would I just be able to remove the top index and everything shift up 1 place on it's own?

+5  A: 

When implementing a stack, you should push and pop at the end of the array, not the start. This way nothing has to move. In fact, if your elements don't have to be cleaned up (no pointers, etc.) then a pop is as simple as decrementing the top-of-stack variable.

Marcelo Cantos
What would be a good way of pushing from the top of a stack?
Jay
The top-of-stack variable should always point the next empty slot. To push, just populate the empty slot and then increment the top-of-stack variable. Also, when pushing, check that your stack doesn't doesn't overflow the array that contains it.
Marcelo Cantos