COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last...

19
COSC 220: Data Structures in C++ Data Structures: Queue Class This week (week 8): Queue, Map, and Set data structures will be discussed Make sure the Sta=c Stanford Libraries are working on your computer!! We got 4 working in 1 office hour last Friday! HW#3 online Weds

Transcript of COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last...

Page 1: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++DataStructures:QueueClass

Thisweek(week8):Queue,Map,andSetdatastructureswillbediscussedMakesuretheSta=cStanfordLibrariesareworkingonyourcomputer!!Wegot4workingin1officehourlastFriday!HW#3onlineWeds

Page 2: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++ReviewoftheCompila=onProcess:

Sourcefile Objectfile

Libraryfile

Executablefile

compiler

linker

1.Sourcefile:textofyourcode2.Compileràtranslatesourcetoobjectfile3.Objectfile:machine-languageinstructs4.ObjectfilecombinedwithotherobjectfilesviaLinkeràExecutablefile5.Otherobjectfiles:predefinedobjfilescalledlibraries6.Libraries:containmachine-langinstructsforvariousopera=onsreq’dbyprogram7.Linking:combiningobjectfilesintoanexecutable

Page 3: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++DataStructures:QueueClass

TheQueueClassCounterparttoStackClass.FIFO:firstinfirstoutStackàLIFOStack:pushàaddtotopofstackpop:àremovefromtopofstacke.g.nestedfunc=oncall:lastfunc=oncalledisfirsttoreturnQueuecounterparttoStack

enqueueàaddelementtoendofqueuedequeueàremoveelementfromfrontofqueue

Page 4: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++DataStructures:QueueClassQueueClass:

enqueueàaddelementtoendofqueuedequeueàremoveelementfromfrontofqueue

Page 5: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++DataStructures:QueueClass

TheQueueClass

Page 6: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++DataStructures:QueueClassTheQueueClassusefulinsimula=ons:

àwai=nginlinemodelingàtrafficmodeling:carfollowingandcarsaccelera=onfromlight,seemath354ClassExercise1:writeafunc=onthatreversesaqueue.Keepinmindyoucannotseeanyinternalrep’nofthequeue

voidreverseQueue(Queue<int>&queue);

Page 7: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++DataStructures:QueueClassTheQueueClassusefulinsimula=ons:

-wai=nginlinemodeling-trafficmodeling:carfollowingandcarsaccelera=onfromlight,seemath354ClassExercise1:writeafunc=onthatreversesaqueue.Keepinmindyoucannotseeanyinternalrep’nofthequeuevoidreverseQueue(Queue<int>&queue){intqSize=queue.size();Stack<int>stack;for(inti=0;i<qSize;i++){stack.push(queue.dequeue());}for(inti=0;i<qSize;i++){queue.enqueue(stack.pop());}}

Page 8: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++DataStructures:QueueClass

Writeafunc=onisPalindromethatreturnsbooleanbasedonifaphraseisapalindromeornotusingboththestackandqueueclasses.

boolisPalindrome(stringstr);

Page 9: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++DataStructures:QueueClass

Writeafunc=onisPalindromethatreturnsbooleanbasedonifaphraseisapalindromeornotusingboththestackandqueueclasses.

boolisPalindrome(stringstr){Queue<char>queue;Stack<char>stack;longintn=str.length();//methodlengthforclassstringfor(inti=0;i<n;i++){queue.enqueue(str[i]);stack.push(str[i]);}for(inti=0;i<n/2;i++){if(queue.dequeue()!=stack.pop())returnfalse;

//notecompareFIFOtoLIFO}returntrue;}

Page 10: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++DataStructures:MapClass

TheMapClassAssocia=vearraysClosestthingtoadic=onaryseeninPythonAssocia=onbetweenakeyandvaluemaps:some=mescalledsymboltablesconstructor:Map<keytype,valuetype>mapAssocia=vearrays:map[key]=value;sameas:map.put(key,value);

Page 11: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++DataStructures:MapClass

Page 12: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++DataStructures:MapClass

SeeAirportCodes.cpp(www.fredpark.com/teaching)1.  airportcodeandairportinfowillbekeyvaluepair

2.Classexercise,joinapartnerandthinkaboutaninteres=ngapplica=onusingthemapclass.Writepseudocodeandthenimplementit.

Page 13: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++DataStructures:MapClass

TheMapClassAssocia=vearraysClosestthingtoadic=onaryseeninPythonAssocia=onbetweenakeyandvaluemaps:some=mescalledsymboltablesconstructor:Map<keytype,valuetype>mapAssocia=vearrays:map[key]=value;sameas:map.put(key,value);

Page 14: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++DataStructures:SetClass

TheSetClassModelsthemathema=calabstrac=onofasetThisisanunorderedcollec=onofelementsoccurringonlyonceLotsofalgorithmicapplica=ons.

Page 15: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++DataStructures:SetClassTheSetClass

Page 16: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++DataStructures:SetClass

Scrabbleoraspellchecker:canuseasettostorethewordssincedefini=onsarenotneededSetofwordswithnoassociateddefini=onsiscalledalexiconAsetbasedimplementa=onfromafilelookslike:

Set<string>lexicon;ifstreaminfile;infile.open("EnglishWords.txt");if(infile.fail())error("Can'topenEnglishWords.txt");while(true){stringword;getline(infile,word);if(infile.fail())break;lexicon.add(word);}infile.close();

Page 17: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++DataStructures:lexiconclass

Setofwordswithnoassociateddefini=onsiscalledalexiconSetbasedimplementa=onisusefulbutnotefficient.Uselexiconclass.Load“EnglishWords.dat”containsreasonablycompletelistofEnglishwords.

Lexiconenglish("EnglishWords.dat");//lexiconclass

Page 18: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++DataStructures:lexiconclass

Page 19: COSC 220: Data Structures in C++ · pop: à remove from top of stack e.g. nested func=on call: last func=on called is first to return Queue counterpart to Stack enqueue à add element

COSC220:DataStructuresinC++Itera=ngoveracollec=on

C++11àcandoarangebasedforloop

for(typevariable:collection){bodyofloop

}

example:iteratethroughallwordsinEnglishlexiconandselectonlythosewithtwolekers:for(stringword:english){

if(word.length()==2){ cout<<word<<endl;}

}

colonoperator“:”isamechanismcalledaniterator