課程
/后端開發(fā)
/C++
/數(shù)據(jù)結(jié)構(gòu)探險—棧篇
如果不知道具體需要建多大的站,構(gòu)造函數(shù)怎么寫
2016-10-29
源自:數(shù)據(jù)結(jié)構(gòu)探險—棧篇 2-4
正在回答
根據(jù)老師寫了一個整個的程序,這個是申請物理空間的,因為物理空間很大一般不會不足,符合你的問題,所以可以按需要取多少大小。
不是很規(guī)范,有所不足互相學習哈
#include<iostream> using?namespace?std; #define?elemtype?char struct?node?{ elemtype?data; node?*last; node?*next; //新定義的結(jié)點的默認構(gòu)造函數(shù)? node(const?elemtype?data?=?0,node?*last?=?NULL,node?*next?=?NULL)?{ this->data?=?data; this->last?=?last; this->next?=?next; } ~node()?{ data?=?0; last?=?next?=?NULL; } }; class?stack{ private: node?*base;?//棧底? node?*top;??//棧頂? int?size;???//棧的實際大小? public: stack()?{ //申請一個哨兵結(jié)點作為棧底,哨兵的data沒有意義? //如果申請物理空間失敗,拋出異常? if?(!(base?=?new?node()))?{ cout?<<?"內(nèi)存不足!"; //throw?... } top?=?base; size?=?0; } ~stack()?{ node?*p=base,*q=NULL; while(p)?{ q?=?p; p?=?p->next; delete?q; } } bool?isfull()?{ //嘗試申請一個node,如果可以申請說明未滿? node?*p=new?node; if?(!p)?{ return?true; } else?{ delete?p; return?false; } } bool?isempty()?{ return?!size; //也可以寫成?return?top?==?base; } void?push(const?elemtype?&c)?{ if?(isfull())?{ cout?<<?"滿了"; //throw?..? } node?*p?=?new?node(c); top->next?=?p; p->last?=?top; top?=?p; ++size; } elemtype?pop()?{ if?(isempty())?{ cout?<<?"空了"; //theow?.. } elemtype?x?=?top->data; node?*p?=?top; top?=?p->last; top->next?=?NULL; delete?p; --size; return?x; } //遍歷函數(shù),默認從棧底到棧頂? void?traverse(bool?isfrombottom?=?true)?{ node?*p; if?(isfrombottom)?{ p?=?base->next; while?(p)?{ cout?<<?p->data; p?=?p->next; } cout<<endl; } else?{ p?=?top; while?(p?!=?base)?{ cout?<<?p->data; p?=?p->last; } cout<<endl; } }? int?lenth()?{ return?size; } void?clearStack()?{ node?*p?=?base->next,*q; while?(p)?{ q?=?p; p?=?p->next; delete?q; } top?=?base; size?=?0; base->next?=?NULL; } }; int?main(void)?{ stack?*s?=?new?stack(); if?(s->isempty()) cout<<"棧為空"<<endl; cout?<<?s->lenth()<<endl; s->push('h'); s->push('e'); s->push('l'); s->push('l'); s->push('o'); cout?<<?s->lenth()<<endl; s->traverse(); s->traverse(false); s->clearStack(); cout?<<?s->lenth()<<endl; cout?<<endl; s->push('a'); s->traverse(); s->pop(); s->traverse(); //測試是不是能二次入棧? s->push('a'); s->traverse(); s->pop(); s->traverse(); //因為內(nèi)存物理空間蠻大的,所以一般棧滿的情況不會出現(xiàn)?在此不做測試? //if?(s->isfull()) //cout<<"棧為滿"<<endl; s->clearStack(); //最后這句話送給大家 s->push('H'); s->push('a'); s->push('p'); s->push('p'); s->push('y'); s->push('?'); s->push('C'); s->push('h'); s->push('r'); s->push('i'); s->push('s'); s->push('t'); s->push('m'); s->push('a'); s->push('s'); s->push('!'); s->traverse(); delete?s; return?0; }
用鏈棧,通過new或malloc動態(tài)申請內(nèi)存。但結(jié)點之間不再是數(shù)組下標之間的關(guān)系,它們是通過指針串起來的。和鏈表相似。
用new寫
舉報
棧,先入后出(FILO),帶領(lǐng)大家體會棧這種數(shù)據(jù)結(jié)構(gòu)的美妙
2 回答為什么要添加默認的構(gòu)造函數(shù),遠征系列還想沒有說吧?
1 回答析構(gòu)函數(shù),這里為什么可以不用虛函數(shù)?
2 回答最后的遍歷函數(shù)難道不用判斷棧是否為空么?
1 回答棧的類模板按照視頻做下來不能用。如下圖。不知道為什么,不識別我建的累。期待老師的回答。
1 回答關(guān)于c的數(shù)據(jù)結(jié)構(gòu)和c++數(shù)據(jù)結(jié)構(gòu)
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學習伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2016-12-25
根據(jù)老師寫了一個整個的程序,這個是申請物理空間的,因為物理空間很大一般不會不足,符合你的問題,所以可以按需要取多少大小。
不是很規(guī)范,有所不足互相學習哈
2016-12-24
用鏈棧,通過new或malloc動態(tài)申請內(nèi)存。但結(jié)點之間不再是數(shù)組下標之間的關(guān)系,它們是通過指針串起來的。和鏈表相似。
2016-10-30
用new寫