CODE
// C++ program to implement a stack that supports
// findMin() in O(1) time and O(1) extra space.
#include <bits/stdc++.h>
using namespace std;
// A user defined stack that supports findMin() in
// addition to push() and pop()
struct MyStack
{
stack<int> s;
int minEle;
// Prints minimum element of MyStack
void findMin()
{
if (s.empty())
cout <<
"Stack is empty\n";
// variable minEle stores the
minimum element
// in the stack.
else
cout
<<"Minimum Element in the stack is: "
<< minEle << "\n";
}
// Prints top element of MyStack
void peek()
{
if (s.empty())
{
cout <<
"Stack is empty ";
return;
}
int t = s.top(); // Top element.
cout << "Top Most Element is: ";
// If t < minEle means minEle
stores
// value of t.
(t < minEle)? cout <<
minEle: cout << t;
}
// Remove the top element from MyStack
void pop()
{
if (s.empty())
{
cout <<
"Stack is empty\n";
return;
}
cout << "Top Most Element
Removed: ";
int t = s.top();
s.pop();
// Minimum will change as the
minimum element
// of the stack is being
removed.
if (t < minEle)
{
cout <<
minEle << "\n";
minEle =
2*minEle - t;
}
else
cout << t
<< "\n";
}
// Removes top element from MyStack
void push(int x)
{
// Insert new number into the
stack
if (s.empty())
{
minEle =
x;
s.push(x);
cout <<
"Number Inserted: " << x << "\n";
return;
}
// If new number is less than
minEle
if (x < minEle)
{
s.push(2*x -
minEle);
minEle =
x;
}
else
s.push(x);
cout << "Number Inserted:
" << x << "\n";
}
};
// Driver Code
int main()
{
MyStack s;
s.push(3);
s.push(5);
s.findMin();
s.push(2);
s.push(1);
s.findMin();
s.pop();
s.findMin();
s.pop();
s.peek();
return 0;
}
Consider an ordinary stack of integers that implements the usual push () and pop () operations in constant time. Descri...
JAVA PROGRAM Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. • push(x) -- Push element x onto stack. • pop() -- Removes the element on top of the stack. • top() -- Get the top element. • getMin() -- Retrieve the minimum element in the stack. Example 1: Input ["MinStack", "push", "push","push", "getMin","pop","top", "getMin"] [1], [-2], [0], [-3],[1,1],[1, [1] Output [null, null, null,null,-3, null,0,-2] Explanation MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0);...
) Consider Java's Stack class, and its five standard stack operations: push, pop, peek, isEmpty, and clear. Complete the two unfinished methods. Do not modify any other parts of the class. // Looks at the top two elements of the stack, and removes and returns the larger // of the two elements from the stack, returning the other element to the stack. // For example, if the stack, from the top, is 8 10 7 2...
Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue. You will need to think about the differences in the Stack and Queue. Think about how they operate and how the nodes may differ. Modify the code as necessary. You will need to push to the Stack, pop from the Stack, peek at the Stack. You will need a member functions like in the example code. Your program will need to show that everything...
Solve it for java Question Remember: You will need to read this assignment many times to understand all the details of the you need to write. program Goal: The purp0se of this assignment is to write a Java program that models an elevator, where the elevator itself is a stack of people on the elevator and people wait in queues on each floor to get on the elevator. Scenario: A hospital in a block of old buildings has a nearly-antique...