1、栈
#include <iostream>
#define MAX 8
using namespace std;
template <typename type>
class Shu
{
type *arr; //栈的数组
int top; //记录栈顶元素的下标
public:
//构造函数
Shu(){}
//有参构造函数
Shu(int t):top(t)
{
arr=new type[MAX];
}
//拷贝构造
Shu(const Shu&other):top(other.top)
{
other.arr=new type[MAX];
}
//析构函数
~Shu()
{
delete []arr;
arr=nullptr;
}
//判断空
int stack_empty();
//判断满
int stack_full();
//入栈
int stack_push(type && data);
//遍历栈
void stack_show();
//出栈
int stack_pop();
//获取栈顶元素
int stack_top();
//求栈的大小
int stack_size();
//清空栈
void stack_free();
};
template <typename type>
//判断空
int Shu<type>::stack_empty()
{
if(NULL==arr)
{
cout<<"判空失败"<<endl;
return -1;
}
return this->top==-1;
}
template <typename type>
//判断满
int Shu<type>::stack_full()
{
if(NULL==arr)
{
cout<<"判满失败"<<endl;
return -1;
}
return this->top==MAX-1;
}
template <typename type>
//入栈
int Shu<type>::stack_push(type && data)
{
if(stack_full())
{
cout<<"入栈失败"<<endl;
return -1;
}
//top下标自增
this->top++;
//将输入的函数放入数组中
arr[this->top]=data;
cout<<"入栈成功"<<endl;
return 0;
}
template <typename type>
//遍历栈
void Shu<type>::stack_show()
{
if(stack_empty())
{
cout<<"遍历失败"<<endl;
return ;
}
//遍历
for(int i=0;i<this->top+1;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
template <typename type>
//出栈
int Shu<type>::stack_pop()
{
if(stack_empty())
{
cout<<"出栈失败"<<endl;
return -1;
}
//出栈
cout<<arr[this->top]<<"出栈成功"<<endl;
//自减
this->top--;
return 0;
}
template <typename type>
//获取栈顶元素
int Shu<type>::stack_top()
{
if(stack_empty())
{
cout<<"获取栈顶元素失败"<<endl;
return -1;
}
//获取栈顶元素
cout<<"栈顶元素为:"<<arr[top]<<endl;
return 0;
}
template <typename type>
//求栈的大小
int Shu<type>::stack_size()
{
if(stack_empty())
{
cout<<"求栈大小失败"<<endl;
return -1;
}
//栈的大小
int size=this->top+1;
cout<<"栈的大小为:"<<size<<endl;
return 0;
}
template <typename type>
//清空栈
void Shu<type>::stack_free()
{
this->top=-1;
cout<<"清空栈成功"<<endl;
}
int main()
{
Shu<int> zhan(-1);
//入栈
zhan.stack_push(2);
zhan.stack_push(5);
zhan.stack_push(3);
zhan.stack_push(8);
//遍历栈
zhan.stack_show();
//出栈
zhan.stack_pop();
zhan.stack_pop();
//获取栈顶元素
zhan.stack_top();
//求栈的大小
zhan.stack_size();
//清空栈
zhan.stack_free();
return 0;
}
2、循环队列
#include <iostream>
#define MAX 8
using namespace std;
template <typename type>
class Shu
{
type *arr; //循环队列的数组
int head; //记录队列头元素的下标
int tail; //记录队列尾元素下标
public:
//构造函数
Shu(){}
//有参构造
Shu(int h,int t):head(h),tail(t)
{
arr=new type[MAX];
}
//拷贝构造
Shu(const Shu&other):head(other.head),tail(other.tail)
{
other.arr=new type[MAX];
}
//析构函数
~Shu()
{
delete []arr;
arr=nullptr;
}
//判断空
int queue_empty();
//判断满
int queue_full();
//入队
int queue_push(type && data);
//遍历队
void queue_show();
//出队
int queue_pop();
//求队列的大小
int queue_size();
//清空队
void queue_free();
};
template <typename type>
//判断空
int Shu<type>::queue_empty()
{
if(NULL==arr)
{
cout<<"判空失败"<<endl;
return -1;
}
return this->head==this->tail;
}
template <typename type>
//判断满
int Shu<type>::queue_full()
{
if(NULL==arr)
{
cout<<"判满失败"<<endl;
return -1;
}
return (this->tail+1)%MAX==this->head;
}
template <typename type>
//入队
int Shu<type>::queue_push(type && data)
{
if(queue_full())
{
cout<<"入队失败"<<endl;
return -1;
}
//将输入的函数放入数组中
arr[this->tail]=data;
//尾下标自增
this->tail=(this->tail+1)%MAX;
cout<<"入队成功"<<endl;
return 0;
}
template <typename type>
//遍历队
void Shu<type>::queue_show()
{
if(queue_empty())
{
cout<<"遍历失败"<<endl;
return ;
}
//遍历
for(int i=this->head;i!=this->tail;i=(i+1)%MAX)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
template <typename type>
//出队
int Shu<type>::queue_pop()
{
if(queue_empty())
{
cout<<"出队失败"<<endl;
return -1;
}
//出队
cout<<arr[this->head]<<"出队成功"<<endl;
//自增
this->head=(this->tail+1)%MAX;
return 0;
}
template <typename type>
//求队列的大小
int Shu<type>::queue_size()
{
if(queue_empty())
{
cout<<"求队列大小失败"<<endl;
return -1;
}
//队列的大小
int size=(this->tail+MAX-this->head)%MAX;
cout<<"队列的大小为:"<<size<<endl;
return 0;
}
template <typename type>
//清空队
void Shu<type>::queue_free()
{
this->head=0;
this->tail=0;
cout<<"清空队列成功"<<endl;
}
int main()
{
Shu<int> duilie(0,0);
//入队
duilie.queue_push(2);
duilie.queue_push(5);
duilie.queue_push(3);
duilie.queue_push(8);
//遍历队
duilie.queue_show();
//出队
duilie.queue_pop();
duilie.queue_pop();
//求队列的大小
duilie.queue_size();
//清空队
duilie.queue_free();
return 0;
}
3、思维导图