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

(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...
Use a switch statement to write a function that returns TRUE if a character is a...
Use a switch statement to write a function that returns TRUE if a character is a consonant and returns FALSE otherwise.
Write a Python function that returns a list of keys in aDict with the value target....
Write a Python function that returns a list of keys in aDict with the value target. The list of keys you return should be sorted in increasing order. The keys and values in aDict are both integers. (If aDict does not contain the value target, you should return an empty list.) This function takes in a dictionary and an integer and returns a list. def keysWithValue(aDict, target): ''' aDict: a dictionary target: an integer ''' # Your code here
16. Write a function that returns the start value of a hailstone sequence that contains the...
16. Write a function that returns the start value of a hailstone sequence that contains the largest value that was reported by largestInAnyHS(n). Write a contract, then an implementation, of a function that takes exactly one parameter, an integer n, and returns the start value from 1 to n of the hailstone sequence that contains the largest value. The heading must be int startHSWithLargest(int n) This function must not read or write anything. Modify your main function so that it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT