当前位置: 首页 > news >正文

day3_C++

day3_C++

  • 思维导图
  • 用C++的类完成数据结构 栈的相关操作
  • 用C++的类完成数据结构 循环队列的相关操作

思维导图

请添加图片描述

用C++的类完成数据结构 栈的相关操作

stack.h

#ifndef STACK_H
#define STACK_H#include <iostream>
#include <cstring>using namespace std;typedef int datatype;#define MAX 5class Stack
{
public:/*构造函数*/Stack();/*拷贝构造函数*/Stack(const Stack& others);/*析构函数*/~Stack();/*判满 true 满 */bool is_full();/*判满 true 空*/bool is_empty();/*入栈*/void in_stack(datatype e);/*出栈*/datatype out_stack();/*清空栈*/void clear_stack();/*求栈顶元素*/datatype get_stackTop_E();/*求栈的大小*/void get_stackSize();private:int top;datatype *data;
};#endif // STACK_H

stack.cpp

#include "stack.h"Stack::Stack():data(new int[MAX]),top(-1)
{memset(this->data,0,MAX);//在堆区申请max个int大小的空间cout<<"栈容器初始化成功"<<endl;
}Stack::Stack(const Stack& others):data(new int[MAX]),top(others.top)
{//深拷贝,将堆区内容也拷贝进来for(int i = 0;i<MAX-1;i++){this->data[i] = others.data[i];}cout<<"拷贝完成"<<endl;
}Stack::~Stack()
{//释放堆区数据delete []data;cout<<"析构完成"<<endl;
}bool Stack::is_full()
{if(this->top ==MAX-1)return true;elsereturn false;
}bool Stack::is_empty()
{if(this->top == -1)return true;elsereturn false;
}void Stack::in_stack(datatype e)
{if(this->is_full()==false){this->top++;this->data[this->top] = e;cout<<"入栈成功"<<endl;}else{cout<<"入栈失败,栈满"<<endl;}
}datatype Stack::out_stack()
{if(this->is_empty()==false){datatype temp = this->data[this->top];this->top--;return temp;}else{cout<<"出栈失败,栈空"<<endl;return NULL;}
}void Stack::clear_stack()
{if(this->is_empty()==false){this->top=-1;cout<<"清空成功"<<endl;}else{cout<<"栈空,无需清理"<<endl;}
}datatype Stack::get_stackTop_E()
{if(this->is_empty()==true)return NULL;return this->data[this->top];
}void Stack::get_stackSize(){cout<<"栈中有元素 "<<this->top+1<<"个"<<endl;
}

用C++的类完成数据结构 循环队列的相关操作

queue.h

#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
#include <cstring>using namespace std;typedef int datatype;
#define MAX 5class Queue
{public:/*构造函数*/Queue();/*拷贝构造函数*/Queue(const Queue& others);/*析构函数*/~Queue();/*判满 true 满 */bool is_full();/*判满 true 空*/bool is_empty();/*入队*/void in_queue(datatype e);/*出队*/datatype out_queue();/*清空队*/void clear_queue();/*求队的大小*/void get_queueSize();private:datatype *data;int font;int tail;
};#endif // QUEUE_H

queue.cpp

#include "queue.h"Queue::Queue():data(new int [MAX]),tail(0),font(0)
{memset(this->data,0,MAX);cout<<"循环队列初始化成功"<<endl;
}Queue::Queue(const Queue& others):data(new int[MAX]),font(others.font),tail(others.tail)
{//深拷贝int f = this->font;int t = this->tail;while ((f+MAX)%MAX==t) {this->data[f] = others.data[f];f++;}cout<<"拷贝完成"<<endl;
}bool Queue::is_full()
{if((this->tail+1)%MAX == this->font){return true;}return false;
}bool Queue::is_empty()
{if(this->font == this->tail){return true;}return false;
}Queue::~Queue()
{//释放堆区数据delete []data;cout<<"析构完成"<<endl;
}void Queue::in_queue(datatype e)
{if(this->is_full() == true){cout<<"队列满了"<<endl;return ;}this->data[this->tail] = e;this->tail =  (this->tail+1)%MAX;cout<<"入队成功"<<endl;
}
/*出队*/
datatype Queue::out_queue()
{if(this->is_empty() == true){cout<<"队列空,无元素"<<endl;return NULL;}int temp =  this->data[this->font];this->font = (this->font+1)%MAX;return temp;
}void Queue::clear_queue()
{if(this->is_empty() == true){cout<<"队列空,无元素"<<endl;return;}this->font = 0;this->tail = 0;
}void Queue::get_queueSize()
{cout<<"队列的大小是:" <<(this->tail-this->font+MAX)%MAX<<endl;
}
http://www.lryc.cn/news/161704.html

相关文章:

  • 力扣题解(73. 矩阵置零),带注释
  • SpringMVC应用
  • 百度输入法全面升级,打造首个基于大模型的输入法原生应用
  • 如何解决GitHub 访问不了?小白教程
  • 龙芯指令集LoongArch——学习笔记(1)
  • ubuntu 20.04 docker安装emqx 最新版本或指定版本
  • 软件测试/测试开发丨学会与 AI 对话,高效提升学习效率
  • CEF内核和高级爬虫知识
  • 视频集中存储/云存储/磁盘阵列EasyCVR平台分组批量绑定/取消设备功能详解
  • 科技成果鉴定测试报告一般包含哪些测试内容?
  • IDEA中的“Deployment“ 将项目直接部署到服务器上
  • 密室逃脱小游戏
  • 【MyBatis】MyBatis项目结构的搭建
  • Vant组件库入门知识
  • Java字符串查找
  • 2023年7月京东投影仪行业品牌销售排行榜(京东大数据)
  • 设计模式-01简单工厂模式详解 详细代码对比
  • IPD-PDT-POP角色的名称、定位和职责说明书
  • 在MySQL中查看数据库和表的数据大小
  • Android前端音视频数据接入GB28181平台意义
  • Ubuntu 20.04上docker安装Redis
  • linux 压缩webfile文件夹 webfile.tar.gz和webfile.tar的区别
  • 基于SSM的农产品推广应用网站
  • 人大金仓分析型数据库身份鉴别
  • 基于SpringBoot的在线教育平台系统
  • 基于大规模测量和多任务深度学习的电子鼻系统目标识别、浓度预测和状态判断
  • Unity游戏客户端进阶路线(只针对本人)
  • 【C++】封装map和set(红黑树实现)
  • 【补】代码随想录算法训练营day38|动态规划 |509. 斐波那契数|70. 爬楼梯|746. 使用最小花费爬楼梯
  • C语言sizeof()计算空间大小为8的问题