In: Computer Science
Write a function that returns the largest value in a stack (only use push and pop)
Function for above problem
int largest_value_in_stack(stack):
temp=Stack() // declare
a temporary stack
max_value=-1 //
initialise max value with -1
while(!stack.isEmpty()): // iterate till
stack becomes empty
value=stack.pop()
// pop a value from stack
if(max_value<value):
// update max_value if required
max_value=value
temp.push(value)
// add value into temporary stack
while(!temp.isEmpty()):
// add all the values of temporary stack into origial stack
value=temp.pop()
stack.push(value)
return max_value
// return max_value
Mention in comments if any mistakes or errors are found. Thank you.