標籤:

SeqStack 順序棧 完結

#ifndef SEQSTACK_H#define SEQSTACK_H#include <iostream>#include <assert.h>using namespace std;const int stackincreasement=20;//每次溢出的增量template<class T>class SeqStack{public: SeqStack(int sz=50); ~SeqStack(){delete []elements;} void Push(const T& x); bool Pop(T& x); bool IsEmpty(){return (top==-1)?true:false;} bool IsFull(){return (top==maxSize-1)?true:false;} int getSize()const{return top+1;} void makeEmpty(){top=-1;} bool getTop(T& x); friend ostream& operator<< (ostream& os,SeqStack<T>& x){ if(x.IsEmpty()) os<<"empty!!!"; else{ for(int t=0;t<=x.top;t++){ os<<t<<x.elements[t]<<" "; } } os<<"
"; return os; }private: T* elements; int top;//棧頂指針的下標 int maxSize;//棧最大容量 void overflowProcess();//溢出處理};template<class T>void SeqStack<T>::overflowProcess(){ T* temp=new T[maxSize+stackincreasement]; for(int t=0;t<=top;t++){ temp[t]=elements[t]; } delete []elements; elements=temp; cout<<"increase down"<<endl;}template<class T>void SeqStack<T>::Push(const T &x){ if(!IsFull()){ top++; elements[top]=x; cout<<top<<" push it!"<<endl; } else{ this->overflowProcess(); maxSize+=stackincreasement; top++; elements[top]=x; cout<<top<<" push it it"<<endl; }}template<class T>SeqStack<T>::SeqStack(int sz){ maxSize=sz; elements=new T[maxSize]; top=-1;}template<class T>bool SeqStack<T>::Pop(T &x){ if(IsEmpty()) { cout <<"empty!no pop"<<endl; return false; } x=elements[top]; top--; return true;}template<class T>bool SeqStack<T>::getTop(T &x){ if(IsEmpty()) { cout<<"empty!"<<endl; return false; } else{ x=elements[top]; return true; }}#endif // SEQSTACK_H

推薦閱讀:

作為普通中國人(非官員),拳匪之亂八國聯軍進京之後,如何最大地減少損失獲得利益?
如何學習編程語言?
程序猿的崛起,一篇文章看懂編程語言!
挑戰真正的編程能力 ——「 PHP挑戰賽 」下周開始
Python分詞模塊jieba (01)-jieba安裝,分詞,提取關鍵詞,自定義分詞,切換詞庫講解

TAG:互聯網 | 編程 |