/**
// //
// 堆栈数据结构 stack.h
//
/**
#include<iostream.h>
template<class Type>class Stack;
template<class Type>
class StackNode
{
friend class Stack<Type>;
private:
Type data;
StackNode<Type> *link;
StackNode(Type D=0,StackNode<Type> *L=NULL):link(L),data(D)......{}
};
template<class Type>
class Stack
{
public:
Stack():top(NULL),NumItem(0)......{}
void Push(Type item);
Type Pop();
Type GetTop();
void MakeEmpty();
bool ISEmpty();
int GetNum();
private:
int NumItem;
StackNode<Type> *top;
};
template<class Type>
void Stack<Type>::Push(Type item)
{
top=new StackNode<Type>(item,top);
NumItem++;
}
template<class Type>
Type Stack<Type>::Pop()
...{
StackNode<Type> *p;
Type temp;
temp=top->data;
p=top;
top=top->link;
delete p;
NumItem--;
return temp;
}
template<class Type>
Type Stack<Type>::GetTop()
{
return top->data;
}
template<class Type>
bool Stack<Type>::ISEmpty()
...{
return top==NULL;
}
template<class Type>
void Stack<Type>::MakeEmpty()
{
delete top;
}
template<class Type>
int Stack<Type>::GetNum()
{
return NumItem;
}
/**//
// //
// 堆栈功能函数 Stack.cpp/ /
// //
/**/
#include"Stack.h"
#include"iostream.h"
const int INT =13;
const double FLOAT= 13.33;
const char CHAR ='a';
template<class Type>
void Stack_Push(Stack<Type> &StackOPP)
{
cout<<"请输入要插入的数据项: ";
Type item;
cin>>item;
StackOPP.Push(item);
}
template<class Type>
void Stack_Pop(Stack<Type> &StackOPP)
{
if(!StackOPP.ISEmpty())
{
cout<<"出栈数据项: ";
cout<<StackOPP.Pop()<<endl;
}
else
{
cout<<"堆栈已经为空!"<<endl;
}
}
template<class Type>
void Stack_ISEmpty(Stack<Type> &StackOPP)
{
if(!StackOPP.ISEmpty())
cout<<"堆栈不空,还有"<<StackOPP.GetNum()<<"数据项!"<<endl;
else
cout<<"堆栈为空!"<<endl;
}
template<class Type>
void Stack_GetTop(Stack<Type> &StackOPP)
{
if(!StackOPP.ISEmpty())
cout<<"栈顶元素为:"<<StackOPP.GetTop()<<endl;
else
cout<<"堆栈为空!"<<endl;
}
template<class Type>
void Stack_MakeEmpty(Stack<Type> &StackOPP)
{
if(!StackOPP.ISEmpty())
{
StackOPP.MakeEmpty();
cout<<"堆栈已经销毁!"<<endl;
}
else
{
cout<<"销毁失败!"<<endl;
}
}
template<class Type>
void StackINI(Type temp)
{
Stack<Type> StackOPP;
do
{
cout<<"堆栈的操作: "<<endl
<<" 1) 插入堆栈"<<endl
<<" 2) 出栈"<<endl
<<" 3) 堆栈是否为空"<<endl
<<" 4) 得栈顶数据项"<<endl
<<" 5) 销毁堆栈"<<endl
<<" X) 退出堆栈操作"<<endl;
int item;
cin>>item;
switch(item)
{
case 1: Stack_Push(StackOPP); break;
case 2: Stack_Pop(StackOPP); break;
case 3: Stack_ISEmpty(StackOPP); break;
case 4: Stack_GetTop(StackOPP); break;
case 5: Stack_MakeEmpty(StackOPP); break;
default: return ;
}
}while(true);
}
void STACK()
{
int item;
cout<<"清选择数据类型: 1) 整型 2) 浮点型 3) 字符型 X) 退出: ";
cin>>item;
switch(item)
{
case 1: StackINI(INT); break; //根据不同的用户需要选择数据类型
case 2: StackINI(FLOAT); break;
case 3: StackINI(CHAR); break;
default: return ; break;
}
}
// //
// 堆栈数据结构 stack.h
//
/**
#include<iostream.h>
template<class Type>class Stack;
template<class Type>
class StackNode
{
friend class Stack<Type>;
private:
Type data;
StackNode<Type> *link;
StackNode(Type D=0,StackNode<Type> *L=NULL):link(L),data(D)......{}
};
template<class Type>
class Stack
{
public:
Stack():top(NULL),NumItem(0)......{}
void Push(Type item);
Type Pop();
Type GetTop();
void MakeEmpty();
bool ISEmpty();
int GetNum();
private:
int NumItem;
StackNode<Type> *top;
};
template<class Type>
void Stack<Type>::Push(Type item)
{
top=new StackNode<Type>(item,top);
NumItem++;
}
template<class Type>
Type Stack<Type>::Pop()
...{
StackNode<Type> *p;
Type temp;
temp=top->data;
p=top;
top=top->link;
delete p;
NumItem--;
return temp;
}
template<class Type>
Type Stack<Type>::GetTop()
{
return top->data;
}
template<class Type>
bool Stack<Type>::ISEmpty()
...{
return top==NULL;
}
template<class Type>
void Stack<Type>::MakeEmpty()
{
delete top;
}
template<class Type>
int Stack<Type>::GetNum()
{
return NumItem;
}
/**//
// //
// 堆栈功能函数 Stack.cpp/ /
// //
/**/
#include"Stack.h"
#include"iostream.h"
const int INT =13;
const double FLOAT= 13.33;
const char CHAR ='a';
template<class Type>
void Stack_Push(Stack<Type> &StackOPP)
{
cout<<"请输入要插入的数据项: ";
Type item;
cin>>item;
StackOPP.Push(item);
}
template<class Type>
void Stack_Pop(Stack<Type> &StackOPP)
{
if(!StackOPP.ISEmpty())
{
cout<<"出栈数据项: ";
cout<<StackOPP.Pop()<<endl;
}
else
{
cout<<"堆栈已经为空!"<<endl;
}
}
template<class Type>
void Stack_ISEmpty(Stack<Type> &StackOPP)
{
if(!StackOPP.ISEmpty())
cout<<"堆栈不空,还有"<<StackOPP.GetNum()<<"数据项!"<<endl;
else
cout<<"堆栈为空!"<<endl;
}
template<class Type>
void Stack_GetTop(Stack<Type> &StackOPP)
{
if(!StackOPP.ISEmpty())
cout<<"栈顶元素为:"<<StackOPP.GetTop()<<endl;
else
cout<<"堆栈为空!"<<endl;
}
template<class Type>
void Stack_MakeEmpty(Stack<Type> &StackOPP)
{
if(!StackOPP.ISEmpty())
{
StackOPP.MakeEmpty();
cout<<"堆栈已经销毁!"<<endl;
}
else
{
cout<<"销毁失败!"<<endl;
}
}
template<class Type>
void StackINI(Type temp)
{
Stack<Type> StackOPP;
do
{
cout<<"堆栈的操作: "<<endl
<<" 1) 插入堆栈"<<endl
<<" 2) 出栈"<<endl
<<" 3) 堆栈是否为空"<<endl
<<" 4) 得栈顶数据项"<<endl
<<" 5) 销毁堆栈"<<endl
<<" X) 退出堆栈操作"<<endl;
int item;
cin>>item;
switch(item)
{
case 1: Stack_Push(StackOPP); break;
case 2: Stack_Pop(StackOPP); break;
case 3: Stack_ISEmpty(StackOPP); break;
case 4: Stack_GetTop(StackOPP); break;
case 5: Stack_MakeEmpty(StackOPP); break;
default: return ;
}
}while(true);
}
void STACK()
{
int item;
cout<<"清选择数据类型: 1) 整型 2) 浮点型 3) 字符型 X) 退出: ";
cin>>item;
switch(item)
{
case 1: StackINI(INT); break; //根据不同的用户需要选择数据类型
case 2: StackINI(FLOAT); break;
case 3: StackINI(CHAR); break;
default: return ; break;
}
}