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

C++ 6

二、myStack

#ifndef MYSTACK_H #define MYSTACK_H #include <iostream> typedef int datatype; using namespace std; class myStack { private: datatype *data; size_t size; size_t capacity; public: myStack():size(0),capacity(5){ //无参构造 data = new datatype [5]; } myStack(size_t cap) //有参构造 { data = new datatype[cap]; capacity = cap; size = 0; } myStack(const myStack& other){ //拷贝构造 if(this != &other) { delete [] this->data; this->size = other.size; this->capacity = other.capacity; this->data = new datatype[this->capacity]; for(size_t i=0;i<this->size;i++) { this->data[i] = other.data[i]; } } } virtual ~myStack() //虚析构函数 { delete []data; } myStack& operator=(const myStack& other); //运算符重载 void expend(); //二倍扩容 datatype& top(); //访问栈顶元素 bool empty(); //检查是否为空 size_t get_size(); //获取元素个数 myStack& push(datatype element); //向栈顶插入元素 myStack& pop(); //删除栈顶元素 }; #endif // MYSTACK_H
#include"myStack.h" myStack& myStack::operator=(const myStack& other) { if (this != &other) { while (this->capacity < other.capacity) { this->expend(); } for (size_t i = 0; i < other.size; ++i) { this->data[i] = other.data[i]; } size = other.size; } return *this; } void myStack::expend() { datatype* Temp = new datatype [capacity*2]; for (size_t i = 0; i < capacity; ++i) { Temp[i] = data[i]; } delete [] data; data = Temp; capacity *= 2; } datatype& myStack::top() { return this->data[size-1]; } bool myStack::empty() { return (this->size == 0); } size_t myStack::get_size() { return this->size; } myStack& myStack::push(datatype element) { if( capacity== (size+1)){ //扩容 this->expend(); } this->data[size] = element; size++; return *this; } myStack& myStack::pop() { size--; return *this; }
#include"myStack.h" int main() { cout<<"*********有参构造,push1,2,3,4,5**************"<<endl; myStack stk1(2); stk1.push(1);stk1.push(2);stk1.push(3);stk1.push(4);stk1.push(5); cout<<"************* top ***********"<<endl; datatype element = stk1.top(); cout<<"top1 = "<<element<<endl; cout<<"*********拷贝构造,top**************"<<endl; myStack stk2(stk1); element = stk2.top(); cout<<"top2 = "<<element<<endl; cout<<"********* pop ***********"<<endl; stk2.pop(); element = stk2.top(); cout<<"top2 = "<<element<<endl; cout<<"****************************"<<endl; return 0; }

三、myQueue

#ifndef MYQUEUE_H #define MYQUEUE_H #ifndef MYSTACK_H #define MYSTACK_H #include <iostream> typedef int datatype; using namespace std; class myQueue { private: datatype *data; size_t frontIndex; size_t backIndex; size_t capacity; public: myQueue():frontIndex(0),backIndex(0),capacity(5){ //无参构造 data = new datatype [5]; } myQueue(size_t cap):frontIndex(0),backIndex(0) //有参构造 //有参构造 { data = new datatype[cap]; capacity = cap; } myQueue(const myQueue& other){ //拷贝构造 if(this != &other) { capacity = other.capacity; data = new datatype[capacity]; frontIndex = other.frontIndex; backIndex = other.backIndex; memcpy(data, other.data, capacity * sizeof(datatype)); } } virtual ~myQueue() //虚析构函数 { delete []data; } myQueue& operator=(const myQueue other); //运算符重载 void expend(); //二倍扩容 datatype& front(); //访问队首元素 datatype& back(); //访问队尾元素 bool empty(); //检查是否为空 size_t get_size(); //获取元素个数 myQueue& push(datatype element); //向栈顶插入元素 myQueue& pop(); //删除栈顶元素 }; #endif // MYSTACK_H #endif // MYQUEUE_H
#include"myQueue.h" myQueue& myQueue::operator=(const myQueue other) { // 检查自赋值 if (this == &other) { return *this; } // 释放原有内存 delete[] data; // 分配新内存并复制数据 capacity = other.capacity; frontIndex = other.frontIndex; backIndex = other.backIndex; data = new datatype[capacity]; memcpy(data, other.data, capacity * sizeof(datatype)); return *this; } void myQueue::expend() { size_t newCapacity = capacity * 2; // 分配新内存并复制原有数据 datatype* newData = new datatype[newCapacity]; memcpy(newData, data, capacity * sizeof(datatype)); // 释放原有内存并将指针指向新内存 delete[] data; data = newData; // 更新容量 capacity = newCapacity; } datatype& myQueue::front() { return data[frontIndex]; } datatype& myQueue::back() { return data[backIndex-1]; } bool myQueue::empty() { return (frontIndex == backIndex); } size_t myQueue::get_size() { return (backIndex - frontIndex); } myQueue& myQueue::push(datatype element) { if(capacity == (backIndex+1)) { //扩容 this->expend(); } this->data[backIndex] = element; this->backIndex++; return *this; } myQueue& myQueue::pop() { if (empty()) { throw std::runtime_error("Queue is empty"); } frontIndex++; return *this; }
#include "myQueue.h" using namespace std; int main() { cout << "*********无参构造,push1,2,3,4,5**************" << endl; myQueue q1; q1.push(1).push(2).push(3).push(4).push(5); cout << "************* front ***********" << endl; datatype element = q1.front(); cout << "front1 = " << element << endl; cout << "*********有参构造,back**************" << endl; myQueue q2(10); q2.push(6).push(7).push(8).push(9).push(10); element = q2.back(); cout << "back2 = " << element << endl; cout << "*********拷贝构造,pop**************" << endl; myQueue q3(q2); q3.pop(); element = q3.front(); cout << "front3 = " << element << endl; cout << "****************************" << endl; return 0; }

