Question

JAVA PROGRAM

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. • push(x) -- Push element x

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

import java.util.*;

public class MinStack
{
Stack<Integer> s;
Integer minEle;
  
MinStack() { s = new Stack<Integer>(); }
  
void getMin()
{
if (s.isEmpty())
System.out.println("Stack is empty");
  
else
System.out.println("Minimum Element in the " +
" stack is: " + minEle);
}
  
void top()
{
if (s.isEmpty())
{
System.out.println("Stack is empty ");
return;
}
Integer t = s.pop(); // Top element
System.out.print("Top Most Element is: ");
  
if (t < minEle)
System.out.println(minEle);
else
System.out.println(t);
}
  
void pop()
{
if (s.isEmpty())
{
System.out.println("Stack is empty");
return;
}
  
System.out.print("Top Most Element Removed: ");
Integer t = s.pop();
  
if (t < minEle)
{
System.out.println(minEle);
minEle = 2*minEle - t;
}
  
else
System.out.println(t);
}
  
void push(Integer x)
{
if (s.isEmpty())
{
minEle = x;
s.push(x);
System.out.println("Number Inserted: " + x);
return;
}
  
if (x < minEle)
{
s.push(2*x - minEle);
minEle = x;
}
  
else
s.push(x);
  
System.out.println("Number Inserted: " + x);
}


public static void main(String[] args)
{
MinStack s = new MinStack();
String[][] opr ={ {"push","push","push", "getMin","pop", "top", "getMin"},{"-2","0","-3","","","",""}};
String[] op= {"","","","","","",""};
for(int i=0; i< 7; i++)
{
       if(opr[0][i].equals("push"))
   {
       int x= Integer.parseInt(opr[1][i]);
       s.push(x);
       String y = Integer.toString(x);
       op[i]=y;
   }
   else if(opr[0][i].equals("pop"))
   {
       s.pop();
       op[i]="null ";
   }
   else if(opr[0][i].equals("getMin"))
   {
       s.getMin();
        op[i]="null ";
   }
   else if(opr[0][i].equals("top"))
   {
       s.top();
       op[i]="null ";
   }
   else
   {
       op[i]="null ";
   }    
}
for(int i=0;i<op.length;i++)
{
   System.out.print(op[i] +" ");
}
}
}

Add a comment
Know the answer?
Add Answer to:
JAVA PROGRAM Design a stack that supports push, pop, top, and retrieving the minimum element in...
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
  • Consider these functions: push() : push an element into the stack pop() : pop the top-of-the-stack...

    Consider these functions: push() : push an element into the stack pop() : pop the top-of-the-stack element top() : returns the item stored in top-of-the-stack-node What will be the output after performing these sequence of operations (after performing top()) push(20); push(4); pop(); push(10); push(6); pop(); pop(); push(5); top();

  • 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...

  • Design a stack in C++ that in addition to push, pop, and top functions, also has...

    Design a stack in C++ that in addition to push, pop, and top functions, also has a min function that returns the minimum element in the entire stack. The stack can contain the minimum element in the top. The purpose of having a min function is for run time purposes, such that the minimum element can be retrieved quickly when needed (as opposed to traversing an entire linked list from start to end just to find the minimum object). You...

  • 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...

  • There is a data structure called a drop-out stack that behaves like a stack in every...

    There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and...

    I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and size using below pseudo code. I need two classes with stack pseudo code implementation and the main method to demonstrate the correct working of each operation. pseudo code StackADT (using raw array) class StackADT { int top int items[] int max StackADT(int n) Initialize array to n capacity top = 0 max = n boolean isEmpty() if array has no elements return true else...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an ...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...

  • Java - data structures Suppose that in the array-based stack, the array doubles in size after...

    Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...

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
ADVERTISEMENT