Question:
Pop Function of Stack
Modify the pop() function of Stack so that it uses recursion.
/**
* pop - pops the top node from the stack. Return -1 if the stack is
* empty.
*/
int Stack::pop()
{
int pop = -1;
// Create a temporary set of node to hold the new stack
int *tmp_nodes = new int[size];
memset(tmp_nodes, 0, sizeof(int) * size);
// If the stack is not empty, pop the first node and copy the
// remaining elements of the nodes into the tmp_nodes
if (nodes && !isEmpty())
{
pop = nodes[0];
for (int i=0; i < size; i++)
{
tmp_nodes[i] = nodes[i+1];
}
curpos--;
}
// Update the stack
memset(nodes, 0, sizeof(int) * size);
memcpy(nodes, tmp_nodes, sizeof(int) * size);
delete [] tmp_nodes;
return pop;
}