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

-
相关阅读:
机器学习实战——训练模型
rust容器
日常随笔——linux 更换cmake 版本
C++提高篇——STL(上)
Redis数据库
Python 的基本数据类型
高并发下的网络 IO 模型设计
Ruby on Rails 实践:更换 aloe 首页
具有部分单调性的区间个数计数问题——考虑分治:GZOI2023Day1T3
js中减少if-else的几个小技巧
-
原文地址:https://blog.csdn.net/weixin_69452640/article/details/132863136