1.将之前定义的栈类和队列类都实现成模板类
栈:
#include <iostream>
#define MAX 8
using namespace std;
template<typename T>
class Stack
{
private:
T *data; //栈的数组,指向堆区空间,用于存储栈的容器
int top; //记录栈顶的变量
public:
Stack():top(-1),data(new T[MAX])
{
cout<<"Stack::无参构造函数:初始化栈成功"<<endl;
}
//析构函数
~Stack(){
cout<<"Stack::析构函数"<<endl;
delete []data;
data=nullptr;
}
//拷贝构造函数:深拷贝
Stack(const Stack &other):data(new T(*other.data)),top(other.top){
cout<<"Stack::拷贝构造函数"<<endl;
}
//入栈
bool stack_push(T e)
{
if(top<-1)
{
cout<<"所给栈不合法"<<endl;
return false;
}else if(stack_full())
{
cout<<"入栈失败,栈满"<<endl;
return false;
}
//先加后压
top++;
this->data[top]=e;
cout<<data[top]<<"入栈成功"<<endl;
return true;
}
//出栈
bool stack_pop()
{
if(top<-1)
{
cout<<"所给栈不合法"<<endl;
return false;
}
//出栈逻辑:先弹后减
cout<<data[top]<<"出栈成功"<<endl;
top--;
return true;
}
//清空栈
void stack_free()
{
if(top<-1)
{
cout<<"所给栈不合法"<<endl;
return;
}
for(top;top>-1;top--)
{
stack_pop();
}
top=-1;
cout<<"清空成功"<<endl;
}
//判空
bool stack_empty()
{
if(top==-1)
{
return true;
}else
{
if(top<-1)
{
cout<<"所给栈不合法"<<endl;
}
return false;
}
}
//判满
bool stack_full()
{
if(top<-1)
{
cout<<"所给栈不合法"<<endl;
return false;
}else if(top==MAX-1)
{
return true;
}else
{
return false;
}
}
//获取栈顶元素
T stack_top()
{
if(top<-1||stack_empty())
{
cout<<"获取失败"<<endl;
return NULL;
}
return data[top];
}
//求栈的大小
int stack_size()
{
if(!stack_empty())
{
return top+1;
}
cout<<"所给链表不合法"<<endl;
return -1;
}
//展示栈
void show()
{
if(stack_empty())
{
cout<<"空栈"<<endl;
return;
}
for (int i=0;i<=top;i++)
{
cout<<"data["<<i<<"]="<<this->data[i]<<" ";
}
cout<<endl;
}
};
int main()
{
Stack<int> s;
//入栈
for(int i=MAX;i>=0;i--)
{
s.stack_push(i);
}
if(s.stack_full())
{
cout<<"栈满"<<endl;
}
//出栈
for(int i=0;i<MAX;i++)
{
s.stack_pop();
}
s.stack_push(1);
s.stack_push(2);
s.stack_push(3);
s.stack_push(11);
s.stack_push(12);
s.stack_push(111);
s.show();
cout<<"栈顶元素为:"<<s.stack_top()<<endl;
s.stack_pop();
s.show();
cout<<"栈顶元素为:"<<s.stack_top()<<endl;
s.stack_free();
s.show();
s.stack_push(1);
s.show();
cout<<"栈顶元素为:"<<s.stack_top()<<endl;
s.stack_push(2);
s.stack_push(3);
s.show();
cout<<"s栈顶元素为:"<<s.stack_top()<<endl;
s.show();
cout<<"****************************"<<endl;
Stack<char> s1;
cout<<"&s2="<<&s1<<endl;
s1.stack_push('h');
s1.stack_push('e');
s1.stack_push('l');
cout<<"s1栈顶元素为:"<<s1.stack_top()<<endl;
s1.show();
cout<<"****************************"<<endl;
Stack<string> s2;
cout<<"&s2="<<&s2<<endl;
s2.stack_push("你好啊");
s2.stack_push("你好吗");
s2.stack_push("我很好");
cout<<"s2栈顶元素为:"<<s2.stack_top()<<endl;
s2.show();
cout<<"****************************"<<endl;
Stack<double> s3;
cout<<"&s3="<<&s3<<endl;
s3.stack_push(2.1);
s3.stack_push(3.14);
s3.stack_push(5.22);
cout<<"s3栈顶元素为:"<<s3.stack_top()<<endl;
s3.show();
cout<<"****************************"<<endl;
return 0;
}
循环队列:
#include <iostream>
using namespace std;
#define MAX 8
template<typename T>
class Queue
{
private:
T *data; //存放队列的数组,初始化时向堆区申请数组空间
int front; //队头位置,记录对头所在的元素下标
int tail; //队尾位置,记录最后一个元素的下一个下标的位置
public:
//无参构造函数
Queue():
data(new T[MAX]),front(0),tail(0)
{
cout<<"Stack::无参构造函数:初始化循环队列成功"<<endl;
}
//析构函数
~Queue()
{
cout<<"Stack::析构函数"<<endl;
delete []data;
data=nullptr;
}
//拷贝构造函数
Queue(const Queue &other):
data(new int(*other.data)),front(other.front),tail(other.tail)
{
cout<<"Stack::拷贝构造函数"<<endl;
}
//入队
bool Queue_push(T e)
{
if(Queue_full())
{
cout<<"入队失败\n"<<endl;
return false;
}
//将数据放在队尾所在地方
data[tail]=e;
cout<<"data["<<tail<<"]="<<e<<" 入队成功"<<endl;
//队尾后移
tail=(tail+1)%MAX;
return true;
}
//出队
bool Queue_pop()
{
if(Queue_empty())
{
cout<<"出队失败\n"<<endl;
return false;
}
cout<<"data["<<front<<"]="<<data[front]<<" 出队成功"<<endl;
//队头后移
front=(front+1)%MAX;
return true;
}
//清空队
void Queue_free()
{
// for(int i=front;i<tail;i++)
// {
// data[i]=NULL;
// }
front=tail=0;
cout<<"清空队列成功"<<endl;
}
//判空
bool Queue_empty()
{
if(front==tail)
{
return true;
}else
{
return false;
}
}
//判满
bool Queue_full()
{
if((tail+MAX)%MAX==front && !Queue_empty())
{
return true;
}else
{
return false;
}
}
//求队列的大小
int Queue_size()
{
return (tail+MAX-front)%MAX;
}
//展示元素
void show()
{
if(Queue_empty())
{
cout<<"空队"<<endl;
return;
}
for(int i=front;i<tail;i++)
{
cout<<"data["<<i<<"]="<<data[i]<<" ";
}
cout<<endl;
}
};
int main()
{
Queue<int> queue;
queue.Queue_push(1);
queue.Queue_push(2);
queue.Queue_push(3);
queue.Queue_push(8);
queue.Queue_push(9);
queue.Queue_push(10);
queue.Queue_push(11);
queue.Queue_push(12);
queue.Queue_push(13);
queue.Queue_pop();
queue.Queue_push(1);
queue.show();
queue.Queue_free();
queue.show();
queue.Queue_push(10);
queue.Queue_push(11);
queue.Queue_push(12);
cout<<"size of queue="<<queue.Queue_size()<<endl;
queue.show();
cout<<"*************************"<<endl;
Queue<int> q=queue;
cout<<"size of q="<<q.Queue_size()<<endl;
q.Queue_push(1);
cout<<"size of q="<<q.Queue_size()<<endl;
q.show();
cout<<"*************************"<<endl;
Queue<double> q2;
cout<<"size of q2="<<q2.Queue_size()<<endl;
q2.Queue_push(1.5);
q2.Queue_push(3.5);
q2.Queue_push(3.14);
cout<<"size of q2="<<q2.Queue_size()<<endl;
q2.show();
cout<<"*************************"<<endl;
cout<<"*************************"<<endl;
Queue<string> q3;
cout<<"size of q3="<<q3.Queue_size()<<endl;
q3.Queue_push("我最喜欢吃的水果是香蕉");
q3.Queue_push("小猫咪很可爱");
q3.Queue_push("爱看香蕉猫");
cout<<"size of q3="<<q3.Queue_size()<<endl;
q3.show();
cout<<"*************************"<<endl;
return 0;
}
运行结果:
栈:
循环队列:
2.思维导图