http://www.jsqmd.com/news/889568/

相关文章:

  • 3分钟掌握B站无水印视频下载:BiliDownload实用指南
  • 如何专业解锁WeMod高级功能:Wand-Enhancer开源工具实用指南
  • 眼周卡粉细纹用什么?CA眼油 快速吸收12天淡纹服帖 - 全网最美
  • 2026杭州书法艺考机构推荐|紫铜书院:统考断层领先、校考强势突围,录取率行业标杆 - 奔跑123
  • Hindsight记忆过滤:基于时间、类型和标签的精确过滤指南 [特殊字符]
  • WindowResizer终极指南:如何强制调整Windows窗口大小的完整教程
  • Unity TMP Button文字修改的正确姿势与常见坑
  • ROFLPlayer:英雄联盟回放文件分析终极指南
  • 泉州客多旧货回收:永春餐饮设备回收哪家好 - LYL仔仔
  • NCMppGui:5分钟极速解锁加密音乐文件的完整免费方案
  • Agent赋能智能运维:如何实现AI自动监控服务器并触发故障工单的闭环架构?
  • 三亚安易捷建筑装饰工程:三亚商铺拆除公司 - LYL仔仔
  • PMP项目进度网络图实战——第1篇:甘特图与PERT的融合应用
  • 为什么BAAI/bge-small-zh-v1.5在C-MTEB基准测试中表现卓越?深度技术解析
  • Git prune深度解析:不可达对象清理原理与安全实践
  • NoFences:Windows桌面分区神器,让你的工作效率提升300%
  • Ark-Pets明日方舟桌宠:打造智能生动的桌面互动伙伴终极指南
  • 高性价比护发素榜:学生党必看的平价好物 - 速递信息
  • [MAF预定义的IChatClient中间件-01]LoggingChatClient——在LLM调用前后输出日志
  • 番茄小说下载器:5分钟打造你的个人数字图书馆,实现真正的阅读自由
  • Beyond Compare 5密钥生成器:从评估到期到永久授权的技术解密方案
  • 3种高效保存完整网页的终极方案:SingleFile工具完全指南
  • Windows Cleaner架构解析:基于Python的现代化Windows系统优化工具
  • 汕头市贵金属全品类回收同城靠谱回收门店权威:黄金+白银+铂金+钯金当场检测当面结算及联系方式推荐 - 亦辰小黄鸭
  • 温州黄金回收怎么选?福正美免费上门透明报价 - 上门黄金回收
  • 发膜功效对比:2026年修复力最强的5款 - 速递信息
  • OpenOOD开放集识别:3种方法如何应对未知类别识别挑战
  • MusicFree插件终极指南:如何打造你的专属音乐宇宙
  • 深圳昆仑腕表保养收费全公开:金桥线性机芯异响、海军上将杯自动陀螺丝松动怎么修?资深技师为你拆解工时费与原厂配件更换账单,守护你的独立制表品牌 “腕间艺术品” - 亨得利官方维修中心
  • 石家庄黄金回收哪家强?福正美免费上门堪称满分首选 - 上门黄金回收