Question: NO NEED TO WRITE CODE,EXPLAIN IN C++ PLEASE. Suppose we have a Stack that can grow indefinitely (for example, the push method has been fixed

NO NEED TO WRITE CODE,EXPLAIN IN C++ PLEASE.

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!