版本比较
基础数据结构模板
正文
增加 2014 行删除 648 行
4 行未改动567899101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551541551561571581594 行未改动1641651661671681681691701711721731741751761771761771781791801813 行未改动18518618718818918919019119219319419519619719819919119219319419519619719819920020120220320420520620720820920120220320420520620720820921021121221321421521621721821922021521621721821922022122222322416 行未改动24124224324424524624724824925024524624724824925025125225325416 行未改动2712722732742752762772782792802752762772782792802812822832842852852862872882895 行未改动295296297298299299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510510511512513514515516517518519511512513514515516517518519520521521522523524525526527528529522523524525526527528529530531532533533534535536537538539540541542543544545546547548549550551552553554555556557558559560534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569569570571571572573574572573574575576577578579580576577581582583579584585586582587588589590591592588593594590595596592597598599595596597600601602603604600605606607608609605606610611612613614615616617618619620616617618621622623624620621625626627628629630626627631632633634635636637638634635636639640641642643644645641646647643648649645646647648650651652653654655656657658659660656661662663664660665666662667668669670671672673669670674675672673674675676677678679680681682683684685686687676677678679680681682683684685686687688689690691692693694695696697698695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793699700701702703704795796705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858378 行未改动全部展开12371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140438 行未改动144314441445144612861287144714481449145014511452145386 行未改动15401541154215431382138315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158518 行未改动16041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927804 行未改动```cpp#include<bits/stdc++.h>using namespace std;struct Node{ int num,next; int num,next;};struct node{ Node a[200010]; int head=0,tail=0,len=0; queue<int> q; void initList(){//初始化 for(int i=1;i<=200000;i++){ q.push(i); } len=0; } bool empty(){//判空 return len==0; } int size(){//长度 return len; } void push_front(int x){//在链表前面插入 int t=q.front(); q.pop(); len++; a[t].num=x; a[t].next=head; head=t; if(tail==0){ tail=t; } } void push_back(int x){//在链表后面插入 int t=q.front(); q.pop(); a[t].num=x; a[t].next=0; if(len!=0){ a[tail].next=t; } len++; tail=t; if(head==0){ head=t; } } void pop_front(){//删除链表头部 if(len==0){ return; } len--; q.push(head); if(a[head].next==0){ a[head].num=0; head=0; tail=0; }else{ int t=a[head].next; a[head].next=0; a[head].num=0; head=t; } } void pop_back(){//删除链表尾部 if(len==0){ return; } len--; if(head==tail){ q.push(head); a[head].num=0; head=0; tail=0; return; } int cur=head; while(a[cur].next!=tail){ cur=a[cur].next; } q.push(tail); a[tail].num=0; a[cur].next=0; tail=cur; } void insertAfter(int x,int y){//在指定元素后插入 if(head==0){ return; } if(x==tail){ push_back(y); return; } int pos=head; while(pos!=0&&a[pos].num!=x){ pos=a[pos].next; } if(pos==0){ return; } len++; int t=q.front(); q.pop(); a[t].num=y; a[t].next=a[pos].next; a[pos].next=t; if(a[t].next==0){ tail=t; } } int front(){//返回头部元素 if(head==0){ return -1; } return a[head].num; } int back(){//返回尾部元素 if(tail==0){ return -1; } return a[tail].num; } void eraseValue(int pos){//按值删除 if(len==0||pos==0){ return; } if(pos==a[head].num){ pop_front(); return; } int cur=head; while(cur!=0&&a[a[cur].next].num!=pos){ cur=a[cur].next; } if(cur==0){ return; } len--; int t=a[a[cur].next].next; a[a[cur].next].num=0; a[a[cur].next].next=0; q.push(a[cur].next); a[cur].next=t; if(a[cur].next==0){ tail=cur; } } Node a[200010]; int head=0,tail=0,len=0; queue<int> q; void initList(){//初始化 for(int i=1;i<=200000;i++){ q.push(i); } len=0; } bool empty(){//判空 return len==0; } int size(){//长度 return len; } void push_front(int x){//在链表前面插入 int t=q.front(); q.pop(); len++; a[t].num=x; a[t].next=head; head=t; if(tail==0){ tail=t; } } void push_back(int x){//在链表后面插入 int t=q.front(); q.pop(); a[t].num=x; a[t].next=0; if(len!=0){ a[tail].next=t; } len++; tail=t; if(head==0){ head=t; } } void pop_front(){//删除链表头部 if(len==0){ return; } len--; q.push(head); if(a[head].next==0){ a[head].num=0; head=0; tail=0; }else{ int t=a[head].next; a[head].next=0; a[head].num=0; head=t; } } void pop_back(){//删除链表尾部 if(len==0){ return; } len--; if(head==tail){ q.push(head); a[head].num=0; head=0; tail=0; return; } int cur=head; while(a[cur].next!=tail){ cur=a[cur].next; } q.push(tail); a[tail].num=0; a[cur].next=0; tail=cur; } void insertAfter(int x,int y){//在指定元素后插入 if(head==0){ return; } if(a[tail].num==x){ push_back(y); return; } int pos=head; while(pos!=0&&a[pos].num!=x){ pos=a[pos].next; } if(pos==0){ return; } len++; int t=q.front(); q.pop(); a[t].num=y; a[t].next=a[pos].next; a[pos].next=t; if(a[t].next==0){ tail=t; } } int front(){//返回头部元素 if(head==0){ return -1; } return a[head].num; } int back(){//返回尾部元素 if(tail==0){ return -1; } return a[tail].num; } void eraseValue(int pos){//按值删除 if(len==0){ return; } if(pos==a[head].num){ pop_front(); return; } int prev=head,cur=a[head].next; while(cur!=0&&a[cur].num!=pos){ prev=cur; cur=a[cur].next; } if(cur==0){ return; } len--; a[prev].next=a[cur].next; if(cur==tail){ tail=prev; } q.push(cur); a[cur].num=0; a[cur].next=0; } void eraseAt(int k){//按位置删除 if(k<=0||k>len){ return; } return; } if(k==1){ pop_front(); return; }4 行未改动 len--; int target=a[cur].next; a[cur].next=a[target].next; if(target==tail){ tail=cur; tail=cur; } q.push(target); a[target].num=0; a[target].next=0; } void reverse(){//反转链表 if(head==0||a[head].next==0){ return; } return; } int prev=0,cur=head,nextnode=0; while(cur!=0){ nextnode=a[cur].next; a[cur].next=prev;3 行未改动 int t=head; head=tail; tail=t; } void clear(){//清空链表 void clear(){//清空链表 int cur=head; while(cur!=0){ int nextnode=a[cur].next; a[cur].num=0; a[cur].next=0; q.push(cur); cur=nextnode; } len=0; head=0; while(cur!=0){ int nextnode=a[cur].next; a[cur].num=0; a[cur].next=0; q.push(cur); cur=nextnode; } len=0; head=0; tail=0; } void printList(){//按顺序输出链表 int cur=head; while(cur!=0){ cout<<a[cur].num<<" -> "; cur=a[cur].next; } cout<<"NULL\n"; } } void printList(){//按顺序输出链表 int cur=head; while(cur!=0){ cout<<a[cur].num<<" -> "; cur=a[cur].next; } cout<<"NULL\n"; }};node a;//定义链表int main(){ a.initList(); if(a.empty()){ cout<<"No"<<endl; }else{ cout<<"Yes"<<endl; } a.push_back(10); a.push_back(20); cout<<"No"<<endl; }else{ cout<<"Yes"<<endl; } a.push_back(10); a.push_back(20); a.push_back(30); a.printList(); cout<<a.front()<<endl; cout<<a.back()<<endl;16 行未改动 cout<<a.front()<<endl; cout<<a.back()<<endl; cout<<a.size()<<endl; if(a.empty()){ cout<<"No"<<endl; }else{ cout<<"Yes"<<endl; } a.reverse(); a.printList(); cout<<"No"<<endl; }else{ cout<<"Yes"<<endl; } a.reverse(); a.printList(); cout<<a.front()<<endl; cout<<a.back()<<endl; cout<<a.size()<<endl; a.eraseAt(2);16 行未改动 a.printList(); cout<<a.front()<<endl; cout<<a.back()<<endl; cout<<a.size()<<endl; a.clear(); a.printList(); cout<<a.size()<<endl; a.push_back(10); a.push_back(20); a.push_back(30); a.clear(); a.printList(); cout<<a.size()<<endl; a.push_back(10); a.push_back(20); a.push_back(30); a.printList(); cout<<a.front()<<endl; cout<<a.back()<<endl; cout<<a.size()<<endl; a.insertAfter(20,40); a.insertAfter(20,40); a.printList(); cout<<a.front()<<endl; cout<<a.back()<<endl; cout<<a.size()<<endl;5 行未改动```cpp#include<bits/stdc++.h>using namespace std;struct Node{ int pre,nxt,num; int pre,nxt,num;};struct node{ Node a[100010]; int head=0,tail=0,len=0; queue<int> q; void initList(){//初始化 for(int i=1;i<=100000;i++){ q.push(i); } len=0; } bool empty(){//判空 return len==0; } int size(){//返回链表长度 return len; } void push_front(int x){//从头部插入元素 int t=q.front(); q.pop(); a[t].num=x; a[t].nxt=head; a[t].pre=0; if(len!=0){ a[head].pre=t; } head=t; len++; if(tail==0){ tail=t; } } void push_back(int x){//从尾部插入元素 int t=q.front(); q.pop(); a[t].num=x; a[t].nxt=0; a[t].pre=tail; if(len!=0){ a[tail].nxt=t; } tail=t; len++; if(head==0){ head=t; } } void pop_front(){//删除头部元素 if(len==0){ return; } len--; q.push(head); a[head].num=0; if(a[head].nxt==0){ head=0; tail=0; }else{ int t=a[head].nxt; a[head].nxt=0; head=t; } } void pop_back(){//删除尾部元素 if(len==0){ return; } len--; q.push(tail); a[tail].num=0; if(a[tail].pre==0){ head=0; tail=0; }else{ int t=a[tail].pre; a[tail].pre=0; tail=t; } } void insertBefore(int x,int y){//在元素前插入 if(len==0){ return; } if(x==head){ push_front(y); return; } int pos=head; while(pos!=0&&a[pos].num!=x){ pos=a[pos].nxt; } if(pos==0){ return; } len++; int t=q.front(); q.pop(); a[t].num=y; a[t].nxt=pos; a[t].pre=a[pos].pre; a[a[pos].pre].nxt=t; a[pos].pre=t; } void insertAfter(int x,int y){//在元素后插入 if(len==0){ return; } if(x==tail){ push_back(y); return; } int pos=head; while(pos!=0&&a[pos].num!=x){ pos=a[pos].nxt; } if(pos==0){ return; } len++; int t=q.front(); q.pop(); a[t].num=y; a[t].nxt=a[pos].nxt; a[t].pre=pos; if(a[t].nxt!=0){ a[a[t].nxt].pre=t; } a[pos].nxt=t; if(a[t].nxt==0){ tail=t; } } int front(){//返回头部元素 if(len==0){ return -1; } return a[head].num; } int back(){//返回尾部元素 if(len==0){ return -1; } return a[tail].num; } void eraseValue(int pos){//按值删除 if(len==0||pos==0){ return; } if(pos==a[head].num){ pop_front(); return; } int cur=head; while(cur!=0&&a[a[cur].nxt].num!=pos){ cur=a[cur].nxt; } if(cur==0){ return; } len--; int t=a[a[cur].nxt].nxt; a[a[cur].nxt].num=0; a[a[cur].nxt].nxt=0; a[a[cur].nxt].pre=0; q.push(a[cur].nxt); a[cur].nxt=t; if(a[cur].nxt==0){ tail=cur; } } void eraseAt(int k){//按位置删除 if(k<=0||k>len){ return; } if(k==1){ pop_front(); return; } int cur=head; for(int i=2;i<=k-1;i++){ cur=a[cur].nxt; } len--; int t=a[cur].nxt; a[cur].nxt=a[t].nxt; if(t==tail){ tail=cur; } q.push(t); a[t].num=0; a[t].nxt=0; a[t].pre=0; } void reverse(){//反转链表 if(len<=1){ return; } int cur=head; while(cur!=0){ int temp=a[cur].pre; a[cur].pre=a[cur].nxt; a[cur].nxt=temp; cur=a[cur].pre; } int temp=head; head=tail; tail=temp; } void clear(){//清空链表 Node a[100010]; int head=0,tail=0,len=0; queue<int> q; void initList(){//初始化 for(int i=1;i<=100000;i++){ q.push(i); } len=0; } bool empty(){//判空 return len==0; } int size(){//返回链表长度 return len; } void push_front(int x){//从头部插入元素 int t=q.front(); q.pop(); a[t].num=x; a[t].nxt=head; a[t].pre=0; if(len!=0){ a[head].pre=t; } head=t; len++; if(tail==0){ tail=t; } } void push_back(int x){//从尾部插入元素 int t=q.front(); q.pop(); a[t].num=x; a[t].nxt=0; a[t].pre=tail; if(len!=0){ a[tail].nxt=t; } tail=t; len++; if(head==0){ head=t; } } void pop_front(){//删除头部元素 if(len==0){ return; } len--; q.push(head); a[head].num=0; if(a[head].nxt==0){ head=0; tail=0; }else{ int t=a[head].nxt; a[head].nxt=0; head=t; } } void pop_back(){//删除尾部元素 if(len==0){ return; } len--; q.push(tail); a[tail].num=0; if(a[tail].pre==0){ head=0; tail=0; }else{ int t=a[tail].pre; a[tail].pre=0; tail=t; } } void insertBefore(int x,int y){//在元素前插入 if(len==0){ return; } if(a[head].num==x){ push_front(y); return; } int pos=head; while(pos!=0&&a[pos].num!=x){ pos=a[pos].nxt; } if(pos==0){ return; } len++; int t=q.front(); q.pop(); a[t].num=y; a[t].nxt=pos; a[t].pre=a[pos].pre; if(a[pos].pre!=0) a[a[pos].pre].nxt=t; else head=t; a[pos].pre=t; } void insertAfter(int x,int y){//在元素后插入 if(len==0){ return; } if(a[tail].num==x){ push_back(y); return; } int pos=head; while(pos!=0&&a[pos].num!=x){ pos=a[pos].nxt; } if(pos==0){ return; } len++; int t=q.front(); q.pop(); a[t].num=y; a[t].nxt=a[pos].nxt; a[t].pre=pos; if(a[t].nxt!=0){ a[a[t].nxt].pre=t; } a[pos].nxt=t; if(a[t].nxt==0){ tail=t; } } int front(){//返回头部元素 if(len==0){ return -1; } return a[head].num; } int back(){//返回尾部元素 if(len==0){ return -1; } return a[tail].num; } void eraseValue(int pos){//按值删除 if(len==0){ return; } if(pos==a[head].num){ pop_front(); return; } int prev=head,cur=a[head].nxt; while(cur!=0&&a[cur].num!=pos){ prev=cur; cur=a[cur].nxt; } if(cur==0){ return; } len--; a[prev].nxt=a[cur].nxt; if(a[cur].nxt!=0) a[a[cur].nxt].pre=prev; if(cur==tail) tail=prev; q.push(cur); a[cur].num=0; a[cur].nxt=0; a[cur].pre=0; } void eraseAt(int k){//按位置删除 if(k<=0||k>len){ return; } if(k==1){ pop_front(); return; } int cur=head; for(int i=2;i<=k-1;i++){ cur=a[cur].nxt; } len--; int t=a[cur].nxt; a[cur].nxt=a[t].nxt; if(a[t].nxt!=0) a[a[t].nxt].pre=cur; if(t==tail){ tail=cur; } q.push(t); a[t].num=0; a[t].nxt=0; a[t].pre=0; } void reverse(){//反转链表 if(len<=1){ return; } int cur=head; while(cur!=0){ int temp=a[cur].pre; a[cur].pre=a[cur].nxt; a[cur].nxt=temp; cur=a[cur].pre; } int temp=head; head=tail; tail=temp; } void clear(){//清空链表 int cur=head; while(cur!=0){ int nextnode=a[cur].nxt; a[cur].num=0; a[cur].nxt=0; a[cur].pre=0; q.push(cur); cur=nextnode; } len=0; head=0; while(cur!=0){ int nextnode=a[cur].nxt; a[cur].num=0; a[cur].nxt=0; a[cur].pre=0; q.push(cur); cur=nextnode; } len=0; head=0; tail=0; } void printList(){//按顺序输出链表 int cur=head; while(cur!=0){ cout<<a[cur].num<<" -> "; cur=a[cur].nxt; } cout<<"NULL\n"; } } void printList(){//按顺序输出链表 int cur=head; while(cur!=0){ cout<<a[cur].num<<" -> "; cur=a[cur].nxt; } cout<<"NULL\n"; }};node L;//定义链表int main(){ L.initList(); L.push_back(10); L.push_back(20); L.push_back(30); L.printList(); L.push_front(5); L.printList(); L.insertAfter(20,25); L.printList(); L.insertAfter(30,35); L.printList(); L.insertBefore(10,8); L.printList(); L.eraseValue(25); L.printList(); L.eraseAt(2); L.printList(); L.pop_front(); L.pop_back(); L.printList(); L.reverse(); L.printList(); cout<<L.front()<<" "<<L.back()<<" "<<L.size()<<endl; L.clear(); L.printList(); cout<<L.size()<<endl; return 0;} L.initList(); L.push_back(10); L.push_back(20); L.push_back(30); L.printList(); L.push_front(5); L.printList(); L.insertAfter(20,25); L.printList(); L.insertAfter(30,35); L.printList(); L.insertBefore(10,8); L.printList(); L.eraseValue(25); L.printList(); L.eraseAt(2); L.printList(); L.pop_front(); L.pop_back(); L.printList(); L.reverse(); L.printList(); cout<<L.front()<<" "<<L.back()<<" "<<L.size()<<endl; L.clear(); L.printList(); cout<<L.size()<<endl; return 0;}```::::::::::::::info[动态链表]::::info[单向链表]```cpp#include<bits/stdc++.h>using namespace std;struct node{struct Node{ int data; node *next; node(int val=0,node *nxt=NULL):data(val),next(nxt){} void push_front(node *&head,int val){//从前面插入 head=new node(val,head); Node *next; Node(int val=0, Node *nxt=NULL):data(val),next(nxt){}};struct LinkedList{ Node *head; LinkedList():head(NULL){} void push_front(int val){//从前面插入 head=new Node(val,head); } void push_back(node *&head,int val){//从后面插入 node *newnode=new node(val); void push_back(int val){//从后面插入 Node *newnode=new Node(val); if(head==NULL){ head=newnode; head=newnode; return; } node *cur=head; Node *cur=head; while(cur->next!=NULL){ cur=cur->next; } cur->next=newnode; } void insertAfter(node *cur,int val){//在指定节点后插入 void insertAfter(Node *cur,int val){//在指定节点后插入 if(cur==NULL){ return; return; } node *newnode=new node(val,cur->next); Node *newnode=new Node(val,cur->next); cur->next=newnode; } void deletenode(node *&head,int val){//按值删除节点 if (head==NULL){ return; void deletenode(int val){//按值删除节点 if(head==NULL){ return; } if(head->data==val){ node *temp=head; Node *temp=head; head=head->next; delete temp; return; } node *prev=head; node *cur=head->next; Node *prev=head; Node *cur=head->next; while(cur!=NULL&&cur->data!=val){ prev=cur; cur=cur->next; } if(cur!=NULL){ prev->next=cur->next; delete cur; } } node *find(node *head,int val){//按值查找节点 node *cur=head; while(cur!=NULL) { Node *find(int val){//按值查找节点 Node *cur=head; while(cur!=NULL){ if(cur->data==val){ return cur; } return cur; } cur=cur->next; } return NULL; } void printList(node *head){//正向遍历打印 node *cur=head; void printList(){//正向遍历打印 Node *cur=head; while(cur!=NULL){ cout<<cur->data<<" -> "; cur=cur->next; } cout<<"NULL\n"; } void freeList(node *&head){//释放整个链表 while (head!=NULL){ node *temp=head; void freeList(){//释放整个链表 while(head!=NULL){ Node *temp=head; head=head->next; delete temp; } } void reverseList(node *&head){//反转链表 void reverseList(){//反转链表 if(head==NULL||head->next==NULL){ return; return; } node *prev=NULL; node *cur=head; node *nextnode=NULL; while (cur!=NULL){ Node *prev=NULL; Node *cur=head; Node *nextnode=NULL; while(cur!=NULL){ nextnode=cur->next; cur->next=prev; prev=cur; cur=nextnode; } head=prev; } int get_front(node *head){//获取头部元素 int get_front(){//获取头部元素 if(head==NULL) return -1; return head->data; } int get_back(node *head){//获取尾部元素 int get_back(){//获取尾部元素 if(head==NULL) return -1; node *cur=head; Node *cur=head; while(cur->next!=NULL){ cur=cur->next; } return cur->data; }};node *head=NULL;//定义链表node a;int main(){ LinkedList a; a.push_back(head,10); a.push_back(head,20); a.push_back(head,30); a.printList(head); cout<<a.get_front(head)<<endl; cout<<a.get_back(head)<<endl; a.deletenode(head,10); a.printList(head); cout<<a.get_front(head)<<endl; cout<<a.get_back(head)<<endl; a.reverseList(head); a.printList(head); cout<<a.get_front(head)<<endl; cout<<a.get_back(head)<<endl; a.freeList(head); return 0; a.push_back(10); a.push_back(20); a.push_back(30); a.printList(); cout<<a.get_front()<<endl; cout<<a.get_back()<<endl; a.deletenode(10); a.printList(); cout<<a.get_front()<<endl; cout<<a.get_back()<<endl; a.reverseList(); a.printList(); cout<<a.get_front()<<endl; cout<<a.get_back()<<endl; a.freeList(); return 0;}```::::::::info[双向链表]```cpp#include<bits/stdc++.h>using namespace std;struct node{ int data; node *prev; node *next; node(int val=0,node *p=NULL,node *n=NULL):data(val),prev(p),next(n){} void insertAfter(node *cur,int val){//在指定节点后插入 if(cur==NULL){ return; } node *newnode=new node(val,cur,cur->next); if(cur->next!=NULL){ cur->next->prev=newnode; } cur->next=newnode; } void insertBefore(node *cur,int val){//在指定节点前插入 if(cur==NULL){ return; } node *newnode=new node(val,cur->prev,cur); if(cur->prev!=NULL){ cur->prev->next=newnode; } cur->prev=newnode; } void deletenode(node *cur){//删除指定节点 if(cur==NULL){ return; } if(cur->prev!=NULL){ cur->prev->next=cur->next; } if(cur->next!=NULL){ cur->next->prev=cur->prev; } cur->prev=NULL; cur->next=NULL; delete cur; } void push_front(node *&head,int val){//从前面插入 if(head==NULL){ head=new node(val); }else{ insertBefore(head,val); head=head->prev; } } void push_back(node *&head,int val){//从后面插入 if (head==NULL){ head=new node(val); return; } node *cur=head; while(cur->next!=NULL){ cur=cur->next; } insertAfter(cur,val); } node *find(node *head,int val){//按值查找节点 node *cur=head; while(cur!=NULL){ if(cur->data==val){ return cur; } cur=cur->next; } return NULL; } void printList(node *head){//正向遍历打印 node *cur=head; while(cur!=NULL){ cout<<cur->data<<" <-> "; cur=cur->next; } cout<<"NULL\n"; } void freeList(node *head){//释放整个链表 while(head!=NULL){ node *temp=head; head=head->next; delete temp; } } int get_front(node *head){//获取头部元素 if(head==NULL){ return -1; } return head->data; } int get_back(node *head){//获取尾部元素 if(head==NULL){ return -1; } node *cur=head; while(cur->next!=NULL){ cur=cur->next; } return cur->data; }struct Node{ int data; Node *prev; Node *next; Node(int val=0, Node *p=NULL, Node *n=NULL):data(val),prev(p),next(n){}};node *head=NULL;//定义链表node a;struct LinkedList{ Node *head; LinkedList():head(NULL){} void insertAfter(Node *cur,int val){//在指定节点后插入 if(cur==NULL){ return; } Node *newnode=new Node(val,cur,cur->next); if(cur->next!=NULL){ cur->next->prev=newnode; } cur->next=newnode; } void insertBefore(Node *cur,int val){//在指定节点前插入 if(cur==NULL){ return; } Node *newnode=new Node(val,cur->prev,cur); if(cur->prev!=NULL){ cur->prev->next=newnode; }else{ head=newnode; // cur 是头节点,更新 head } cur->prev=newnode; } void deletenode(Node *cur){//删除指定节点 if(cur==NULL){ return; } if(cur->prev!=NULL){ cur->prev->next=cur->next; }else{ head=cur->next; // 删的是头节点,更新 head } if(cur->next!=NULL){ cur->next->prev=cur->prev; } cur->prev=NULL; cur->next=NULL; delete cur; } void push_front(int val){//从前面插入 if(head==NULL){ head=new Node(val); }else{ insertBefore(head,val); } } void push_back(int val){//从后面插入 if(head==NULL){ head=new Node(val); return; } Node *cur=head; while(cur->next!=NULL){ cur=cur->next; } insertAfter(cur,val); } Node *find(int val){//按值查找节点 Node *cur=head; while(cur!=NULL){ if(cur->data==val){ return cur; } cur=cur->next; } return NULL; } void printList(){//正向遍历打印 Node *cur=head; while(cur!=NULL){ cout<<cur->data<<" <-> "; cur=cur->next; } cout<<"NULL\n"; } void freeList(){//释放整个链表 while(head!=NULL){ Node *temp=head; head=head->next; delete temp; } } int get_front(){//获取头部元素 if(head==NULL){ return -1; } return head->data; } int get_back(){//获取尾部元素 if(head==NULL){ return -1; } Node *cur=head; while(cur->next!=NULL){ cur=cur->next; } return cur->data; }};int main(){ a.push_front(head,10); a.push_front(head,20); a.push_front(head,30); a.printList(head); cout<<a.get_front(head)<<endl; cout<<a.get_back(head)<<endl; a.push_back(head,40); a.push_back(head,50); a.printList(head); cout<<a.get_front(head)<<endl; cout<<a.get_back(head)<<endl; node *pos=a.find(head,20); if(pos!=NULL){ a.insertAfter(pos,25); } a.printList(head); cout<<a.get_front(head)<<endl; cout<<a.get_back(head)<<endl; node *pos1=a.find(head,40); if(pos1!=NULL){ a.insertBefore(pos1,35); } a.printList(head); cout<<a.get_front(head)<<endl; cout<<a.get_back(head)<<endl; node *pos2=a.find(head,25); if(pos2!=NULL){ a.deletenode(pos2); } a.printList(head); cout<<a.get_front(head)<<endl; cout<<a.get_back(head)<<endl; if(head!=NULL){ node *temp=head; head=head->next; a.deletenode(temp); } a.printList(head); cout<<a.get_front(head)<<endl; cout<<a.get_back(head)<<endl; node *pos3=a.find(head,50); if(pos3!=NULL){ a.deletenode(pos3); } a.printList(head); cout<<a.get_front(head)<<endl; cout<<a.get_back(head)<<endl; a.freeList(head); return 0; LinkedList a; a.push_front(10); a.push_front(20); a.push_front(30); a.printList(); cout<<a.get_front()<<endl; cout<<a.get_back()<<endl; a.push_back(40); a.push_back(50); a.printList(); cout<<a.get_front()<<endl; cout<<a.get_back()<<endl; Node *pos=a.find(20); if(pos!=NULL){ a.insertAfter(pos,25); } a.printList(); cout<<a.get_front()<<endl; cout<<a.get_back()<<endl; Node *pos1=a.find(40); if(pos1!=NULL){ a.insertBefore(pos1,35); } a.printList(); cout<<a.get_front()<<endl; cout<<a.get_back()<<endl; Node *pos2=a.find(25); if(pos2!=NULL){ a.deletenode(pos2); } a.printList(); cout<<a.get_front()<<endl; cout<<a.get_back()<<endl; if(a.head!=NULL){ a.deletenode(a.head); } a.printList(); cout<<a.get_front()<<endl; cout<<a.get_back()<<endl; Node *pos3=a.find(50); if(pos3!=NULL){ a.deletenode(pos3); } a.printList(); cout<<a.get_front()<<endl; cout<<a.get_back()<<endl; a.freeList(); return 0;}```:::::::::378 行未改动全部展开}```::::::info[queue循环队列]```cpp#include<bits/stdc++.h>using namespace std;queue<int> q,q2;//定义队列int main(){ q.push(10);//尾部插入 q.push(20);//尾部插入 q.push(30);//尾部插入 cout<<q.front()<<endl;//输出队首元素 cout<<q.back()<<endl;//输出队尾元素 cout<<q.size()<<endl;//输出队列长度 q.pop();//头部删除 cout<<q.front()<<endl;//输出队首元素 cout<<q.back()<<endl;//输出队尾元素 cout<<q.size()<<endl;//输出队列长度 if(q.empty()){//判空 cout<<"Empty"<<endl; }else{ cout<<"Not Empty"<<endl; } q2.push(100);//尾部插入 q2.push(200);//尾部插入 q.swap(q2);//交换两个队列 cout<<q.front()<<endl;//输出交换后q的队首元素 cout<<q2.front()<<endl;//输出交换后q2的队首元素 while(!q.empty()){//清空队列 q.pop(); } cout<<q.size()<<endl;//输出清空后的队列长度 return 0;}```:::::::::::::::::info[双端队列]::::info[手写双端队列]```cpp#include<bits/stdc++.h>using namespace std;#define int long longstruct node{ int a[1000010]; int head,tail; void init(){//初始化 head=500000; tail=500000; } void clear(){//清空队列 init(); } void push_front(int x){//从头部插入 a[--head]=x; } void push_back(int x){//从尾部插入 a[tail++]=x; } void pop_front(){//从头部弹出 if(head<tail){ head++; } } void pop_back(){//从尾部弹出 if(head<tail){ tail--; } } int front(){//返回头部元素 if(head>=tail){ return -1; } return a[head]; } int back(){//返回尾部元素 if(head>=tail){ return -1; } return a[tail-1]; } int operator[](int x){//返回指定位置元素(从1开始) if(head+x-1>=tail){ return -1; } return a[head+x-1]; } bool empty(){//判空 return head>=tail; } int size(){//返回长度 return tail-head; } void print(){//按顺序输出元素 for(int i=head;i<tail;i++){ cout<<a[i]<<" "; } cout<<"\n"; }};node dq;//定义双端队列signed main(){ dq.init(); dq.push_back(10); dq.push_back(20); dq.push_front(5); dq.push_front(1); cout<<dq.front()<<endl; cout<<dq.back()<<endl; cout<<dq.size()<<endl; dq.print(); dq.pop_front(); dq.pop_back(); cout<<dq.front()<<endl; cout<<dq.back()<<endl; cout<<dq.size()<<endl; dq.print(); if(dq.empty()){ cout<<"Empty"<<endl; }else{ cout<<"Not Empty"<<endl; } cout<<dq[1]<<endl; dq.clear(); cout<<dq.size()<<endl; return 0;}```::::::::info[deque双端队列]```#include<bits/stdc++.h>using namespace std;deque<int> dq;//定义双端队列int main(){ dq.push_back(10);//尾部插入 dq.push_back(20);//尾部插入 dq.push_front(5);//头部插入 dq.push_front(1);//头部插入 cout<<dq.front()<<endl;//输出队首元素 cout<<dq.back()<<endl;//输出队尾元素 cout<<dq.size()<<endl;//输出队列长度 dq.pop_front();//头部删除 dq.pop_back();//尾部删除 cout<<dq.front()<<endl;//输出删除后的队首元素 cout<<dq.back()<<endl;//输出删除后的队尾元素 cout<<dq.size()<<endl;//输出队列长度 if(dq.empty()){//判空 cout<<"Empty"<<endl; }else{ cout<<"Not Empty"<<endl; } cout<<dq[0]<<endl;//按下标访问(从0开始) dq.insert(dq.begin()+1,99);//在指定位置插入 cout<<dq[1]<<endl;//输出插入后的元素 dq.erase(dq.begin()+1);//删除指定位置元素 cout<<dq[1]<<endl;//输出删除后的元素 dq.clear();//清空队列 cout<<dq.size()<<endl;//输出清空后的队列长度 return 0;}```::::::::::::::info[单调队列]```cpp38 行未改动}```:::::::::::::::::info[栈]:::::info[普通栈]:::::info[栈]::::info[普通栈]:::info[手写栈]```cpp#include<bits/stdc++.h>using namespace std;struct node{86 行未改动 } return 0;} ```::::::::::info[单调栈]::::::info[stack栈]```cpp#include<bits/stdc++.h>using namespace std;stack<int> st,st2;//定义栈int main(){ st.push(10);//栈顶压入元素 st.push(20);//栈顶压入元素 st.push(30);//栈顶压入元素 cout<<st.top()<<endl;//输出栈顶元素 cout<<st.size()<<endl;//输出栈内元素个数 st.pop();//弹出栈顶元素 cout<<st.top()<<endl;//输出弹出后的栈顶元素 cout<<st.size()<<endl;//输出栈内元素个数 if(st.empty()){//判空 cout<<"Empty"<<endl; }else{ cout<<"Not Empty"<<endl; } st2.push(100); st2.push(200); st.swap(st2);//交换两个栈 cout<<st.top()<<endl;//输出交换后st的栈顶元素 cout<<st2.top()<<endl;//输出交换后st2的栈顶元素 while(!st.empty()){//清空栈 st.pop(); } cout<<st.size()<<endl;//输出清空后的栈大小 return 0;}```:::::::::::info[单调栈]```cpp#include<bits/stdc++.h>using namespace std;#define ll long longint n,s[100010],ans[100010];int main(){ ios::sync_with_stdio(0);18 行未改动 } return 0;}```::::::::::::::::info[二叉树和哈夫曼树]::::::info[二叉树]:::::info[二叉树的遍历]::::info[深度优先搜索]:::info[先序遍历]```cpp#include<bits/stdc++.h>using namespace std;struct into{ int l,r;}a[1030];int n,t;void p1(int p){ printf("%d ",p); if(a[p].l!=0){ p1(a[p].l); } if(a[p].r!=0){ p1(a[p].r); }}int main(){ ios::sync_with_stdio(0); scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d%d",&a[i].l,&a[i].r); } t=0; p1(1); return 0;}```::::::info[中序遍历]```cpp#include<bits/stdc++.h>using namespace std;struct into{ int l,r;}a[1030];int n,t;void p2(int p){ if(a[p].l!=0){ p2(a[p].l); } printf("%d ",p); if(a[p].r!=0){ p2(a[p].r); }}int main(){ ios::sync_with_stdio(0); scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d%d",&a[i].l,&a[i].r); } t=0; p2(1); return 0;}```::::::info[后序遍历]```cpp#include<bits/stdc++.h>using namespace std;struct into{ int l,r;}a[1030];int n,t;void p3(int p){ if(a[p].l!=0){ p3(a[p].l); } if(a[p].r!=0){ p3(a[p].r); } printf("%d ",p);}int main(){ ios::sync_with_stdio(0); scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d%d",&a[i].l,&a[i].r); } t=0; p3(1); return 0;}```::::::info[先序中序转后序]```cpp#include<bits/stdc++.h>using namespace std;int n,a[1030],b[1030];void dfs(int l1,int r1,int l2,int r2){ if(l1>r1){ return; } if(l1==r1){ printf("%d ",a[l1]); return; } int x; for(int i=l2;i<=r2;i++){ if(b[i]==a[l1]){ x=i; break; } } dfs(l1+1,l1+x-l2,l2,x-1); dfs(l1+x-l2+1,r1,x+1,r2); printf("%d ",a[l1]);}int main(){ ios::sync_with_stdio(0); scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } for(int i=1;i<=n;i++){ scanf("%d",&b[i]); } dfs(1,n,1,n); return 0;}```::::::info[中序后序转先序]```cpp#include<bits/stdc++.h>using namespace std;int n,a[1030],b[1030];void dfs(int l1,int r1,int l2,int r2){ if(l1>r1){ return; } if(l1==r1){ printf("%d ",a[l1]); return; } printf("%d ",b[r2]); int x=0; for(int i=l1;i<=r1;i++){ if(a[i]==b[r2]){ x=i; break; } } dfs(l1,x-1,l2,l2+x-1-l1); dfs(x+1,r1,l2+x-l1,r2-1);}int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } for(int i=1;i<=n;i++){ cin>>b[i]; } dfs(1,n,1,n); return 0;}```:::::::::::info[广度优先搜索]```cpp#include<bits/stdc++.h>using namespace std;struct node{ int l,r;}a[1025];int n;void bfs(){ queue<int> q; q.push(1); while(!q.empty()){ int t=q.front(); q.pop(); cout<<t<<" "; if(a[t].l!=0){ q.push(a[t].l); } if(a[t].r!=0){ q.push(a[t].r); } }}int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i].l>>a[i].r; } bfs(); return 0;}```::::::::::::::info[二叉树的基本计数]::::info[求深度]```cpp#include<bits/stdc++.h>using namespace std;struct node{ int l,r;}a[1030];int n;int depth(int p){ if(p==0){ return 0; } return 1+max(depth(a[p].l),depth(a[p].r));}int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i].l>>a[i].r; } cout<<depth(1)<<endl; return 0;}```::::::::info[求节点数]```cpp#include<bits/stdc++.h>using namespace std;struct node{ int l,r;}a[1030];int n;int cntNode(int p){ if(p==0){ return 0; } return 1+cntNode(a[p].l)+cntNode(a[p].r);}int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i].l>>a[i].r; } cout<<cntNode(1)<<endl; return 0;}```::::::::info[求叶子数]```cpp#include<bits/stdc++.h>using namespace std;struct node{ int l,r;}a[1030];int n;int cntLeaf(int p){ if(p==0){ return 0; } if(a[p].l==0&&a[p].r==0){ return 1; } return cntLeaf(a[p].l)+cntLeaf(a[p].r);}int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i].l>>a[i].r; } cout<<cntLeaf(1)<<endl; return 0;}```::::::::info[求第 k 层节点数]```cpp#include<bits/stdc++.h>using namespace std;struct node{ int l,r;}a[1030];int n,k;void dfs(int p,int dep,int &cnt){ if(p==0){ return; } if(dep==k){ cnt++; return; } dfs(a[p].l,dep+1,cnt); dfs(a[p].r,dep+1,cnt);}int main(){ cin>>n>>k; for(int i=1;i<=n;i++){ cin>>a[i].l>>a[i].r; } int cnt=0; dfs(1,1,cnt); cout<<cnt<<endl; return 0;}```::::::::info[求最大宽度]```cpp#include<bits/stdc++.h>using namespace std;struct node{ int l,r;}a[1030];int n;int width[1030];void dfs(int p,int dep){ if(p==0){ return; } width[dep]++; dfs(a[p].l,dep+1); dfs(a[p].r,dep+1);}int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i].l>>a[i].r; } dfs(1,1); int ans=0; for(int i=1;i<=n;i++){ ans=max(ans,width[i]); } cout<<ans<<endl; return 0;}```::::::::::::::info[二叉树的应用]::::info[最近公共祖先]```cpp#include<bits/stdc++.h>using namespace std;struct node{ int l,r;}a[1030];int n,u,v;int fa[1030],dep[1030];void dfs(int p,int f,int d){ if(p==0){ return; } fa[p]=f; dep[p]=d; dfs(a[p].l,p,d+1); dfs(a[p].r,p,d+1);}int lca(int u,int v){ while(dep[u]>dep[v]){ u=fa[u]; } while(dep[v]>dep[u]){ v=fa[v]; } while(u!=v){ u=fa[u]; v=fa[v]; } return u;}int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i].l>>a[i].r; } dfs(1,0,1); cin>>u>>v; cout<<lca(u,v)<<endl; return 0;}```::::::::info[节点到根的路径]```cpp#include<bits/stdc++.h>using namespace std;struct node{ int l,r;}a[1030];int n,fa[1030];void dfs(int p,int f){ if(p==0){ return; } fa[p]=f; dfs(a[p].l,p); dfs(a[p].r,p);}int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i].l>>a[i].r; } dfs(1,0); int x; cin>>x; vector<int> path; while(x!=0){ path.push_back(x); x=fa[x]; } for(int i=path.size()-1;i>=0;i--){ cout<<path[i]<<" "; } cout<<endl; return 0;}```::::::::info[两节点距离]```cpp#include<bits/stdc++.h>using namespace std;struct node{ int l,r;}a[1030];int n,u,v;int fa[1030],dep[1030];void dfs(int p,int f,int d){ if(p==0){ return; } fa[p]=f; dep[p]=d; dfs(a[p].l,p,d+1); dfs(a[p].r,p,d+1);}int lca(int u,int v){ while(dep[u]>dep[v]){ u=fa[u]; } while(dep[v]>dep[u]){ v=fa[v]; } while(u!=v){ u=fa[u]; v=fa[v]; } return u;}int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i].l>>a[i].r; } dfs(1,0,1); cin>>u>>v; int w=lca(u,v); cout<<dep[u]+dep[v]-2*dep[w]<<endl; return 0;}```::::::::info[树的直径]```cpp#include<bits/stdc++.h>using namespace std;struct node{ int l,r;}a[1030];int n,ans;int dfs(int p){ if(p==0){ return 0; } int L=dfs(a[p].l); int R=dfs(a[p].r); ans=max(ans,L+R); return 1+max(L,R);}int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i].l>>a[i].r; } ans=0; dfs(1); cout<<ans<<endl; return 0;}```::::::::info[判断完全二叉树]```cpp#include<bits/stdc++.h>using namespace std;struct node{ int l,r;}a[1030];int n;bool isComplete(){ queue<int> q; q.push(1); bool flag=false; while(!q.empty()){ int t=q.front(); q.pop(); if(t==0){ flag=true; continue; } if(flag){ return false; } q.push(a[t].l); q.push(a[t].r); } return true;}int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i].l>>a[i].r; } if(isComplete()){ cout<<"Yes"<<endl; }else{ cout<<"No"<<endl; } return 0;}```::::::::info[判断二叉搜索树]```cpp#include<bits/stdc++.h>using namespace std;struct node{ int l,r,val;}a[1030];int n;bool check(int p,long long low,long long high){ if(p==0){ return true; } if(a[p].val<=low||a[p].val>=high){ return false; } return check(a[p].l,low,a[p].val)&&check(a[p].r,a[p].val,high);}int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i].l>>a[i].r>>a[i].val; } if(check(1,LLONG_MIN,LLONG_MAX)){ cout<<"Yes"<<endl; }else{ cout<<"No"<<endl; } return 0;}```::::::::info[镜像翻转]```cpp#include<bits/stdc++.h>using namespace std;struct node{ int l,r;}a[1030];int n;void mirror(int p){ if(p==0){ return; } swap(a[p].l,a[p].r); mirror(a[p].l); mirror(a[p].r);}void p1(int p){ if(p==0){ return; } cout<<p<<" "; p1(a[p].l); p1(a[p].r);}int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i].l>>a[i].r; } mirror(1); p1(1); return 0;}```::::::::info[所有根到叶子的路径]```#include<bits/stdc++.h>using namespace std;struct node{ int l,r;}a[1030];int n;vector<int> path;void dfs(int p){ if(p==0){ return; } path.push_back(p); if(a[p].l==0&&a[p].r==0){ for(int i=0;i<path.size();i++){ cout<<path[i]<<" "; } cout<<endl; }else{ dfs(a[p].l); dfs(a[p].r); } path.pop_back();}int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i].l>>a[i].r; } dfs(1); return 0;}```::::::::info[所有叶子节点]```cpp#include<bits/stdc++.h>using namespace std;struct node{ int l,r;}a[1030];int n;void dfs(int p){ if(p==0){ return; } if(a[p].l==0&&a[p].r==0){ cout<<p<<" "; return; } dfs(a[p].l); dfs(a[p].r);}int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i].l>>a[i].r; } dfs(1); return 0;}```:::::::::::::::::::::info[哈夫曼树]:::::info[建树 + WPL]```cpp#include<bits/stdc++.h>using namespace std;#define ll long longstruct node{ ll w; int l,r; node(ll w=0,int l=0,int r=0):w(w),l(l),r(r){}};struct cmp{ bool operator()(int x,int y){ return node[x].w>node[y].w; }};node a[200010];int n,cnt;priority_queue<int,vector<int>,cmp> pq;int build(){ cnt=n; while(pq.size()>1){ int x=pq.top(); pq.pop(); int y=pq.top(); pq.pop(); cnt++; a[cnt]=node(a[x].w+a[y].w,x,y); pq.push(cnt); } return pq.top();}ll wpl(int p,int dep){ if(a[p].l==0&&a[p].r==0){ return a[p].w*dep; } return wpl(a[p].l,dep+1)+wpl(a[p].r,dep+1);}int main(){ ios::sync_with_stdio(0); cin>>n; for(int i=1;i<=n;i++){ ll x; cin>>x; a[i]=node(x); pq.push(i); } int root=build(); cout<<wpl(root,0)<<endl; return 0;}```::::::::::info[哈夫曼编码]```cpp#include<bits/stdc++.h>using namespace std;#define ll long longstruct node{ ll w; int l,r; node(ll w=0,int l=0,int r=0):w(w),l(l),r(r){}};struct cmp{ bool operator()(int x,int y){ return node[x].w>node[y].w; }};node a[200010];int n,cnt;string code[200010];priority_queue<int,vector<int>,cmp> pq;int build(){ cnt=n; while(pq.size()>1){ int x=pq.top(); pq.pop(); int y=pq.top(); pq.pop(); cnt++; a[cnt]=node(a[x].w+a[y].w,x,y); pq.push(cnt); } return pq.top();}void dfs(int p,string s){ if(a[p].l==0&&a[p].r==0){ code[p]=s; return; } dfs(a[p].l,s+"0"); dfs(a[p].r,s+"1");}int main(){ ios::sync_with_stdio(0); cin>>n; for(int i=1;i<=n;i++){ ll x; cin>>x; a[i]=node(x); pq.push(i); } int root=build(); dfs(root,""); for(int i=1;i<=n;i++){ cout<<i<<" "<<a[i].w<<" "<<code[i]<<endl; } return 0;}```::::::::::info[哈夫曼解码]```cpp#include<bits/stdc++.h>using namespace std;#define ll long longstruct node{ ll w; int l,r; node(ll w=0,int l=0,int r=0):w(w),l(l),r(r){}};struct cmp{ bool operator()(int x,int y){ return node[x].w>node[y].w; }};node a[200010];int n,cnt;string code[200010];priority_queue<int,vector<int>,cmp> pq;int build(){ cnt=n; while(pq.size()>1){ int x=pq.top(); pq.pop(); int y=pq.top(); pq.pop(); cnt++; a[cnt]=node(a[x].w+a[y].w,x,y); pq.push(cnt); } return pq.top();}void dfs(int p,string s){ if(a[p].l==0&&a[p].r==0){ code[p]=s; return; } dfs(a[p].l,s+"0"); dfs(a[p].r,s+"1");}void decode(int root,string s){ int p=root; for(char c:s){ if(c=='0'){ p=a[p].l; }else{ p=a[p].r; } if(a[p].l==0&&a[p].r==0){ cout<<p<<" "; p=root; } } cout<<endl;}int main(){ ios::sync_with_stdio(0); cin>>n; for(int i=1;i<=n;i++){ ll x; cin>>x; a[i]=node(x); pq.push(i); } int root=build(); dfs(root,""); for(int i=1;i<=n;i++){ cout<<i<<" "<<a[i].w<<" "<<code[i]<<endl; } string s; cin>>s; decode(root,s); return 0;}```::::::::::::::::::::::::info[堆]:::::info[手写堆]::::info[大根堆]```cpp#include<bits/stdc++.h>using namespace std;struct node{ int a[100010],cnt=0; void init(){//初始化 cnt=0; } void clear(){//清空堆 cnt=0; } void push(int x){//插入元素 a[++cnt]=x; int i=cnt; while(i>1&&a[i]>a[i/2]){ swap(a[i],a[i/2]); i/=2; } } void pop(){//弹出堆顶 if(cnt==0){ return; } a[1]=a[cnt--]; int i=1; while(i*2<=cnt){ int j=i*2; if(j+1<=cnt&&a[j+1]>a[j]){ j++; } if(a[i]>=a[j]){ break; } swap(a[i],a[j]); i=j; } } int top(){//返回堆顶元素 if(cnt==0){ return -1; } return a[1]; } bool empty(){//判空 return cnt==0; } int size(){//返回长度 return cnt; } void erase(int x){//删除所有指定值元素 int tmp[100010],tot=0; for(int i=1;i<=cnt;i++){ if(a[i]!=x){ tmp[++tot]=a[i]; } } cnt=0; for(int i=1;i<=tot;i++){ push(tmp[i]); } } void build(int n,int b[]){//建堆 cnt=n; for(int i=1;i<=n;i++){ a[i]=b[i]; } for(int i=cnt/2;i>=1;i--){ int j=i; while(j*2<=cnt){ int k=j*2; if(k+1<=cnt&&a[k+1]>a[k]){ k++; } if(a[j]>=a[k]){ break; } swap(a[j],a[k]); j=k; } } } void print(){//按顺序输出堆内元素 for(int i=1;i<=cnt;i++){ cout<<a[i]<<" "; } cout<<"\n"; }};node heap;//定义堆int main(){ heap.init(); heap.push(10); heap.push(20); heap.push(30); heap.push(5); heap.push(40); heap.print(); cout<<heap.top()<<" "<<heap.size()<<endl; if(heap.empty()){ cout<<"No"<<endl; }else{ cout<<"Yes"<<endl; } heap.pop(); heap.print(); cout<<heap.top()<<" "<<heap.size()<<endl; heap.erase(10); heap.print(); heap.clear(); cout<<heap.size()<<endl; int b[9]={0,3,1,4,1,5,9,2,6}; heap.build(8,b); heap.print(); cout<<heap.top()<<endl; return 0;}```::::::::info[小根堆]```cpp#include<bits/stdc++.h>using namespace std;struct node{ int a[100010],cnt=0; void init(){//初始化 cnt=0; } void clear(){//清空堆 cnt=0; } void push(int x){//插入元素 a[++cnt]=x; int i=cnt; while(i>1&&a[i]<a[i/2]){ swap(a[i],a[i/2]); i/=2; } } void pop(){//弹出堆顶 if(cnt==0){ return; } a[1]=a[cnt--]; int i=1; while(i*2<=cnt){ int j=i*2; if(j+1<=cnt&&a[j+1]<a[j]){ j++; } if(a[i]<=a[j]){ break; } swap(a[i],a[j]); i=j; } } int top(){//返回堆顶元素 if(cnt==0){ return -1; } return a[1]; } bool empty(){//判空 return cnt==0; } int size(){//返回长度 return cnt; } void erase(int x){//删除所有指定值元素 int tmp[100010],tot=0; for(int i=1;i<=cnt;i++){ if(a[i]!=x){ tmp[++tot]=a[i]; } } cnt=0; for(int i=1;i<=tot;i++){ push(tmp[i]); } } void build(int n,int b[]){//建堆 cnt=n; for(int i=1;i<=n;i++){ a[i]=b[i]; } for(int i=cnt/2;i>=1;i--){ int j=i; while(j*2<=cnt){ int k=j*2; if(k+1<=cnt&&a[k+1]<a[k]){ k++; } if(a[j]<=a[k]){ break; } swap(a[j],a[k]); j=k; } } } void print(){//按顺序输出堆内元素 for(int i=1;i<=cnt;i++){ cout<<a[i]<<" "; } cout<<"\n"; }};node heap;//定义堆int main(){ heap.init(); heap.push(10); heap.push(20); heap.push(30); heap.push(5); heap.push(40); heap.print(); cout<<heap.top()<<" "<<heap.size()<<endl; if(heap.empty()){ cout<<"No"<<endl; }else{ cout<<"Yes"<<endl; } heap.pop(); heap.print(); cout<<heap.top()<<" "<<heap.size()<<endl; heap.erase(10); heap.print(); heap.clear(); cout<<heap.size()<<endl; int b[9]={0,3,1,4,1,5,9,2,6}; heap.build(8,b); heap.print(); cout<<heap.top()<<endl; return 0;}```::::::::::::::info[priority_queue优先队列]::::info[大根堆]```cpp#include<bits/stdc++.h>using namespace std;priority_queue<int> pq;//定义大根堆int main(){ pq.push(10);//插入元素 pq.push(20);//插入元素 pq.push(30);//插入元素 pq.push(5);//插入元素 pq.push(40);//插入元素 cout<<pq.top()<<endl;//输出堆顶元素 cout<<pq.size()<<endl;//输出堆内元素个数 if(pq.empty()){//判空 cout<<"Empty"<<endl; }else{ cout<<"Not Empty"<<endl; } pq.pop();//弹出堆顶元素 cout<<pq.top()<<endl;//输出弹出后的堆顶元素 cout<<pq.size()<<endl;//输出堆内元素个数 priority_queue<int> pq2; pq2.push(100); pq2.push(200); pq.swap(pq2);//交换两个堆 cout<<pq.top()<<endl;//输出交换后pq的堆顶元素 cout<<pq2.top()<<endl;//输出交换后pq2的堆顶元素 while(!pq.empty()){//清空堆 pq.pop(); } cout<<pq.size()<<endl;//输出清空后的堆大小 return 0;}```::::::::info[小根堆]```cpp#include<bits/stdc++.h>using namespace std;priority_queue<int,vector<int>,greater<int>> pq;//定义小根堆int main(){ pq.push(10);//插入元素 pq.push(20);//插入元素 pq.push(30);//插入元素 pq.push(5);//插入元素 pq.push(40);//插入元素 cout<<pq.top()<<endl;//输出堆顶元素 cout<<pq.size()<<endl;//输出堆内元素个数 if(pq.empty()){//判空 cout<<"Empty"<<endl; }else{ cout<<"Not Empty"<<endl; } pq.pop();//弹出堆顶元素 cout<<pq.top()<<endl;//输出弹出后的堆顶元素 cout<<pq.size()<<endl;//输出堆内元素个数 priority_queue<int,vector<int>,greater<int>> pq2; pq2.push(100); pq2.push(200); pq.swap(pq2);//交换两个堆 cout<<pq.top()<<endl;//输出交换后pq的堆顶元素 cout<<pq2.top()<<endl;//输出交换后pq2的堆顶元素 while(!pq.empty()){//清空堆 pq.pop(); } cout<<pq.size()<<endl;//输出清空后的堆大小 return 0;}```:::::::::::::::