This is supposed to remove the values from the Hashtable from smallest to largest and push them onto the stack.
Here it is:
/****************************************************
Name: insertIntoQueue()
Parameters: HashTable ht
Declared variables: Stack queue, Iterator i, String temp
Set s, Integer currentLow, currentItem.
Return value: void
Purpose: Takes a HashTable argument, remove the frequencies and
insert them into a priority queue;
Algorithm:
insertIntoQueue(hashTable h)
Create a new queue, a new keySet for the items in the table
and attach an iterator to the set.
temp=first item.
currentLow=temp
while (not at the end)
if currentLow is greaterthan temp(currentItem)
currentLow=temp(currentItem);
currentFlag=temp;
end while
remove currentFlag
push value of currentFlag onto the Stack.
recurse.
end.
*****************************************************/
publicvoid insertIntoQueue(Hashtable ht){
//Function variables.
queue=new Stack();
Set s=ht.keySet();
Iterator i=s.iterator();
String temp, ci="";
Integer currentLow;
temp=(String)i.next();//Equals the current item we are looking at.
currentLow=((Integer)ht.get(temp));
while(i.hasNext()){
temp = (String)i.next();//Updates the currentItem.
if(currentLow.intValue() >= ((Integer)ht.get(temp)).intValue()){
currentLow=((Integer)ht.get(temp));
ci=temp;
}
}
queue.push(currentLow);//Push it onto the Stack.
ht.remove(ci);//Remove it from the Hashtable.
insertIntoQueue(ht);//Recurse.
}
If you are getting an exception when trying to call insertIntoQueue recursively, try checking the size of the hashtable before calling the method again. You might be getting a null pointer exception, i think.