
CS251
Spring2009
VoicuPopescu
Assignment4
DueFridayFebruary13,11:59PM
Vectors,Lists,andSequences
1. ImplementaC++classthatmodelsacollectionofwordswithanarrayoflists.Aliststoreswords.There
isonelistofwordsforeacharrayelement.Eacharrayelementcorrespondstoalowercaseletterand
thelistofthearrayelementstoresallwordsinthecollectionthatbeginwiththatletter.Wordsare
spelledexclusivelywithlowercaseletters.Thewordcollectioncanstoreduplicates.Thedatastructure
shouldnotmakeanyassumptionaboutthemaximumnumberofwords.Themaximumlengthofaword
is20characters(i.e.letters).Thewordcharactersarestoredinanullterminatedcharacterstring(i.e.
thereisa0afterthelastcharacteroftheword;a0andnota‘0’).Yourdatastructureshouldprovide:
a. Aconstructorthatmakesanemptywordcollection(arrayelementshaveemptylists).
b. Aninsertmethodthataddsagivenwordtothewordcollectioninconstanttime.
c. AnisPresentmethodthatoutput“Yes”tostandardoutputifandonlyifagivenwordisalready
partofthewordcollection,“No”otherwise;runningtimeO(nl)wherenlisthelengthofthe
longestlist.
d. Aremovemethodthatremovesoneoccurrenceofagivenword,ifany;runningtimeO(nl),as
before.
e. AremoveAllmethodthatremovesalloccurrencesofagivenword,ifany;runningtimeO(nl),as
before.
f. AremoveDuplicatesmethodthateliminatesallbutoneoccurrencesforeachwordina
collection.
g. Aprintmethodthattakesacharacterinput,andoutputallwordsstartingwiththatcharacter.
h. AprintAllmethodthatoutputalllists,alphabetically(Firstthelistforcharactera,thenb,thenc
andsoon.Thelistforeachsinglecharactermaybenotalphabeticallysortedinitself).
2. Younoticethatmanywordsbeginwiththeletter‘p’,whichmakesthelistforplongandtheisPresent
methodinefficient.Describeamodificationtoyourdatastructurethatalleviatesthisproblem,andgive
apseudocodedescriptionoftheinsertandisPresentmethodsforthemodifieddatastructure.
3. Extra‐credit(3%):ProblemC‐5.6,page247oftextbook.
4. Turnininstructions
a. Question1:Nameyourprogramfileas“words.cpp”.“words.cpp”shouldimplementthe
operationsgivenin“words.h”.Youmayaddsomecodeto“words.h”,infactyoushould.
However,donotchangethenamesandsignaturesofthegivenoperations.Atestfile
“wordsTest.cpp”isalsoprovidedtotestthedatastructure.Examineandrunthatprogramto
testyouroperations.Youwillnotturninthetestfile.
b. Question2:TurninaPDFdocumentnamedas“a4.pdf”.Thisfileshouldincludeyourdescription
ofthemodificationandpseudocodedescriptionsoftheinsertandisPresentmethodsforthe
modifieddatastructure.ReadGeneralturnininstructionspartsE,FandGverycarefully,on
thewebsite.