思维导图:
![](https://i-blog.csdnimg.cn/blog_migrate/fdcb920a872faeb2296f082581825ae3.png)
改变类型只需将选择功能函数中的Zhan<double> z;中的double改为相对的类型:
栈:
![](https://i-blog.csdnimg.cn/blog_migrate/34df57adc9cec1411223227f7bab4efd.png)
头文件:
#ifndef ZHAN_H
#define ZHAN_H
#include <iostream>
using namespace std;
template<typename T>
class Zhan
{
private:
T *zhan;//存放栈中的数据
int N;//栈空间
int top;//栈顶
public:
//构造函数设置空间为10
Zhan();
//析构函数
~Zhan();
Zhan(const Zhan &other);
//清空栈
void clear();
//是否栈空
int empty();
//是否栈满
int full();
//获取栈顶元素
void top_data();
//进栈
void into();
//出栈
int out();
//栈的大小
void len();
void show();
};
//选择功能函数
void choose();
#endif // ZHAN_H
功能文件:
#include"zhan.h"
//构造函数
template <typename T>
Zhan<T>::Zhan()
{
N=10;
zhan=new T[N];//分配栈空间
this->top=-1;//初始化栈顶
}
//析构函数
template <typename T>
Zhan<T>::~Zhan()
{
delete zhan;
}
//拷贝构造
template <typename T>
Zhan<T>::Zhan(const Zhan &other)
{
zhan=new T(*other.zhan);
N=other.N;
top=other.top;
}
//清空栈
template <typename T>
void Zhan<T>::clear()
{
this->top=-1;
cout<<"清空成功"<<endl;
}
//是否栈空
template <typename T>
int Zhan<T>::empty()
{
return (this->top==-1)?1:0;
}
//是否栈满
template <typename T>
int Zhan<T>::full()
{
return (this->top==N-1)?1:0;
}
//获取栈顶元素
template <typename T>
void Zhan<T>::top_data()
{
if(empty())
return;
else
{
int a=0;
a=this->top;
cout<<"栈顶元素为:"<<zhan[a--]<<endl;
}
}
//进栈
template <typename T>
void Zhan<T>::into()
{
if(full())
return;
else
{
T x;
cout<<"请输入入栈元素:";
cin>>x;
while(getchar()!=10);
zhan[++(this->top)]=x;
cout<<x<<"入栈成功"<<endl;
}
}
//出栈
template <typename T>
int Zhan<T>::out()
{
if(empty())
return 0;
else
return zhan[(this->top)--];
}
//栈的大小
template <typename T>
void Zhan<T>::len()
{
T a=top;
a+=1;
cout<<"现在栈的大小为:"<<a<<endl;
}
template <typename T>
void Zhan<T>::show()
{
cout<<"现在栈元素为:";
T a=this->top;
for(int i=0;i<=a;i++)
{
cout<<zhan[i]<<" ";
}
cout<<endl;
}
//选择功能函数
void choose()
{
Zhan<double> z;
int i=0;
while(1)
{
system("CLS");
z.show();
cout<<"******1.入栈***********"<<endl;
cout<<"******2.出栈***********"<<endl;
cout<<"******3.清空栈*********"<<endl;
cout<<"******4.栈顶元素********"<<endl;
cout<<"******5.栈的大小********"<<endl;
cout<<"******6.退出********"<<endl;
cout<<"请选择功能:";
cin>>i;
while(getchar()!=10);
switch(i)
{
case 1:
z.into();
break;
case 2:
z.out();
break;
case 3:
z.clear();
break;
case 4:
z.top_data();
break;
case 5:
z.len();
break;
case 6:
return;
break;
default:
cout<<"输入错误,请重新输入"<<endl;
break;
}
cout<<"请输入任意字符重新选择:";
getchar();
}
}
测试文件:
#include"zhan.h"
int main()
{
choose();
return 0;
}
队列:
![](https://i-blog.csdnimg.cn/blog_migrate/7d1e2f2c9d6e4e46863bc6a3e1af3815.png)
头文件:
#ifndef DL_H
#define DL_H
#include <iostream>
using namespace std;
template<typename T>
class Duilie
{
private:
T *duilie;//存放栈中的数据
int N;//队列空间
int top;//队列头
int tail;//队列尾
public:
//构造函数设置空间为10
Duilie();
//析构函数
~Duilie();
//拷贝构造
Duilie(const Duilie &other);
//清空队列
void clear();
//是否队列空
int empty();
//是否队列满
int full();
//进队列
void into();
//出队列
int out();
//队列的大小
void len();
void show();
};
void choose();
#endif // DL_H
功能文件:
#include"dl.h"
template <typename T>
Duilie<T>::Duilie()
{
N=10;
duilie=new T[N];//分配队列空间
this->top=0;//初始化队列头尾
this->tail=0;
}
//析构函数
template <typename T>
Duilie<T>::~Duilie()
{
delete duilie;
}
//拷贝构造
template <typename T>
Duilie<T>::Duilie(const Duilie &other)
{
duilie=new T(*other.duilie);
N=other.N;
top=other.top;
tail=other.tail;
}
//清空队列
template <typename T>
void Duilie<T>::clear()
{
for(int i=this->top;i!=this->tail;i=(i+1)%N)
{
duilie[i]=0;
}
this->tail=this->top=0;
cout<<"清空成功"<<endl;
}
//是否队列空
template <typename T>
int Duilie<T>::empty()
{
return (this->top==this->tail)?1:0;
}
//是否队列满
template <typename T>
int Duilie<T>::full()
{
int a=this->tail;
return ((a+1)%N==this->top)?1:0;
}
//进队列
template <typename T>
void Duilie<T>::into()
{
if(full())
return;
else
{
T x;
int a=this->tail;
cout<<"请输入入队列的元素:";
cin>>x;
while(getchar()!=10);
duilie[a]=x;
this->tail=(this->tail+1)%N;
cout<<x<<"入队列成功"<<endl;
}
}
//出队列
template <typename T>
int Duilie<T>::out()
{
if(empty())
return 0;
else
{
cout<<duilie[this->top]<<"出队列成功"<<endl;
this->top=(this->top+1)%N;
return 1;
}
}
//队列的大小
template <typename T>
void Duilie<T>::len()
{
int a=0;
a=(this->tail+N-this->top)%N;
cout<<"现在队列的大小为:"<<a<<endl;
}
template <typename T>
void Duilie<T>::show()
{
int a=this->top,b=this->tail;
cout<<"现在队列从头到尾元素为:";
for(int i=a;i!=b;i=(i+1)%N)
{
cout<<duilie[i]<<" ";
}
cout<<endl;
}
//选择功能函数
void choose()
{
int i=0;
Duilie<double> d;
while(1)
{
system("CLS");
d.show();
cout<<"******1.入队列***********"<<endl;
cout<<"******2.出队列***********"<<endl;
cout<<"******3.清空队列*********"<<endl;
cout<<"******4.队列的大小********"<<endl;
cout<<"******5.退出********"<<endl;
cout<<"请选择功能:";
cin>>i;
while(getchar()!=10);
switch(i)
{
case 1:
d.into();
break;
case 2:
d.out();
break;
case 3:
d.clear();
break;
case 4:
d.len();
break;
case 5:
return;
break;
default:
cout<<"输入错误,请重新输入"<<endl;
break;
}
cout<<"请输入任意字符重新选择:";
getchar();
}
}
测试文件:
#include"dl.h"
int main()
{
choose();
return 0;
}