Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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);

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote