数据结构算法--堆栈
来源:互联网 发布:thinkphp与php的区别 编辑:程序博客网 时间:2024/05/30 04:53
/**////////////////////////////
// //
// 堆栈数据结构 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;
}
}
0 0
- 【数据结构与算法】堆栈
- 数据结构算法--堆栈
- 数据结构之堆栈模拟应用(算法3.1)
- 数据结构与算法01--堆栈 & 队列
- 用python学习数据结构与算法-堆栈
- 数据结构:堆栈
- 数据结构--堆栈
- 数据结构--堆栈
- 数据结构-堆栈
- 数据结构 堆栈
- 用C#的类实现数据结构的堆栈算法
- 用C#的类实现数据结构的堆栈算法
- 新手学习数据结构与算法---堆栈基本操作
- 堆栈数据结构应用之括号匹配检查(算法3.2.2)
- 数据结构与算法之动态数组实现堆栈
- 【算法学习笔记】06.数据结构基础 队列与堆栈初步
- 算法设计优化中常用到的数据结构--堆栈
- 算法与数据结构学习 03 递归和堆栈
- 设计模式—FactoryMethod工厂模式
- 区分JS中的undefined,null,"",0和false
- 面向对象的三个基本特征
- 数据结构算法集--C++语言实现
- jquery easyui 日期框怎么禁用输入和日期框加readonly失效的解决办法
- 数据结构算法--堆栈
- 数据结构算法--队列
- 数据结构算法--链表
- VS2012 Release编译时出现 “error LNK2026: 模块对于 SAFESEH 映像是不安全的。” 错误提示的解决方法
- 数据结构算法--图
- 数据结构算法--排序
- 我的Linux内核学习之路
- 数据结构算法--二叉树
- IPTV中的流传输技术研究与分析