標籤:雙鏈表 付出 思考 物聯網 棧
#include<iostream.h> template<class T> struct STU {T data;STU<T> * next;};template<class T> class LinkStack { public: LinkStack(){top=NULL;} ~LinkStack(); void Push(T x); T Pop(); T GetTop(){if(top!=NULL)return top->data;}; int Empty(){top==NULL?return 1:return 0;};private: STU<T> * top;}; template<class T> class LinkStack<T>::Push(T x){s=new STU;s->data=x;s->next=top;top=s;}template<class T> class LinkStack<T>::Pop(){STU<T> *p;T x;if(top=NULL)throw"Wrong";x=top->data;p=top;top=top->next;delete p;return x;}int main() { LinkStack<int>s1; int m; do{ cout<<"-------------------------"<<endl; cout<<"輸入1開始入棧"<<endl; cout<<"輸入2開始彈棧"<<endl; cout<<"輸入3開始讀取棧頂"<<endl; cout<<"輸入4開始判斷是否空"<<endl; cin>>m; switch(m) { case 1: cout<<"請輸入入棧的元素值"<<endl; cin>>a; s1.Push(a); break; case 2: cout<<"此操作為出棧"<<endl; s1.Pop(); break; case 3: int v; cout<<"此操作為讀取棧頂"<<endl; s1.GetTop(); break; case 4: cout<<"此操作為判斷棧為空白"<<endl; s1.Empty(); break; } }while(1); return 0; }
“鏈棧”實際代碼實現【物聯網1132-11】