以前学数据结构时写的。
程序代码:SeqStack.h文件
//SeqStack.h SeqStack模板类的定义
template
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
SeqStack:: SeqStack(int i)
{
iMaxSize=i;
iTop=-1;
Array=new EleType[iMaxSize];
}
template
SeqStack:: ~SeqStack()
{
delete [] Array;
}
template
int SeqStack:: Pop(EleType &TopElement)
{
if(iTop==-1)
{
std::cout<
return 1;
}
else
{
TopElement=Array[iTop];
iTop--;
return 0;
}
}
template
int SeqStack:: Push(EleType &TopElement)
{
if(iTop==iMaxSize-1)
{
std::cout<
return 1;
}
else
{
iTop++;
Array[iTop]=TopElement;
return 0;
}
}
template
inline int SeqStack:: IsEmpty() const
{
return iTop==-1;
}
template
inline int SeqStack:: IsFull() const
{
return iTop==iMaxSize-1;
}
template
int SeqStack:: ShowElement() const
{
if(iTop==-1)
{
std::cout<
return 1;
}
else
{
cout<
for(int i=iTop;i>=0;i--)
cout<
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 MySeqStack(5);
int iChoice=0;
while(iChoice=GetChoice())
{
switch(iChoice)
{
case 1: {
if(MySeqStack.IsEmpty())
cout<
else
cout<
break;
}
case 2: {
int a;
if(!MySeqStack.IsFull())
{
cout<
cin>>a;
MySeqStack.Push(a);
MySeqStack.ShowElement();
}
else
cout<
break;
}
case 3: {
int a;
if(!MySeqStack.IsEmpty())
{
MySeqStack.Pop(a);
cout<
MySeqStack.ShowElement();
}
else
cout<
break;
}
default: cout<
}
}
return 0;
}
int GetChoice()
{
int iChoice=0;
cout<
cin>>iChoice;
return iChoice;
}
链栈的实现:建立新的工程Test_LinkStack.h,由stdafx.h,LinkStack.h,LinkStack.cpp,Test_LinkStack.cpp组成,内容如下:
(1) stdafx.h文件:
// stdafx.h : 标准系统包含文件的包含文件,
#pragma once
#include
(2) LinkStack.h文件:
// LinkStack模板类的定义
template
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 LinkStack
{
protected:
Node * 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
LinkStack:: LinkStack()
{
Head=new Node;
Head->SetNext(NULL);
}
template
LinkStack:: ~LinkStack()
{
Node *pNode;
if(!IsEmpty())
{
pNode=Head->GetNext();
Head->SetNext(pNode->GetNext());
delete pNode;
}
delete Head;
}
template
int LinkStack:: Pop(ValueType &TopElement)
{
if(this->IsEmpty())
{
std::cout<
return 1;
}
else
{
Node *pNode;
pNode=Head->GetNext();
Head->SetNext(pNode->GetNext());
TopElement=pNode->GetValue();
delete pNode;
return 0;
}
}
template
int LinkStack:: Push(const ValueType TopElement)
{
Node *pNode;
pNode=new Node;
pNode->SetValue(TopElement);
pNode->SetNext(Head->GetNext());
Head->SetNext(pNode);
return 0;
}
template
inline bool LinkStack:: IsEmpty() const
{
return Head->GetNext()==NULL;
}
template
int LinkStack:: ShowElement() const
{
if(IsEmpty())
{
std::cout<
return 1;
}
else
{
cout<
Node *pNode,*temp;
for(pNode=Head->GetNext();pNode->GetNext()!=NULL;pNode=pNode->GetNext())
{
cout<GetValue()<
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 MyLinkStack;
int iChoice=0;
while(iChoice=GetChoice())
{
switch(iChoice)
{
case 1: {
if( MyLinkStack.IsEmpty())
cout<
else
cout<
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<
MyLinkStack.ShowElement();
}
else
cout<
break;
}
default: cout<
}
}
return 0;
}
// 获取用户选择
int GetChoice()
{
int iChoice=0;
cout<
cin>>iChoice;
return iChoice;
}