This isn't the kind of thing where you can adjust the code really. These two implementations for a stack are so very different, it'd prolly be easiest to just rewrite your functions. The trick is to make it so that any program using those functions will not have to rewrite anthing...
An array is probably a better model for a stack than a linked list because in memory, the stack is arranged in the same way as a stack in real life.
I have a small stack of coins in front of me. I may only add or remove one coin at a time, and I may only place or remove the coin at the top of this stack. Clearly, the only coin which I can remove is the currently top coin (a quarter). If I were to add a penny to the stack, I would not be able to reach the quarter without taking off the penny, because the penny is in the way. This stack of coins clearly fulfills the programming definition of a stack.
Now, let's organize these coins as an array...
Code:
COIN array[100];
array[0] = DIME;
array[1] = NICKEL;
array[2] = QUARTER;
Okay now... let's say I want to add a penny...
addThisCoin (PENNY);
I would have to make
array[3] = PENNY
How would the computer know to put penny in array[3]? I mean, it does technically have 100 different values to choose from here. Well, I can only put the penny on top of the last element in the stack.
How does the computer know where the last element in the stack is? It doesn't, so we have to tell it.
Since we use this variable to keep track of the top of the stack, let's call it top...Exellent. Now if I wanted to add a penny to the stack, it knows where to put it. However, by adding the penny to the stack, the quarter is no longer the top coin on the stack, the penny is, in array[3], so we need to update top so it becomes 3. It should be clear that every time we add a coin to the stack, the variable top is going to go up by one.
Now let's say I wanted to remove the top coin in the stack. Let's go back to the DIME, NICKEL, QUARTER stack. In that stack, how did I know that array[3] wasn't in the stack? There's 2 ways to do this, either array[3..99] could have been some invalid value, to show that they do not represent coins... or, since we already have it, we can just use the variable top to say that all the coins above it don't really exist...
If that is true, then to remove a top coin, all we really have to do is wink it out of existance by lowering top by one... Clearly we'd have had to decrease top every time I removed a coin anyhow since top HAS to refer to the top coin's position.
So, here's some more complete pseudocode...
Code:
int top = 2;
COIN array [100];
array[0] = DIME;
array[1] = NICKEL;
array[2] = QUARTER;
void addCoin (COIN c)
{
// Be careful. array[top] is the top coin in the stack, so we have
// to increase top before doing array[top] = c, or else we'll end up
// overwriting the top coin in the stack.
top ++;
array[top] = c;
}
void removeCoin (COIN c)
{
// We could give array[top] some value here, but we don't have to
// since we're just gonna pretend it doesn't exist as long as top is
// below it. The only way to make top above it is to add enough
// coins... but the addCoin operation would overwrite array[top] in
// doing so, so there isn't any problem with doing it this way.
top --;
}
// This function is just here to show that it's quite easy to treat the
// stack such that the coins above top don't exist.
void printCoins ()
{
int i;
// We can either start at the bottom or the top of the stack. I
// choose to start at the bottom, but doing it from the top is similar.
for (i = 0; i <= top; i++) // 0 is bottom of stack, top is top.
{
// type coin is undefined, so I can't supply a type specifier here
printf (???, array[i]);
}
}
I may have gone a bit far with the coin allegory, I didn't intend to just write all that which I did. Still, the code is written with the idea in mind that instead of coin, you could use any data type. If there is anything that this code should show, it is that the array implementation of a stack is much much simpler than the linked list implementation.
And my code is not quite complete....
1) What if I remover from an empty stack?
2) Since I am using an array to implement the stack, there will neccisarily be a maximum number of elements that I can put on the stack. In my example, this was 100. How should I handle a user trying to add a 101th element?