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

坐牢第三十四天(c++)

一.作业

1.栈的手写

#include <iostream>
using namespace std;
// 封装一个栈
class stcak
{
private:int *data;    //int max_size; // 最大容量int top;      // 下标
public:// 无参构造函数stcak();// 有参构造函数stcak(int size);// 拷贝构造函数stcak(const stcak &other);// 析构函数~stcak();// 判空函数bool empty();// 判满函数bool full();// 扩容函数void resize(int new_size);// 返回元素个数函数int size();// 向栈顶插入元素函数void push(int value);// 删除栈顶元素函数int pop();// 访问栈顶元素函数int get_top();// 赋值重载函数stcak &operator=(const stcak &other);// 遍历栈里元素函数void show();
};
// 无参构造函数
stcak::stcak() : max_size(10)
{data = new int[10];max_size = 10;top = -1;cout << "无参构造" << endl;
}
// 有参构造函数
stcak::stcak(int size)
{data = new int[size];max_size = size;top = -1;cout << "有参构造" << endl;
}
// 拷贝构造函数
stcak::stcak(const stcak &other)
{max_size = other.max_size;top = other.top;data = new int[max_size];for (int i = 0; i <= top; i++){data[i] = other.data[i];}cout << "拷贝构造" << endl;
}
// 析构函数
stcak::~stcak()
{delete[] data;cout << "析构函数" << endl;
}
// 判空函数
bool stcak::empty()
{return top == -1;
}
// 判满函数
bool stcak::full()
{return top == max_size - 1;
}
// 扩容函数
void stcak::resize(int new_size)
{int *new_data = new int[new_size];for (int i = 0; i <= top; i++){new_data[i] = data[i];}delete[] data;data = new_data;max_size = new_size;
}
// 返回元素个数函数
int stcak::size()
{return top + 1;
}
// 向栈顶插入元素函数
void stcak::push(int value)
{if (full()){// 调用扩容函数resize(max_size * 2);}data[++top] = value;
}
// 删除栈顶元素函数
int stcak::pop()
{if (empty()){cout << "栈是空的";return -1;}return data[top--]; // 出栈
}
// 访问栈顶元素函数
int stcak::get_top()
{if (empty()){cout << "栈是空的";return -1;}return data[top]; // 出栈
}
// 赋值重载函数
stcak &stcak::operator=(const stcak &other)
{if (this == &other){return *this;}delete[] data;max_size = other.max_size;top = other.top;data = new int[max_size];for (int i = 0; i <= top; i++){data[i] = other.data[i];}return *this;
}
// 遍历栈里元素函数
void stcak::show()
{if (empty()){cout << "栈是空的";return;}cout << "栈里元素有:"<<endl;for (int i = 0; i <= top; i++){cout<< data[i] <<'\t'; }cout <<endl;
}
/******************主函数*********************/ 
int main()
{stcak s1(20);cout << s1.size() << endl;s1.push(1);s1.push(2);s1.show();cout << s1.size() << endl;stcak s2 = s1;return 0;
}

 效果图:

2.队列的手写

#include <iostream>
using namespace std;
class queue
{
private:int *data;    // 容器int max_size; // 最大容量int front;    // 头下标int tail;     // 尾下标
public:// 无参构造函数queue();// 有参构造函数queue(int size);// 拷贝构造函数queue(const queue &other);// 析构函数~queue();// 判空函数bool empty();// 判满函数bool full();// 扩容函数void resize(int new_size);// 元素个数函数int size();// 向队列尾部插入元素函数void push(int value);// 删除首个元素函数 出队int pop();// 遍历队列元素void show();// 赋值重载函数queue &operator=(const queue &other);
};
// 无参构造函数
queue::queue() : max_size(10)
{data = new int[10];max_size = 10;front = tail = -1;cout << "无参构造" << endl;
}
// 有参构造函数
queue::queue(int size)
{data = new int[size];max_size = size;front = tail = 0;cout << "有参构造" << endl;
}
// 拷贝构造函数
queue::queue(const queue &other)
{max_size=other.max_size;front=other.front;tail=other.tail;data=new int[max_size];for (int i = front; i != tail; i = (i + 1) % max_size){data[i]=other.data[i];}   cout << "拷贝构造" << endl;
}
// 析构函数
queue::~queue()
{delete[] data;cout << "析构函数" << endl;
}
// 判空函数
bool queue::empty()
{return front == tail;
}
// 判满函数
bool queue::full()
{return (tail + 1) % max_size == front;
}
// 元素个数函数
int queue::size()
{return (tail - front + max_size) % max_size;
}
// 扩容函数
void queue::resize(int new_size)
{int *new_data = new int[new_size];for (int i = front; i <= tail; i++){new_data[i] = data[i];}data = new_data;max_size = new_size;
}
// 向队列尾部插入元素函数
void queue::push(int value)
{if (full()){// 调用扩容函数resize(max_size * 2);}data[tail] = value;tail = (tail + 1) % max_size;
}
// 删除首个元素函数 出队
int queue::pop()
{if (empty()){cout << "队列为空" << endl;return -1;}cout << data[front] << "出队" << endl;front = (front + 1) % max_size;return 0;
}
// 遍历队列元素
void queue::show()
{if (empty()){cout << "队列为空" << endl;return;}cout << "队列元素:" << endl;for (int i = front; i != tail; i = (i + 1) % max_size){cout << data[i] << '\t';}cout << endl;
}
// 赋值重载函数
queue &queue::operator=(const queue &other)
{if (this == &other){return *this;}delete []data;max_size=other.max_size;front=other.front;tail=other.tail;data=new int[max_size];for (int i = front; i != tail; i = (i + 1) % max_size){data[i]=other.data[i];}cout << "拷贝赋值函数" <<endl;  return *this; 
}
/******************主函数*********************/
int main()
{queue s1(20);s1.push(1);s1.push(2);s1.show();// s1.pop();// s1.pop();// s1.show();queue s2=s1;queue s3;s3=s2;return 0;
}

 效果图:

二.思维导图

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • shell判断、if语句
  • 探索C++编程技巧:计算两个字符串的最长公共子串
  • 内网Exadata使用git的配置过程
  • 一、VSCode安装IDF5.3
  • 数据结构---->内核链表
  • 解决:使用Charles查看本机的ip地址
  • 数学建模常见模型(下)
  • 【HTTP、Web常用协议等等】前端八股文面试题
  • 【 WPF 中常用的Brush类的简要介绍、使用方法和适用场景】
  • 微服务面试题
  • 安卓逆向(之)真机root(红米手机)
  • 什么是Java中的模板方法模式?请给出示例。Java中的设计模式有哪些?请列举几个并解释其应用场景。
  • .net core 管理用户机密
  • 加密技术.
  • 编程式路由跳转
  • [NodeJS] 关于Buffer
  • Android路由框架AnnoRouter:使用Java接口来定义路由跳转
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • javascript 哈希表
  • LeetCode29.两数相除 JavaScript
  • LeetCode算法系列_0891_子序列宽度之和
  • magento2项目上线注意事项
  • MQ框架的比较
  • ReactNative开发常用的三方模块
  • Synchronized 关键字使用、底层原理、JDK1.6 之后的底层优化以及 和ReenTrantLock 的对比...
  • 来,膜拜下android roadmap,强大的执行力
  • 每个JavaScript开发人员应阅读的书【1】 - JavaScript: The Good Parts
  • 你不可错过的前端面试题(一)
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 一个SAP顾问在美国的这些年
  • 如何用纯 CSS 创作一个货车 loader
  • ​LeetCode解法汇总2304. 网格中的最小路径代价
  • #13 yum、编译安装与sed命令的使用
  • #Spring-boot高级
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (ZT) 理解系统底层的概念是多么重要(by趋势科技邹飞)
  • (备忘)Java Map 遍历
  • (第二周)效能测试
  • (翻译)Entity Framework技巧系列之七 - Tip 26 – 28
  • (十六)视图变换 正交投影 透视投影
  • (学习日记)2024.02.29:UCOSIII第二节
  • ***原理与防范
  • .NET Core WebAPI中使用Log4net 日志级别分类并记录到数据库
  • .NET Core WebAPI中使用swagger版本控制,添加注释
  • .NET 材料检测系统崩溃分析
  • .net 连接达梦数据库开发环境部署
  • .net 设置默认首页
  • .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)...
  • .NET企业级应用架构设计系列之开场白
  • .NET之C#编程:懒汉模式的终结,单例模式的正确打开方式
  • .sys文件乱码_python vscode输出乱码
  • /usr/bin/env: node: No such file or directory
  • :class的用法及应用
  • @Builder用法
  • @FeignClient注解,fallback和fallbackFactory