编写一个STACK的类
栈的ADT(至少)具有以下特点:? 可建立空堆栈;
? 可从栈顶删除数据项(弹出);
? 可将数据项添加到栈顶(压入);
? 可查看堆栈是否为空;
编写一个STACK的类,该类要符合上述ADT要求。
SeqStack.h文件 //SeqStack.h SeqStack模板类的定义 template<class EleType> class SeqStack { protected: EleType * Array; int iTop,iMaxSize; public: SeqStack(int i); ~SeqStack(); int Pop(EleType &); int Push(EleType &); int IsEmpty() const; int IsFull() const; int ShowElement() const; }; SeqStack.cpp文件 //SeqStack.cpp SeqStack模板类的实现 #include "SeqStack.h" #include "iostream" using namespace std; template<class EleType> SeqStack<EleType>:: SeqStack(int i) { iMaxSize=i; iTop=-1; Array=new EleType[iMaxSize]; } template<class EleType> SeqStack<EleType>:: ~SeqStack() { delete [] Array; } template<class EleType> int SeqStack<EleType>:: Pop(EleType &TopElement) { if(iTop==-1) { std::cout<<"\n栈已空!"; return 1; } else { TopElement=Array[iTop]; iTop--; return 0; } } template<class EleType> int SeqStack<EleType>:: Push(EleType &TopElement) { if(iTop==iMaxSize-1) { std::cout<<"\n栈已满!"; return 1; } else { iTop++; Array[iTop]=TopElement; return 0; } } template<class EleType> inline int SeqStack<EleType>:: IsEmpty() const { return iTop==-1; } template<class EleType> inline int SeqStack<EleType>:: IsFull() const { return iTop==iMaxSize-1; } template<class EleType> int SeqStack<EleType>:: ShowElement() const { if(iTop==-1) { std::cout<<"\n栈已空!"; return 1; } else { cout<<'\n'; for(int i=iTop;i>=0;i--) cout<<Array[i]<<'\t'; return 0; } } Test_SeqStack.cpp文件 //Test_SeqStack.cpp 程序入口,对SeqStack模板类的检验 #include "SeqStack.h" #include "SeqStack.cpp" #include "iostream" int GetChoice(); using namespace std; int main() { SeqStack<int> MySeqStack(5); int iChoice=0; while(iChoice=GetChoice()) { switch(iChoice) { case 1: { if(MySeqStack.IsEmpty()) cout<<"栈已空!\n"; else cout<<"栈未空!\n"; break; } case 2: { int a; if(!MySeqStack.IsFull()) { cout<<"你要入栈的整数是:"; cin>>a; MySeqStack.Push(a); MySeqStack.ShowElement(); } else cout<<"栈已满\n"; break; } case 3: { int a; if(!MySeqStack.IsEmpty()) { MySeqStack.Pop(a); cout<<"你出栈的整数是:" <<a<<endl; MySeqStack.ShowElement(); } else cout<<"栈已空\n"; break; } default: cout<<"错误的输入\n"; } } return 0; } int GetChoice() { int iChoice=0; cout<<"\n\t\t1 判栈空\n\t\t2 入栈\n\t\t3 出栈\n\t\t0 退出\n\t"; cin>>iChoice; return iChoice; } 链栈的实现:建立新的工程Test_LinkStack.h,由stdafx.h,LinkStack.h,LinkStack.cpp,Test_LinkStack.cpp组成,内容如下: (1) stdafx.h文件: // stdafx.h : 标准系统包含文件的包含文件, #pragma once #include <iostream> (2) LinkStack.h文件: // LinkStack模板类的定义 template<class ValueType> class Node { protected: ValueType Value; Node *Next; public: Node() {Next=NULL;} ~Node() {} void SetValue(ValueType a) {Value=a;} void SetNext(Node * a) {Next=a;} ValueType &GetValue() {return Value; } Node * GetNext() {return Next;} }; template<class ValueType> class LinkStack { protected: Node<ValueType> * Head; public: LinkStack(); ~LinkStack(); int Pop(ValueType &); int Push(const ValueType ); bool IsEmpty() const; int ShowElement() const; }; (3)LinkStack.cpp文件 //LinkStack.cpp LinkStack模板类成员函数的实现 #include "stdafx.h" #include "LinkStack.h" template<class ValueType> LinkStack<ValueType>:: LinkStack() { Head=new Node<ValueType>; Head->SetNext(NULL); } template<class ValueType> LinkStack<ValueType>:: ~LinkStack() { Node<ValueType> *pNode; if(!IsEmpty()) { pNode=Head->GetNext(); Head->SetNext(pNode->GetNext()); delete pNode; } delete Head; } template<class ValueType> int LinkStack<ValueType>:: Pop(ValueType &TopElement) { if(this->IsEmpty()) { std::cout<<"栈已空!"; return 1; } else { Node<ValueType> *pNode; pNode=Head->GetNext(); Head->SetNext(pNode->GetNext()); TopElement=pNode->GetValue(); delete pNode; return 0; } } template<class ValueType> int LinkStack<ValueType>:: Push(const ValueType TopElement) { Node<ValueType> *pNode; pNode=new Node<ValueType>; pNode->SetValue(TopElement); pNode->SetNext(Head->GetNext()); Head->SetNext(pNode); return 0; } template<class ValueType> inline bool LinkStack<ValueType>:: IsEmpty() const { return Head->GetNext()==NULL; } template<class ValueType> int LinkStack<ValueType>:: ShowElement() const { if(IsEmpty()) { std::cout<<"\n栈已空!"; return 1; } else { cout<<'\n'; Node<ValueType> *pNode,*temp; for(pNode=Head->GetNext();pNode->GetNext()!=NULL;pNode=pNode->GetNext()) { cout<<pNode->GetValue()<<'\t'; temp=pNode->GetNext(); pNode=temp; } return 0; } } (4)Test_LinkStack.cpp文件 //Test_LinkStack.cpp 程序入口,对LinkStack模板类的检验 #include "stdafx.h" #include "LinkStack.cpp" int GetChoice(); using namespace std; int main() { LinkStack<int> MyLinkStack; int iChoice=0; while(iChoice=GetChoice()) { switch(iChoice) { case 1: { if( MyLinkStack.IsEmpty()) cout<<"栈已空!\n"; else cout<<"栈未空!\n"; break; } case 2: { int a; cout<<"你要入栈的整数是:"; cin>>a; MyLinkStack.Push(a); MyLinkStack.ShowElement(); break; } case 3: { int a; if(!MyLinkStack.IsEmpty()) { MyLinkStack.Pop(a); cout<<"你出栈的整数是:" <<a<<endl; MyLinkStack.ShowElement(); } else cout<<"栈已空\n"; break; } default: cout<<"错误的输入\n"; } } return 0; } // 获取用户选择 int GetChoice() { int iChoice=0; cout<<"\n\t\t1 判栈空\n\t\t2 入栈\n\t\t3 出栈\n\t\t0 退出\n\t"; cin>>iChoice; return iChoice; }