Part (a)
// adds all elements of the Set items to the q
// postcondition: the Set items is not modified;
// q contains its original elements followed
// by the elements from items
private void appendSetToQueue(Set items, Queue q)
{
Iterator it = items.iterator();
while (it.hasNext())
q.enqueue(it.next());
}
Part (b)
// returns a Set consisting of the e-mail addresses associated with
// the parameter alias, such that all intermediate resulting aliases
// have been expanded;
// precondition: alias is a key in addressBook
public Set expandAlias(String alias)
{
Set result = new TreeSet();
Queue q = new ListQueue();
q.enqueue(alias);
while (!q.isEmpty())
{
String token = (String)q.dequeue();
Set items = (Set)addressBook.get(token);
if (items == null) // not an alias
result.add(token);
else
appendSetToQueue(items, q);
}
return result;
}
|