CS 251 Assignment 4: Implementing a Word Collection Data Structure in C++ - Prof. Voicu S., Assignments of Data Structures and Algorithms

An assignment for a computer science course, cs 251, where students are required to implement a c++ class for a word collection data structure. The data structure should store words in lists, one list for each lowercase letter, and provide methods for constructing an empty word collection, inserting words, checking presence, removing words, removing all occurrences, and printing words. The document also suggests a modification to the data structure to improve efficiency for long lists, and provides instructions for submitting the assignment.

Typology: Assignments

Pre 2010

Uploaded on 07/31/2009

koofers-user-wvc-1
koofers-user-wvc-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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. Extracredit(3%):ProblemC5.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.
pf2

Partial preview of the text

Download CS 251 Assignment 4: Implementing a Word Collection Data Structure in C++ - Prof. Voicu S. and more Assignments Data Structures and Algorithms in PDF only on Docsity!

CS 251

Spring 2009 Voicu Popescu Assignment 4 Due Friday February 13, 11:59PM Vectors, Lists, and Sequences

  1. Implement a C++ class that models a collection of words with an array of lists. A list stores words. There is one list of words for each array element. Each array element corresponds to a lower case letter and the list of the array element stores all words in the collection that begin with that letter. Words are spelled exclusively with lower case letters. The word collection can store duplicates. The data structure should not make any assumption about the maximum number of words. The maximum length of a word is 20 characters (i.e. letters). The word characters are stored in a null terminated character string (i.e. there is a 0 after the last character of the word; a 0 and not a ‘0’). Your data structure should provide: a. A constructor that makes an empty word collection (array elements have empty lists). b. An insert method that adds a given word to the word collection in constant time. c. An isPresent method that output “Yes” to standard output if and only if a given word is already part of the word collection, “No” otherwise; running time O(n (^) l ) where nl is the length of the longest list. d. A remove method that removes one occurrence of a given word, if any; running time O(n (^) l ) , as before. e. A removeAll method that removes all occurrences of a given word, if any; running time O(nl ) , as before. f. A removeDuplicates method that eliminates all but one occurrences for each word in a collection. g. A print method that takes a character input, and output all words starting with that character. h. A printAll method that output all lists, alphabetically (First the list for character a, then b, then c and so on. The list for each single character may be not alphabetically sorted in itself).
  2. You notice that many words begin with the letter ‘p’ , which makes the list for p long and the isPresent method inefficient. Describe a modification to your data structure that alleviates this problem, and give a pseudocode description of the insert and isPresent methods for the modified data structure.
  3. Extra‐credit (3%): Problem C‐5.6, page 247 of text book.
  4. Turn in instructions a. Question 1: Name your program file as “words.cpp”. “words.cpp” should implement the operations given in “words.h”. You may add some code to “words.h”, in fact you should. However, do not change the names and signatures of the given operations. A test file “wordsTest.cpp” is also provided to test the data structure. Examine and run that program to test your operations. You will not turnin the test file. b. Question 2: Turn in a PDF document named as “a4.pdf”. This file should include your description of the modification and pseudocode descriptions of the insert and isPresent methods for the modified data structure. Read General turnin instructions parts E, F and G very carefully, on the website.

c. Question 3 (Extra): Turn in a PDF document named as “a4extra.pdf”. This file should include any explanations and the proof. Read General turnin instructions parts E, F and G very carefully, on the website. d. Your codes MUST compile on lore.cs.purdue.edu for it to be graded. e. All of your documents (words.h, words.cpp, a4.pdf, a4extra.pdf (OPTIONAL)) MUST be in the directory A4. In the directory above A4, run the command on lore.cs.purdue.edu turnin ‐c cs251 ‐p A4 A optionally run the following command to verify what files were submitted turnin ‐c cs251 ‐p A4 –v Warning: turnin will be automatically disabled at 02/13/2009 at 11:59 PM EST and you will be unable to turnin your assignment after this time. Warning: turning in multiple times is ok but only the last one will be graded as each turnin permanently overwrites the previous one. Warning: failure to turnin exactly according to the instructions given above will result in your A receiving a grade of 0.