In: Computer Science
Write a C function to implement operation of a stack using the following format:
/**
* function:
* push
*
* expects:
* pointer to the stack
* pointer to the size
* the value to push
*
* returns:
* true when value has been pushed
* false otherwise
*
* The push function push a value to the passed in stack
*/
bool push(int *stack, int *size, int max_size, int to_push)
{
/**
* TODO: finish implementing this
*/
}
Required function definition is given below:
bool push(int *stack, int *size, int max_size, int to_push)
{
if(*size==max_size) // condition for array overflow
{
cout<<"STACK OVERFLOW\n";
return false; // return false because element will not add
}
else
{
size++; //incrementing by one because element can be add.
stack[*size-1]=to_push; //pushing element into stack
return true; // return true because element has been added
}
}
If you have any doubt feel free to ask and if you like the answer please upvote it .
Thanks