Add a new function. This function changes the stack so that all appearances of t
ID: 3679471 • Letter: A
Question
Add a new function. This function changes the stack so that all appearances of the smallest element are at the bottom of the stack, while the order of the other elements remains the same (unchanged).Example: After the execution of the function the input stack (the top is the leftmost element) 3, 2, 4, 5, 8, 2, 4 is changed into 3, 4, 5, 8, 4, 2, 2. Add a new function. This function changes the stack so that all appearances of the smallest element are at the bottom of the stack, while the order of the other elements remains the same (unchanged).
Example: After the execution of the function the input stack (the top is the leftmost element) 3, 2, 4, 5, 8, 2, 4 is changed into 3, 4, 5, 8, 4, 2, 2.
Explanation / Answer
void SpecialStack::push(int x)
{
if(isEmpty()==true)
{
Stack::push(x);
min.push(x);
}
else
{
Stack::push(x);
int y = min.pop();
min.push(y);
if( x < y )
min.push(x);
else
min.push(y);
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.