目录
1. 自行封装一个栈的类,包含私有成员属性:栈的数组、记录栈顶的变量
成员函数完成:构造函数、析构函数、拷贝构造函数、入栈、出栈、清空栈、判空、判满、获取栈顶元素、求栈的大小
2.自行封装一个循环顺序队列的类,包含私有成员属性:存放队列的数组、队头位置、队尾位置
成员函数完成:构造函数、析构函数、拷贝构造函数、入队、出队、清空队列、判空、判满、求队列大小
- #include
-
- using namespace std;
-
- class Sta
- {
- private:
- int arr[128];
- int len=-1;
- public:
- // 显性定义无参构造函数
- Sta();
-
- //析构函数
- ~Sta();
-
- //拷贝构造函数
- Sta(const Sta &other);
-
- //判空
- bool Sta_empty();
-
- //判满
- bool Sta_full();
-
- //入栈
- void Sta_insert();
-
- //出栈
- void Sta_pop();
-
- //清空栈
- void Sta_outall();
-
- //获取栈顶元素
- int Sta_topdata();
-
- //求栈的大小
- int Sta_size();
- };
-
- // 显性定义无参构造函数
- Sta::Sta()
- {
- cout<<"Sta::无参构造"<
- }
-
- //析构函数
- Sta::~Sta()
- {
- cout<<"Sta::析构函数"<
- }
-
- //拷贝构造函数
- Sta::Sta(const Sta &other)
- {
- this->len=other.len;
- cout<<"Sta::拷贝构造函数"<
- }
-
- //判空
- bool Sta::Sta_empty()
- {
- if(-1==len)
- {
- return true;
- }
- return false;
- }
-
- //判满
- bool Sta::Sta_full()
- {
- if(127==len)
- {
- return true;
- }
- return false;
- }
-
- //入栈
- void Sta::Sta_insert()
- {
- if(Sta_full())
- {
- cout<<"栈满了,入栈失败"<
- return ;
- }
- int i;
- cout<<"请输入要入栈的数据:";
- cin>>i;
- arr[len+1]=i;
- len++;
- cout<<"入栈成功"<
- return ;
- }
-
- //出栈
- void Sta::Sta_pop()
- {
- if(Sta_empty())
- {
- cout<<"已经是空栈了,不能出栈了"<
- return ;
- }
- int i=0;
- i=arr[len];
- len--;
- cout<"出栈成功"<
- return ;
- }
-
- //清空栈
- void Sta::Sta_outall()
- {
- if(Sta_empty())
- {
- cout<<"已经是空栈了,不需要清空栈"<
- return ;
- }
- int temp=0;
- for(;len>=0;len--)
- {
- temp=arr[len];
- cout<
"出栈成功"< - }
- cout<<"清空栈完成"<
- return ;
- }
-
- //获取栈顶元素
- int Sta::Sta_topdata()
- {
- if(Sta_empty())
- {
- cout<<"空栈,获取个der"<
- return -1;
- }
- return arr[len];
- }
-
- //求栈的大小
- int Sta::Sta_size()
- {
- return len+1;
- }
-
-
- int main()
- {
- Sta six;
- six.Sta_insert();
- six.Sta_insert();
- six.Sta_insert();
- six.Sta_insert();
- six.Sta_insert();
- six.Sta_pop();
- int i=0;
- i=six.Sta_topdata();
- cout<<"栈顶元素:"<
- int x=0;
- x=six.Sta_size();
- cout<<"栈的大小为:"<
- six.Sta_outall();
-
- return 0;
- }
-
-
-
-
-

2.自行封装一个循环顺序队列的类,包含私有成员属性:存放队列的数组、队头位置、队尾位置
成员函数完成:构造函数、析构函数、拷贝构造函数、入队、出队、清空队列、判空、判满、求队列大小
- #include
-
- using namespace std;
-
- class Que
- {
- private:
- int arr[128];
- int front;
- int tail;
- public:
- // 显性定义有参构造函数
- Que(int front,int tail);
-
- //析构函数
- ~Que();
-
- //拷贝构造函数
- Que(const Que &other);
-
- //判空
- bool Que_empty();
-
- //判满
- bool Que_full();
-
- //入队
- void Que_insert();
-
- //出队
- void Que_pop();
-
- //清空队
- void Que_outall();
-
- //求队的大小
- int Que_size();
- };
-
- // 显性定义无参构造函数
- Que::Que(int front,int tail):front(front),tail(tail)
- {
- cout<<"Que::有参构造"<
- }
-
- //析构函数
- Que::~Que()
- {
- cout<<"Que::析构函数"<
- }
-
- //拷贝构造函数
- Que::Que(const Que &other)
- {
- this->front=other.front;
- this->tail=other.tail;
- cout<<"Que::拷贝构造函数"<
- }
-
- //判空
- bool Que::Que_empty()
- {
- if(front==tail)
- {
- return true;
- }
- return false;
- }
-
- //判满
- bool Que::Que_full()
- {
- if((tail+1)%128==front)
- {
- return true;
- }
- return false;
- }
-
- //入队
- void Que::Que_insert()
- {
- if(Que_full())
- {
- cout<<"队满了,入队失败"<
- return ;
- }
- int i;
- cout<<"请输入要入队的数据:";
- cin>>i;
- arr[tail]=i;
- tail=(tail+1)%128;
- cout<<"入队成功"<
- return ;
- }
-
- //出队
- void Que::Que_pop()
- {
- if(Que_empty())
- {
- cout<<"已经是空队了,不能出队了"<
- return ;
- }
- int i=0;
- i=arr[front];
- front=(front+1)%128;
- cout<"出队成功"<
- return ;
- }
-
- //清空队
- void Que::Que_outall()
- {
- if(Que_empty())
- {
- cout<<"已经是空队了,不需要清空队"<
- return ;
- }
- cout<<"开始清空队列"<
- int temp=0;
- for(;front!=tail;front=(front+1)%128)
- {
- temp=arr[front];
- cout<
"出队成功"< - }
- cout<<"清空队完成"<
- return ;
- }
-
-
- //求队的大小
- int Que::Que_size()
- {
- return (tail+128-front)%128;
- }
-
-
- int main()
- {
- //有参 构造
- Que six(0,0);
-
- six.Que_insert();
- six.Que_insert();
- six.Que_insert();
- six.Que_insert();
- six.Que_insert();
- six.Que_pop();
-
- int x=0;
- x=six.Que_size();
- cout<<"队的大小为:"<
- six.Que_outall();
-
- return 0;
- }
-
-

-
相关阅读:
Unity中的三种渲染路径
扩散模型加持下,机器人模型DALL-E-Bot可以轻松完成自主重新排列任务
驱动开发:内核封装WSK网络通信接口
[T3N4CI0US 2022] 一个韩国比赛
java基于springboot医院预约挂号管理系统附代码段
2.5 Go语言中的切片(Slice)
FreeRTOS入门教程(队列详细使用示例)
Gnostice PDFToolkit自动图文集的页码
@Lazy注解的原理
解题-->在线OJ(十九)
-
原文地址:https://blog.csdn.net/weixin_58469613/article/details/132773839