Purchase Solution

Pop Function of Stack

Not what you're looking for?

Ask Custom Question

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;
}

Purchase this Solution

Solution Summary

Since question just asks to use recursion in pop function, and it does not clearly mention whether pop function itself has to be made recursive or recursion has to be used in part of it. This solution assumes the latter and converts the for-loop copying in it to recursive copying.

Solution Preview

Since question just asks to use recursion in pop function, and it does not clearly mention whether pop function itself has to be made recursive or recursion has to be used in part of it. This solution assumes the latter.

/**
* recursive_copy_into_temporary_stack
*/

void ...

Purchase this Solution


Free BrainMass Quizzes
C# variables and classes

This quiz contains questions about C# classes and variables.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Javscript Basics

Quiz on basics of javascript programming language.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

C++ Operators

This quiz tests a student's knowledge about C++ operators.