Question
PLZ in c++ code. Suppose we have a Stack that can grow indefinitely (for example, the push method has been fixed to double the size
PLZ in c++ code.
Suppose we have a Stack that can grow indefinitely (for example, the push method has been fixed to double the size of the array when at capacity instead of throwing a StackFullException). We want to create a second Stack data structure, which I promise will always contain only comparable items (e.g., integers, strings). We also want to add a function findMin to the Stack interface that will return the smallest element currently in the stack. We could implement this by searching the array, but that takes time linear in the number of elements in the Stack. Explain how you would change the Stack data structure to allow for this function to run in O(1) time. If you are storing additional private member data, state what else you are storing. If you are changing existing functions push, pop, or top (or the constructor/size functions), explain briefly how you are changing them. Their running times must still be O(1); for example, you cannot search the full stack for the newest min at every push and pop.
You may assume that there will never be a duplicate item pushed to the Stack.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started