//The fromHoldingStackToOutputStack method moves the smallest element among //al
ID: 3689720 • Letter: #
Question
//The fromHoldingStackToOutputStack method moves the smallest element among //all holding stacks into the output stack. //It also keeps track of the next smallest number and the holding stack //that contains in it. public void fromHoldingStackToOutputStack() { if (stackWithNextSmallest >= 0 && holdingStacks[stackWithNextSmallest].isEmpty() == false) { //remove(pop) the smallest number from its stack to move to the output stack //and move(pop) it to output stack /****3. ADD Your Code Here ****/ System.out.println("Move number " + smallestNumber + " from holding stack#" + stackWithNextSmallest + " to output stack"); printHoldingStacks(); } //Find the next smallest number and the holding stack that contains it //by checking the top of all holding stacks smallestNumber = sizeOfInitialStack + 1; for (int i = 0; i < numberOfHoldingStacks; i++) { if (holdingStacks[i].isEmpty() == false && ((Integer) holdingStacks[i].peek()).intValue() < smallestNumber) { smallestNumber = ((Integer) holdingStacks[i].peek()).intValue(); stackWithNextSmallest = i; } } //After the above loop, the variable "stackWithNextSmallest" should have an index //of the holding stack contains the next smallest Number //AND the variable "smallestNumber" should have the next smallestNumber to move //to the output stack. }
Explanation / Answer
Code: remove(pop) the smallest number from its stack to move to the output stack and move(pop) it to output stack
int pop=holdingStacks[stackWithNextSmallest].remove(smallestNumber);
outputStack.push(pop);
Modified method:
public void fromHoldingStackToOutputStack()
{
if (stackWithNextSmallest >= 0
&& holdingStacks[stackWithNextSmallest].isEmpty() == false)
{
//remove(pop) the smallest number from its stack to move to the output stack
//and move(pop) it to output stack
/****3. ADDED Code Here ****/
int pop=holdingStacks[stackWithNextSmallest].remove(smallestNumber);
outputStack.push(pop);
System.out.println("Move number " + smallestNumber + " from holding stack#"
+ stackWithNextSmallest + " to output stack");
printHoldingStacks();
}
//Find the next smallest number and the holding stack that contains it
//by checking the top of all holding stacks
smallestNumber = sizeOfInitialStack + 1;
for (int i = 0; i < numberOfHoldingStacks; i++)
{
if (holdingStacks[i].isEmpty() == false
&& ((Integer) holdingStacks[i].peek()).intValue() < smallestNumber)
{
smallestNumber = ((Integer) holdingStacks[i].peek()).intValue();
stackWithNextSmallest = i;
}
}
//After the above loop, the variable "stackWithNextSmallest" should have an index
//of the holding stack contains the next smallest Number
//AND the variable "smallestNumber" should have the next smallestNumber to move
//to the output stack.
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.