Question:
It also contains methods to print out stacks as well as to reorganize stack content.
Methods are:
organizeCharacters()
fromSupportingStackToFinalStack()
putInSupportingStack()
The top of the startStack is 'c', if you pushed all characters from left to right onto the startStack. Since
it is not the smallest, you need to push 'c' onto the supporting stack, say, #0.
The next character of the startStack is 'd'. It cannot be pushed onto the supporting stack #0 because it
is larger than the top of the supporting stack #0, which is 'c'. So we can push 'd' onto another
supporting stack, say #1.
Now, the next character 'f' can be pushed only onto the supporting stack #2 because that is the only
stack whose top is larger than 'f'. Remember, the content of each supporting stack needs to be
increasing order.
After pushing 'c', 'd', 'h', 'f', 'e', 'b' onto supporting stacks, the content of supporting stacks will be:
The next character from the startStack is 'a', so this is pushed directly onto the finalStack.
At this point, the next smallest character 'b' is at the top of the supporting stack #0. So we pop it and
push it onto the finalStack. Then we will do the same for the character 'c'. We continue to pop from
the supporting stacks as long as the next smallest character is at the top of one of the supporting
stacks.
Supporting stack 0: []
Supporting stack 1: []
// Description: This program will take out all characters in the start stack
// and put them into the final stack in their sorted order using supporting stacks.
import java.util.Stack;
private Stack<Character> finalStack; //stack to have characters in sorted order after running this
algorithm
private Stack<Character>[] supportingStacks; //stacks to hold some characters that are not in either
start or final stack
private char smallestCharacter; //current smallest character that can be moved to the final stack
private int stackWithNextSmallest; //the index of supporting stack that contains the next smallest
character
private final int ASCII_FOR_a = ((int) 'a'); //ASCII number for a, we can use it to keep track of
smallest/largest characters
this.sizeOfStartStack = sizeOfStartStack;
this.numberOfSupportingStacks = numOfSupportingStacks;
stackWithNextSmallest = -1; //index of supporting stack containing the next smallest is unknown
startStack.push(character1);
System.out.println();
}
//the next character that should move to final stack is initially 'a'
while(startStack.empty() == false)
char nextCharacter;
if (nextCharacter == nextCharacterToFinalStack)
nextCharacterToFinalStack++;
//As long as the smallest character among all supporting stacks
//This will compute the smallest character and which supporting stack it belongs
fromSupportingStackToFinalStack();
nextCharacterToFinalStack++;
else
if (putInSupportingStack(nextCharacter) == false)
success = false;
return success;
return success;
//It also keeps track of the next smallest character and the supporting stack
if (stackWithNextSmallest >= 0
printSupportingStacks();
//Find the next smallest character and the supporting stack that contains it
if (supportingStacks[i].isEmpty() == false
smallestCharacter = supportingStacks[i].peek().charValue();
stackWithNextSmallest = i;
//After the above loop, the variable "stackWithNextSmallest" should have an index
}
//The putInSupportingStack tries to push the parameter character
//into the chosen stack, i.e., the one with the top element larger
//than the parameter character and also with the top element smallest among
int chosenStackIndex = -1; //initialize to a stack index that does not exists
//look for a non-empty stack that contains its top character which is larger than
//The index of the supporting stack with smallest top should be the chosenStackIndex
//If the stack that you check is empty, go ahead and pick that supporting
//selected yet.
//since all supporting stacks have its top character being smaller than
if (chosenStackIndex == -1)
return false;
printSupportingStacks();
return true;
Assignment11 class nothing has to be fixed here, just simply to help make program run.
// enter the next choice until the choice of 'Q' (Quit) is entered.
import java.io.*;
char input1;
printMenu();
line = stdin.readLine();
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1)
switch (input1)
try
System.out.print("Please specify how many characters will be used (the maximum is 26):\n");
organizer.addCharacterToStack(character1);
if (success == true)
System.out.print("\norganization completed\n");
else
else
catch(NumberFormatException ex)
break;
case 'Q': //Quit
break;
printMenu();
break;
default:
System.out.print("Unknown action\n");
break;
else
System.out.print("Unknown action\n");
System.out.print("Choice\t\tAction\n" +
"------\t\t------\n" +
"Q\t\tQuit\n" +
"?\t\tDisplay Help\n\n");