C++ 栈和队列的简单封装(9.3)

2024-09-04 07:12
文章标签 简单 c++ 封装 队列 9.3

本文主要是介绍C++ 栈和队列的简单封装(9.3),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.栈的封装

代码

#include <iostream>using namespace std;typedef int datatype;
class Stack
{
private:datatype *data;int max_size;       //栈的大小int the_top;        //栈顶
public:Stack(){data= new int[50];max_size = 50;the_top = -1;}Stack(int a){data= new int[a];max_size = a;the_top = -1;}~Stack(){delete[] data;}//访问栈顶元素datatype &top(){return *(data+the_top);}//判空bool empty(){return the_top==-1;}//求栈的大小int size(){return the_top+1;}//插入元素void push(datatype e){if(the_top==max_size-1){cout<<"栈满,入栈失败"<<endl;}else{data[++the_top]=e;}}//删除元素void pop(){if(the_top==-1){cout<<"删除失败"<<endl;}else{the_top--;}}//赋值运算符Stack &operator=(const Stack& other){//自赋值检查if(this==&other){return *this;}//释放旧数据delete [] data;data = nullptr;the_top = -1;//分配新数据max_size = other.max_size;data = new datatype[max_size];//复制数据the_top = other.the_top;for (int i = 0; i <= the_top; ++i){data[i] = other.data[i];}// 返回当前对象的引用return *this;}void show()const{if(the_top==-1){cout << "栈为空" << endl;return;}else{cout << "栈内容(从栈底到栈顶): ";for (int i = the_top; i >= 0; --i){cout << data[i] << " ";}cout << endl;}}};int main()
{//插入演示Stack s1(10);s1.push(1);s1.push(2);s1.push(3);s1.show();cout<<"******************"<<endl;//访问栈顶元素演示cout<<s1.top()<<endl;cout<<"******************"<<endl;//求栈的大小演示cout<<s1.size()<<endl;cout<<"******************"<<endl;//删除演示s1.pop();s1.show()
;    cout<<"******************"<<endl;//赋值=演示Stack s2(5);s2=s1;s2.show();cout<<"******************"<<endl;return 0;
}

 运行结果:

 

 

2.队列的封装

 代码:

#include <iostream>using namespace std;typedef int datatype; // 队列中存储的数据类型class Queue
{
private:datatype* data;int front;          // 队首元素的索引int rear;           // 队尾元素的下一个位置的索引int max_size;       // 队列的最大容量int c_size;         // 队列当前的大小
public://有参构造Queue(int n):front(0),rear(0),max_size(n),c_size(0){data = new datatype[max_size];}//析构函数~Queue(){delete [] data;}//访问第一个元素 my_frontdatatype my_front(){if(empty()){cout<<"队列为空"<<endl;return 0;}else{return data[front];}}//访问最后一个元素 backdatatype back(){if(empty()){cout<<"队列为空"<<endl;return 0;}else{return data[(rear - 1 + max_size) % max_size]; // 循环队列处理}}//判空 emptybool empty(){return c_size==0;}//返回容纳的元素数 sizeint size()const{return c_size;}//队尾插入元素 pushvoid push(datatype e){if(c_size == max_size)  //判满{cout<<"队列已满"<<endl;}else{data[rear] = e;rear = (rear + 1) % max_size; // 循环队列处理++c_size;}}//删除首个元素 popvoid pop(){if(empty()){cout<<"队列为空"<<endl;}else{front = (front + 1) % max_size; // 循环队列处理--c_size;}}//赋值 operator=Queue& operator=(const Queue& other){if (this != &other){delete[] data; // 删除旧数据max_size = other.max_size;data = new datatype[max_size];// 复制数据c_size = other.c_size;front = other.front;rear = other.rear;for (int i = 0; i < c_size; ++i){data[(front + i) % max_size] = other.data[(other.front + i) % other.max_size];}}return *this;}//展示函数void show()const{if(c_size==0){cout<<"队列为空"<<endl;return;}else{cout << "队列内容: ";for (int i = 0; i < c_size; i++){cout<<data[(front +i)% max_size]<<" ";}cout << endl;}}
};int main()
{Queue q1(5);//插入演示q1.push(1);q1.push(2);q1.push(3);q1.show();cout<<"******************"<<endl;//访问第一个元素cout<<q1.my_front()<<endl;cout<<"******************"<<endl;//访问最后一个元素cout<<q1.back()<<endl;cout<<"******************"<<endl;//返回size大小演示cout<<q1.size()<<endl;cout<<"******************"<<endl;//删除演示q1.pop();q1.show();cout<<"******************"<<endl;//赋值=演示Queue q2(6);q2=q1;q2.show();cout<<"******************"<<endl;return 0;
}

运行结果:

 

 

 

3.继承  思维导图

这篇关于C++ 栈和队列的简单封装(9.3)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1135351

相关文章

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基

python连接sqlite3简单用法完整例子

《python连接sqlite3简单用法完整例子》SQLite3是一个内置的Python模块,可以通过Python的标准库轻松地使用,无需进行额外安装和配置,:本文主要介绍python连接sqli... 目录1. 连接到数据库2. 创建游标对象3. 创建表4. 插入数据5. 查询数据6. 更新数据7. 删除

Jenkins的安装与简单配置过程

《Jenkins的安装与简单配置过程》本文简述Jenkins在CentOS7.3上安装流程,包括Java环境配置、RPM包安装、修改JENKINS_HOME路径及权限、启动服务、插件安装与系统管理设置... 目录www.chinasem.cnJenkins安装访问并配置JenkinsJenkins配置邮件通知