Yuav Ua Li Cas Kom Huv Si Pawg

Cov txheej txheem:

Yuav Ua Li Cas Kom Huv Si Pawg
Yuav Ua Li Cas Kom Huv Si Pawg

Video: Yuav Ua Li Cas Kom Huv Si Pawg

Video: Yuav Ua Li Cas Kom Huv Si Pawg
Video: Yuav ua li cas kom tus txiv neej huam ua ntej yuav siv aim 2024, Kaum ib hlis
Anonim

Ib qho ntawm cov hom qauv ntaub ntawv uas tau siv dav nyob rau niaj hnub no hauv cov ntaub ntawv thov kev pab yog cov khoom muag. Nws cov peculiarity yog lub hauv paus ntsiab lus ntawm kev teeb tsa cov khoom, uas lawv ntxiv thiab tshem tawm yog ua tau tsuas yog ib qho zuj zus thiab tsuas yog los ntawm "saum toj", uas yog, raws li LIFO txoj ntsiab cai. Tab sis qee zaus nws tsim nyog los tshem tawm cov pawg tag nrho ntawm ib zaug.

Yuav ua li cas kom huv si pawg
Yuav ua li cas kom huv si pawg

Tsim nyog

  • - ntawv nyeem editor lossis IDE;
  • - tus txhais lus los ntawm cov lus siv programming.

Cov Lus Qhia

Kauj ruam 1

Siv cov hau kev ntawm cov khoom pob uas tshwj xeeb uas tsim los ntxuav tawm cov pawg. Lawv muaj nyob rau hauv feem ntau ntawm cov chav kawm coj ntawm ntau lub tsev qiv ntawv thiab qauv haujlwm. Piv txwv,. NET chav kawm chav kawm muaj cov txheej txheem Ntshiab. Ib qho piv txwv ntawm nws cov ntawv thov hauv C # yuav zoo li qhov no:

Cov tshooj oStack = tshiab Stack (); // tsim cov kwv kwv

oStack. Push ("000"); // sau rau pawg

oStack. Push ("111");

oStack. Clear (); // tshem tawm pawg

Kauj ruam 2

Cov hau kev rau kev hloov cov naj npawb ntawm cov khoom ntawm cov chav kawm thawv, uas ua haujlwm ntawm cov chav kawm feem ntau tau tsim, kuj tseem siv tau rau kev ntxuav. Koj tsuas yog xav tau los txo cov ntsiab lus tam sim no mus rau xoom. Piv txwv li Qt template chav kawm QStack cov qub txeeg qub teg los ntawm QVector template chav kawm, uas muaj cov qauv kho qhov loj. Ib qho piv txwv ntawm nws qhov kev siv yuav yog zoo li no:

QSack oStack; // kev tshaj tawm ntawm pawg khoom

rau (rau cov menyuam = =; i <10; i ++) oStack.push (i); // sau rau pawg

oStack.resize (0); // tshem tawm pawg

Kauj ruam 3

Kev ntxuav tu lub teeb khoom feem ntau tuaj yeem ua los ntawm tus neeg teb xov tooj, uas feem ntau ua tiav nyob rau hauv cov chav kawm coj. Txhawm rau ua qhov no, lub hom phiaj ntawm cov pawg yuav tsum tau muab tshem tawm yuav tsum tau muab tso rau ib ntus khoom tsim los ntawm tus qauv siv tam sim no. Piv txwv li, C ++ Chav Qiv Ntawv cov ntawv pov tseg templated chav kawm, uas yog kev hloov kho rau cov thawv ntim hauv chav kawm, tsis muaj txoj kev rau kev hloov ntau tus lej lossis tshem tag nrho cov ntsiab lus. Koj tuaj yeem tshem nws zoo li no:

std:: pawg <int, std:: list> oStack; // kev tshaj tawm ntawm pawg khoom

rau (rau cov menyuam = =; i <10; i ++) oStack.push (i); // sau rau pawg

oStack = std:: txhawv(); // tshem tawm paw

Kauj ruam 4

Tshem cov pawg khoom uas yog hu rau daim qauv tsim qauv siv tus neeg ua haujlwm tshiab nrog cov khoom sib cav tsim los ntawm tus qauv txhais:

std:: pawg <int, std:: list> oStack; // kev tshaj tawm ntawm pawg khoom

rau (rau cov menyuam = =; i <10; i ++) oStack.push (i); // sau rau pawg

tshiab std:: stack(oStack); // tshem tawm paw

Kauj ruam 5

Cov pawg tuaj yeem muab qhov kev ua kom meej los ntawm kev rov ua tag nrho cov ntsiab lus uas siv cov qauv uas tsim nyog:

std:: pawg <int, std:: list> oStack; // kev tshaj tawm ntawm pawg khoom

rau (rau cov menyuam = =; i <10; i ++) oStack.push (i); // sau rau pawg

thaum (! oStack.empty ()) oStack.pop (); // tshem tawm pawg

Txawm li cas los xij, txoj hauv kev no muaj lub sijhawm nyuaj uas linearly nyob ntawm tus naj npawb ntawm cov khoom hauv pawg. Yog li ntawd, nws qhov kev siv tsis meej pem.

Pom zoo: