Question

In: Computer Science

Write a function that returns the largest value in a stack (only use push and pop)

Write a function that returns the largest value in a stack (only use push and pop)

Solutions

Expert Solution

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.


Related Solutions

Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the...
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the java and give the explanation
JAVA Stack - Implementation. You will be able to use the push, pop and peek of...
JAVA Stack - Implementation. You will be able to use the push, pop and peek of Stack concept. Post-Fix calculator - When an arithmetic expression is presented in the postfix form, you can use a stack to evaluate the expression to get the final value. For example: the expression 3 + 5 * 9 (which is in the usual infix form) can be written as 3 5 9 * + in the postfix. More interestingly, post form removes all parentheses...
(In Java) Design a stack that supports getMin(), pop() and push() in O(1) time. Must use...
(In Java) Design a stack that supports getMin(), pop() and push() in O(1) time. Must use the iterator and comparator, does not need to be a linked list, although it's what I've tried using. I'm using another tester class to test this class. import java.util.Comparator; import java.util.List; import java.util.LinkedList; import java.util.Iterator; import java.util.Stack; import java.util.ListIterator; public class FMinStack<T> implements MinStack<T> { protected Comparator<? super T> comp; T min; protected List<T> ds; public FMinStack() { this(new DefaultComparator<T>()); } public FMinStack(Comparator<? super...
All code should be in Python 3. Implement the Stack Class, using the push, pop, str,...
All code should be in Python 3. Implement the Stack Class, using the push, pop, str, init methods, and the insurance variable 'list'.
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3....
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3. isEmpty 4. PEEK 5. Size */ #include <stdio.h> #define CAPACITY 1000 //Two stacks .. for each stack we need // 1. An Array that can hold capacity of elements // 2. A top initialzied to -1 (signifying that the stak is empty at the start) //NOTE : THESE STACKS ARE OF TYPE CHAR :( ... so you need to FIX IT!!!! to int and...
5 marks] A MinStack supports three main operations: the standard Stack operations push(x) and pop() and...
5 marks] A MinStack supports three main operations: the standard Stack operations push(x) and pop() and the non-standard min() operation which returns the minimum value stored on the stack. The zip file gives an implementation SlowMinStack that implements these operations so that push(x) and pop() each run in O(1) time, but  min()runs in Θ(n) time. For this question, you should complete the implementation of FastMinStack that implements all three operations in O(1) time per operation. As part of your implementation, you...
Assume that a minus sign in the input indicates pop the stack and write the return...
Assume that a minus sign in the input indicates pop the stack and write the return value to standard output, and any other string indicates push the string onto the stack. Further, suppose that the following input is processed: it was - the best - of times - - it was - the - - 1/ What is written to the standard output? 2/What are the contents (top to bottom) left on the stack?
how could I implement an intStack class that has only a push and pop method? in...
how could I implement an intStack class that has only a push and pop method? in java of course.
For this question you will need to use the following library function: Math.floor(x) returns the largest...
For this question you will need to use the following library function: Math.floor(x) returns the largest whole number less than or equal to x Define a function named weight which has one input. The input is a Number representing a person's weight in pounds. Your function will calculate and return a string saying how much they weigh in stone. Your function should start by multiplying the input by 0.0714286. Because stone weight is always a whole number, your function will...
Write a boolean function that is given a binary tree and returns true if and only...
Write a boolean function that is given a binary tree and returns true if and only if the tree has an odd number of nodes. An empty tree is considered to have an even number of nodes. Notes: The function should have just one argument, a pointer to the root. No global variables may be used. No additional functions may be defined. You may not count the number of nodes.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT