Question

Same problem as Problem 1 however you will implement the opposite of a MaxStack, namely MinStack....

Same problem as Problem 1 however you will implement the opposite of a MaxStack, namely MinStack. Design a stack class by importing the available java.util.Stack to have the following features:

  • push(x) -- push element x onto stack, where x is anywhere between Integer.MIN_VALUE and Integer.MAX_VALUE.
  • pop() -- remove the element on top of the stack.
  • top() -- get the top element.
  • getMin() -- retrieve the min element in the stack in constant time (i.e., O(1)).

Your code should have the following shape and form:

import java.util.Stack;
public class HomeworkAssignment1_2 {   
    public static void main(String[] args) {
       // just like Problem 1, whatever you need here
       // etc.
   }
}
// JUST LIKE ANY PROBLEM, YOUR STYLING AND DOCUMENTATION GOES HERE
// SEE PROBLEM 1 FOR EXAMPLE.
class MinStack {

   // Initialize your data structure here
   public MinStack() { // YOUR CODE HERE }

   public void push(int x) { // YOUR CODE HERE }
   public void pop() { // YOUR CODE HERE }
   public int top() { // YOUR CODE HERE }
   public int getMin() { // YOUR CODE HERE }
}

EXAMPLES

MinStack minStack = new minStack();

minStack.push(-2);

minStack.push(0);

minStack.push(-3);

minStack.getMin(); // returns -3

minStack.pop();

minStack.top(); // returns 0

minStack.getMin(); // returns -2

CONSTRAINTS AND ASSUMPTIONS

  • For this problem you are ONLY allowed to use Java's reference class Stack . Failure to do so will receive 5 points off.
  • MinStack does not mean elements have to be ordered in increasing or decreasing values in the Stack.

You solution should persist a global min value while maintaining the ability to transact on a Stack data structure wrapped in your MinStack class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

screenshot

sample output

code

import java.util.Stack;

public class HomeworkAssignment1_2 {
   public static void main(String[] args) {
       MinStack minStack = new MinStack();
       minStack.push(-2);
       minStack.push(0);
       minStack.push(-3);
System.out.println(minStack.getMin() ); // returns -3
minStack.pop();
System.out.println(minStack.top()); // returns 0
System.out.println( minStack.getMin()); // returns -2
}
}
class MinStack {
// two stack one for stroing min and one for storing original value
   Stack<Integer> s;
   Stack<Integer> ms;
// initialize both stack as empty
   public MinStack() {
       s=new Stack<Integer>();
       ms=new Stack<Integer>();
   }

// push x as it is in one stack and min in one stack
   public void push(int x) {
       s.push(x);
       int temp=x;
       if(!ms.isEmpty())
           temp=ms.peek();

       x=x<temp?x:temp;
       ms.push(x);
   }

   public void pop() {
       if(s.isEmpty()){
           return;
       }
       s.pop();
       ms.pop();
   }
   public int top() {
       return s.peek();
   }
   public int getMin() {
       return ms.peek();
   }


}

please upvote if it helped. Your rating means a lot to us.

Add a comment
Know the answer?
Add Answer to:
Same problem as Problem 1 however you will implement the opposite of a MaxStack, namely MinStack....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design a stack class by importing the available java.util.Stack to have the following features: push(x) --...

    Design a stack class by importing the available java.util.Stack to have the following features: push(x) -- push element x onto stack, where x is anywhere between Integer.MIN_VALUE and Integer.MAX_VALUE. pop() -- remove the element on top of the stack. top() -- get the top element. getMax() -- retrieve the max element in the stack in constant time (i.e., O(1)). Your code should have the following shape and form, all in one .java file. Note the styling and documentation API already...

  • I need to modify my C++ code so it can get the min value of the...

    I need to modify my C++ code so it can get the min value of the stack code is as follows: #include <iostream> using namespace std; #define MAX_SIZE 100 class Stack { private: int S[MAX_SIZE]; int top; public: Stack() {   top = -1; } void push(int x) {   if (top == MAX_SIZE - 1) {    cout << "Stack is Full." << endl;    return;   }   S[++top] = x; } void pop() {   if (top == -1) {    cout << "Stack is...

  • Implement the EasyStack interface with the MyStack class. You can use either a linked list or...

    Implement the EasyStack interface with the MyStack class. You can use either a linked list or a dynamic array to implement the data structure. A stack is a specialised form of list in which you can only get and remove the element most recently added to the stack. The class should be able to work with the following code: EasyStack stack = new MyStack(); NB: You cannot import anything from the standard library for this task. The data structure must...

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns...

    Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIS is public int sizeIs() a.) Write the code for sizeIs for the ArrayStack class b.) Write the code for sizeIs for the LinkedStack class (do not add any instance variables to the class; each time sizeIs is called you must "walk" through the stack...

  • Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The...

    Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The application will be to add large numbers. Review adding large numbers Remember that we can use stacks to safely add integer values that overflow the int data type g. in Java, the maximum possible int value Integer.MAX_VALUE is: 2147483647 so any int addition larger than this will overflow and fail Using stacks to add large numbers safely Will actually represent the large integers to...

  • Implement the stack queue data structure with a linked list implementation to get the given test...

    Implement the stack queue data structure with a linked list implementation to get the given test code in driver.cpp to work properly: driver.cpp code: #include <iostream> #include "stackLL.h" using namespace std; int main() { /////////////Test code for stack /////////////// stackLL stk; stk.push(5); stk.push(13); stk.push(7); stk.push(3); stk.push(2); stk.push(11); cout << "Popping: " << stk.pop() << endl; cout << "Popping: " << stk.pop() << endl; stk.push(17); stk.push(19); stk.push(23); while( ! stk.empty() ) { cout << "Popping: " << stk.pop() << endl; }...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • Now, your objective is to rewrite the same Stack class with a Generic ArrayList and make...

    Now, your objective is to rewrite the same Stack class with a Generic ArrayList and make the entire class support using Generic types. You should be able to create a Stack of any primitive wrapper class, and show us that your Generic Stack implementation (push, pop, search, display, etc.) works with: • Character (Stack) • Integer (Stack) • Float (Stack • Double (Stack) • String (Stack) You can create these Stack objects in main() and perform operations on them to...

  • (C++) Two stacks of the same type are the same if they have the same number...

    (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT