歡迎您光臨本站 註冊首頁

C++模板實現順序棧

←手機掃碼閱讀     bom485332 @ 2020-05-03 , reply:0

順序棧:利用一組連續的存儲單元依次存放自棧底到棧頂的數據元素;由於棧頂元素是經常變動的,所以附設top指示棧頂元素在順序表中的位置,同時也需要知道順序棧存儲空間的起始位置,因此還需設定一個base指針用來指示棧空間的起始位置。
一般約定top指針指向棧頂元素的下一個位置,即新數據元素將要插入得位置。
下面我們使用模板簡單實現一個順序棧:
SeqStack.h
template

class SeqStack{ public: SeqStack(int sz):m_ntop(-1),m_nMaxSize(sz){ m_pelements=new Type[sz]; if(m_pelements==NULL){ cout<<"application public:="" void="" const="" type="" push="" data="" pop="" get="" print="" the="" stack="" make="" empty="" m_ntop="=m_nMaxSize-1;" bool="" return="" private:="" int="" typename="" is="" there="" no="" i="0;i<=m_ntop;i++){" ---="">"<<m_pelements[i]; } cout<<"--->top"<<endl<<endl<<endl; }
Main.cpp

#includeusing namespace std; #include "SeqStack.h" int main(){ SeqStackstack(10); int init[10]={1,2,6,9,0,3,8,7,5,4}; for(int i=0;i<10;i++){ stack.Push(init[i]); } stack.Print(); stack.Push(88); cout<<stack.Pop()<<endl; stack.Print(); stack.MakeEmpty(); stack.Print(); stack.Pop(); return 0; }

[bom485332 ] C++模板實現順序棧已經有239次圍觀

http://coctec.com/docs/c/language/show-post-232643.html