Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create a program named ManipulatingNumStack.java and implements the following methods: public void mergePair(NonZeroNumStack stack) This method takes a NonZeroNumStack as a parameter and that merges
Create a program named ManipulatingNumStack.java and implements the following methods: public void mergePair(NonZeroNumStack stack) This method takes a NonZeroNumStack as a parameter and that merges values by replacing each successive pair of integers with the sum of the pair. For example, suppose a stack stores these values: bottom [7, 2, 8, 9, 4, 13, 7, 1, 9, 10] top The first pair should be merged to 9 (7 + 2), the second pair should be merged into 17 (8 + 9), the third pair should be merged into 17 (4 + 13) and so on to yield: bottom [9, 17, 17, 8, 19] top (the numbers stored in the stack after calling this method) If the stack stores an odd number of elements, the final element is not merged. For example, the stack: bottom [1, 2, 3, 4, 5] top Would merge into: bottom [3, 7, 5] top With the 5 at the top of the stack unchanged. public int removeLastMin(NonZeroNumStack stack) This method accepts a NonZeroNumStack as a parameter and removes and returns the last occurrence of the smallest value from the stack. For example if a variable s stores these values: bottom [2, 8, 3, 19, 7, 3, 2, 3, 2, 7, 12, -8, 4] top and we make the following call: int n = removeLastMin(s); The method removes and returns -8, so n will store -8 after the call and s will store the following values: bottom [2, 8, 3, 19, 7, 3, 2, 3, 2, 7, 12, 4] top If the minimum value appears more than once, only the last occurrences of the smallest value should be removed. For example, given the stack above, if we again call removeLastMin(s), it would return 2 and leave the stack as follows: bottom [2, 8, 3, 19, 7, 3, 2, 3, 7, 12, 4] top If the stack is empty, calling removeLastMin(s) should return 0.
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