#include <iostream>using namespace std;
template <typename T>
class stu
{
private:T num[20];T head;T low;
public:stu(){}stu(T h,T l):head(h),low(l){cout<<"有参构造"<<endl;}~stu(){cout<<"析构函数"<<endl;}stu(const stu &other):head(other.head),low(other.low){cout<<"拷贝构造函数"<<endl;}//判空bool queue_empty();//判满bool queue_full();//入队void queue_push(int n);//出队void queue_pop();//清空void queue_null();//队列长度void queue_size();
};
//判空
template <typename T>
bool stu<T>::queue_empty()
{if(this->low==this->head){return 1;}return 0;
}
//判满
template <typename T>
bool stu<T>::queue_full()
{if((this->low+1)%20==head){return 1;}return 0;
}
//入队
template <typename T>
void stu<T>::queue_push(int n)
{if(queue_full()){cout<<"队列已满"<<endl;}this->num[this->low]=n;this->low=(this->low+1)%20;cout<<"入队成功"<<endl;
}
//出队
template <typename T>
void stu<T>::queue_pop()
{if(queue_empty()){cout<<"出队失败"<<endl;}cout<<"出队成功"<<this->num[this->head]<<endl;this->head=(this->head+1)%20;
}
//清空
template <typename T>
void stu<T>::queue_null()
{while(1){cout<<this->num[this->head]<<endl;this->head=(this->head+1)%20;if(this->head==this->low){cout<<"清空成功"<<endl;break;}}
}
//队列长度
template <typename T>
void stu<T>::queue_size()
{cout<<"队列长度"<<(this->low-this->head+20)%20<<endl;
}
int main()
{stu<int> p1(0,0);p1.queue_push(5);p1.queue_push(8);p1.queue_push(10);p1.queue_pop();p1.queue_size();p1.queue_null();p1.queue_size();return 0;
}
#include <iostream>using namespace std;
template<typename T>
class stu
{
private:T num[20];T top;
public:stu(){}//构造stu(int n,int t){num[top]=n;top=t;cout<<"stu::构造函数"<<endl;}//析构~stu(){cout<<"stu::析构函数"<<this<<endl;}//拷贝stu(const stu &other):num(other.num),top(other.top){cout<<"拷贝构造函数"<<endl;}//判空bool stack_empty();//判满bool stack_full();//入栈void stack_push(int n);//出栈void stack_pop();//遍历栈void stack_show();//清空栈void stack_null();//获取栈顶元素void stack_top();//求栈的大小void stack_num();
};
//判空
template <typename T>
bool stu<T>::stack_empty()
{if(-1==top){return 1;}return 0;
}//判满
template <typename T>
bool stu<T>::stack_full()
{if(19==top){return 1;}return 0;
}//入栈
template <typename T>
void stu<T>::stack_push(int n)
{this->top++;this->num[this->top]=n;cout<<"入栈成功"<<endl;
}//出栈
template <typename T>
void stu<T>:: stack_pop()
{if(stack_empty()){cout<<"出栈失败"<<endl;}int n = this->num[this->top];cout<<"出栈成功:"<<n<<endl;this->top--;
}//遍历栈
template <typename T>
void stu<T>:: stack_show()
{if(stack_empty()){cout<<"遍历失败"<<endl;}cout<<"从栈顶到栈底分别为"<<endl;for(int i=this->top;i>=0;i--){cout<<this->num[i]<<" ";}cout<<endl;
}//清空栈
template <typename T>
void stu<T>:: stack_null()
{if(stack_empty()){cout<<"清空失败"<<endl;}for(int i=this->top;i>=0;i--){this->num[i]=NULL;}cout<<"清空成功"<<endl;
}//获取栈顶元素
template <typename T>
void stu<T>:: stack_top()
{cout<<"栈顶元素"<<this->num[this->top]<<endl;
}//求栈的大小
template <typename T>
void stu<T>:: stack_num()
{cout<<"栈的大小:"<<this->top+1<<endl;
}int main()
{stu<int> s1(0,-1);s1.stack_push(6);s1.stack_push(8);s1.stack_push(9);s1.stack_show();s1.stack_top();s1.stack_num();s1.stack_null();s1.stack_show();return 0;
}