1.思维导图
2.将之前实现的顺序栈、循环顺序队列,定义成模板类
顺序栈
#include <iostream>
using namespace std;
template<typename T>
class Stack
{
private:
T *s=nullptr;
//栈顶
int top;
int len;
public:
//有参构造
Stack(int size):s(new T[size]), top(-1)
{
len=size;
}
//拷贝赋值
Stack &operator=(const Stack& other)
{
if(len>other.top)
{
for(int i=0;i<other.len;i++)
{
*(s+i)=*(other.s+i);
top++;
}
}
return *this;
}
//析构函数
~Stack()
{
delete []s;
s=nullptr;
}
//判空函数
bool is_empty( )
{
if(top==-1)
{
return true;
}
return false;
}
//判满函数
bool is_full( )
{
if(top==len-1)
{
return true;
}
return false;
}
//入栈函数
void push_stack(T e)
{
//判断是否存在,是否满
if(s==nullptr||is_full())
{
return;
}
//入栈
s[++top]=e;
return;
}
//出栈函数
T pop_stack( )
{
//判断是否存在,是否空
if(s==nullptr||is_empty())
{
T a;
return a;
}
//出栈
T a=s[top--];
return a;
}
//遍历函数
void o_stack( )
{
if(s==nullptr||is_empty())
{
return;
}
int i=0;
while(i<=top)
{
cout<<s[i++]<<"\t";
}
cout<<endl;
return;
}
//返回容纳的元素数
int size()
{
return top+1;
}
//获取栈顶元素的引用
T &get_quote( )
{
return s[top];
}
};
int main()
{
double e;
int size;
cout<<"请输入栈的大小 ";
cin>>size;
//实例化栈
Stack<double> s1(size);
//入栈
while(size--)
{
cout<<"请输入入栈元素";
cin>>e;
s1.push_stack(e);
}
cout<<"栈中元素为: "<<endl;
//遍历
s1.o_stack();
Stack<double> s2(4);
s2=s1;
s2.o_stack();
//出栈
s1.pop_stack();
cout<<"出栈一个元素后"<<endl;
s1.o_stack();
double &ref=s1.get_quote();
cout<<"栈顶元素为 "<<ref<<endl;
cout<<"栈中元素数量 "<<s1.size()<<endl;
return 0;
}
循环顺序队列
#include <iostream>
using namespace std;
template<typename T>
class Queue
{
private:
T *q=nullptr;
//队头
int front;
//队尾
int rear;
//队列大小
int len;
public:
//有参构造
Queue(int size):q(new T[size]) , front(0),rear(0) ,len(size) {}
//拷贝赋值
Queue &operator=(const Queue &other)
{
//赋值考虑容量
if(len>=(other.len-other.front+other.rear)%other.len)
{
//队头队尾位置判断
if(other.front<=len&&other.rear<=len)
{
for(int i=other.front;i!=other.rear;i=(i+1)%other.len)
{
front=other.front;
rear=other.rear;
*(q+i)=*(other.q+i);
}
}
}
else
{
cout<<"拷贝赋值失败!"<<endl;
}
return *this;
}
//析构
~Queue()
{
delete []q;
q=nullptr;
}
//判空
bool is_empty()
{
if(rear==front)
{
return true;
}
return false;
}
//判满
bool is_full()
{
if((rear+1)%len==front)
{
return true;
}
return false;
}
//入队
void en_queue(T e)
{
if(q==nullptr||is_full())
{
cout<<"入队失败!"<<endl;
return;
}
q[rear]=e;
rear=(rear+1)%len;
return;
}
//出队
T qu_queue()
{
if(q==nullptr||is_empty())
{
cout<<"出队失败!"<<endl;
T a;
return a;
}
T a=q[front];
front=(front+1)%len;
return a;
}
//front访问第一个元素
T front_queue()
{
if(q==nullptr||is_empty())
{
cout<<"访问第一个元素失败"<<endl;
T a;
return a;
}
return q[front];
}
//back访问最后一个元素
T back_queue()
{
if(q==nullptr||is_empty())
{
cout<<"访问最后一个元素失败"<<endl;
T a;
return a;
}
return q[rear-1];
}
//遍历
void ou_queue()
{
if(q==nullptr||is_empty())
{
return;
}
int i=front;
while(i!=rear)
{
cout<<q[i]<<"\t";
i=(i+1)%len;
}
cout<<endl;
return;
}
//计算队列元素个数
int num_queue()
{
return (len-front+rear)%len;
}
};
int main()
{
int e;
int len;
cout<<"请输入队列的大小 ";
cin>>len;
//实例化队列
Queue<int> q1(len);
len-=1;
//入队
while(len--)
{
cout<<"请输入入队元素";
cin>>e;
q1.en_queue(e);
}
cout<<"队列为: "<<endl;
//遍历
q1.ou_queue();
Queue<int> q2(9);
q2=q1;
//遍历
q2.ou_queue();
//出队
q1.qu_queue();
cout<<"出队一个元素后"<<endl;
q1.ou_queue();
//计算元素个数
cout<<"队列剩余元素为 "<<endl;
q1.ou_queue();
//队头
cout<<"队头 "<<q1.front_queue()<<endl;
//队尾
cout<<"队尾 "<<q1.back_queue()<<endl;
}