Java Reference
In-Depth Information
/ ** Dictionaryimplementedbyunsortedarray-basedlist. * /
classUALdictionary<Key,E>implementsDictionary<Key,E>{
privatestaticfinalintdefaultSize=10;//Defaultsize
privateAList<KVpair<Key,E>>list;//Tostoredictionary
/ ** Constructors * /
UALdictionary(){this(defaultSize);}
UALdictionary(intsz)
{list=newAList<KVpair<Key,E>>(sz);}
/ ** Reinitialize * /
publicvoidclear(){list.clear();}
/ ** Insertanelement:appendtolist * /
publicvoidinsert(Keyk,Ee){
KVpair<Key,E>temp=newKVpair<Key,E>(k,e);
list.append(temp);
}
/ ** Usesequentialsearchtofindtheelementtoremove * /
publicEremove(Keyk){
Etemp=find(k);
if(temp!=null)list.remove();
returntemp;
}
/ ** Removethelastelement * /
publicEremoveAny(){
if(size()!=0){
list.moveToEnd();
list.prev();
KVpair<Key,E>e=list.remove();
returne.value();
}
elsereturnnull;
}
/ ** Findkusingsequentialsearch
@returnRecordwithkeyvaluek * /
publicEfind(Keyk){
for(list.moveToStart();list.currPos()<list.length();
list.next()){
KVpair<Key,E>temp=list.getValue();
if(k==temp.key())
returntemp.value();
}
returnnull;//"k"doesnotappearindictionary
}
Figure4.33 A dictionary implemented with an unsorted array-based list.
Search WWH ::




Custom Search