cosc 220: data structures in c++ · pop: à remove from top of stack e.g. nested func=on call: last...
TRANSCRIPT
![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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042310/5ed754db55acf1337524f0ee/html5/thumbnails/19.jpg)
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