#include <iostream>
using namespace std;
template <typename T>
class my_stack
{
protected:
T *per;
int MAXsize;
int top;
public:
my_stack ():per(new T[10]),MAXsize(10),top(-1){cout<<"无参构造初始化"<<endl;}
my_stack (int n):per(new T[n]),MAXsize(n),top(-1){cout<<"有参参构造初始化"<<endl;}
~my_stack(){cout<<"析构函数"<<endl;delete []per;}
my_stack(const my_stack &other):MAXsize(other.MAXsize),top(-1),per(new T(*(other.per))){cout<<"拷贝构造"<<endl;}
my_stack &operator=(const my_stack &other)
{
if(this!=&other)
{
MAXsize=(other.MAXsize);
top=-1;
per=new T(*(other.per));
}
cout<<"拷贝赋值函数"<<endl;
return *this;
}
T topnum()//获取栈顶元素
{
return per[top];
}
int input()//入栈
{
if(full()==0)
return 0;
cout<<"top="<<top<<endl;
T n;
cout<<"请输入:"<<endl;
cin>>n;
per[++top]=n;
return 0;
}
int output()//出栈
{
if(myvoid()==0)
return 0;
top--;
return 0;
}
void put()//遍历
{
for(int i=top;i>=0;i--)
{
cout<<per[i]<<" ";
}
cout<<endl;
}
int full()//判满
{
if(top==MAXsize-1)
{
return 0;
}
return 1;
}
int myvoid()//判空
{
if(top==-1) return 0;
return 1;
}
};
int main()
{
my_stack<int> s(10);
for(int i=0;i<11;i++)
{
s.input();
}
s.put();
s.output();
s.topnum();
s.put();
return 0;
}
#include <iostream>
using namespace std;
template <typename T>
class queue
{
protected:
T *data;
int MAXsize;
int head;
int rear;
public:
queue():data(new T(10)),MAXsize(10),head(0),rear(0)
{cout<<"无参构造"<<endl;}
queue(int n):data(new T(n)),MAXsize(n),head(0),rear(0)
{cout<<"有参构造"<<endl;}
~queue(){cout<<"析构函数"<<endl;delete []data;}
queue(const queue &other):data(new T(*(other.data))),MAXsize(other.MAXsize),head(0),rear(0)
{cout<<"拷贝构造"<<endl;}
queue &operator=(const queue &other)
{
cout<<"拷贝赋值"<<endl;
this->data=new T(*(other.data));
this->MAXsize=other.MAXsize;
this->head=0;
this->rear=0;
}
//入队
void input(T n)
{
if(head==(rear+1)%MAXsize) return;
data[rear]=n;
rear=(rear+1)%MAXsize;
}
//出队
void output()
{
if(head==rear) return ;
head=(head+1)%MAXsize;
}
//遍历
void put()
{
for(int i=head;i<rear;i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
//队列中元素个数
void num()
{
cout<<"队中元素个数:"<<(MAXsize+rear-head)%MAXsize<<endl;
}
};
int main()
{
queue <int>s(5);
for(int i=0;i<4;i++)
{
int e;
cout<<"请输入元素:"<<endl;
cin>>e;
s.input(e);
}
s.put();
s.num();
s.output();
s.num();
return 0;
}