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

server/2024/9/23 10:39:42/

一.作业

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;
}

 效果图:

二.思维导图


http://www.ppmy.cn/server/113147.html

相关文章

PostgreSQL 中的 `generate_series` 函数使用

1. 概述 在 PostgreSQL 中&#xff0c;generate_series 是一个非常实用的内置函数&#xff0c;它能够根据给定的起始值和结束值生成一系列连续的数字。这一功能对于需要生成大量连续数据或进行批量操作的场景非常有用。本文将详细介绍 generate_series 函数的基本用法&#xf…

Trollspeed网速悬浮窗,精简且强大

我很喜欢iOS的界面UI&#xff0c;它的设计哲学是以人为本&#xff0c;简约而不简单。不过有时候&#xff0c;我也会觉得iOS简约过头了。人类是很没安全感的动物&#xff0c;获取的信息量足够多&#xff0c;我们才会感觉到安全。 在iOS16中&#xff0c;苹果加入了电量百分比显示…

单片机通过串口向电脑发送字符串

1.TTL电平 TTL 是 Transistor-Transistor Logic &#xff0c;即晶体管 - 晶体管逻辑的简称&#xff0c;它是计算机处理器控制的设备 内部各部分之间通信的标准技术。 TTL 电平信号应用广泛&#xff0c;是因为其数据表示采用二进制规定&#xff0c; 5V 等价于逻辑 ”1” &…

Unity 热更 之 【YooAsset 热更】Unity 可以进行热更的资源管理系统,并 【Android 端简单实现·案例热更】

Unity 热更 之 【YooAsset 热更】Unity 可以进行热更的资源管理系统&#xff0c;并 【Android 端简单实现案例热更】 目录 Unity 热更 之 【YooAsset 热更】Unity 可以进行热更的资源管理系统&#xff0c;并 【Android 端简单实现案例热更】 一、简单介绍 二、YooAsset 引…

KRTS网络模块:UDP通信

KRTS网络模块:UDP通信 目录 KRTS网络模块:UDP通信UDP简介KRST UDP简介核心特性界面设计 核心代码运行实例稳定性测试 UDP简介 UDP&#xff08;User Datagram Protocol&#xff0c;用户数据报协议&#xff09;是一种无连接的传输层协议&#xff0c;它位于OSI七层模型中的传输层…

LTE PSS主同步信号搜索 MATLAB实现

本期带来PSS相关检测说明和MATLAB实现&#xff0c;本期只讲相关方面的&#xff0c;所以MATLAB实现也是相关的部分&#xff0c;频偏估计方面的待下期开讲。 LTE 4G PSS搜索分为TDD搜索和FDD搜索&#xff0c;但是对于 TDD 和 FDD 而言&#xff0c;PSS同步信号的结构是完全一样的&…

【前端面试】leetcode指针解法javascript

最大盛水 https://leetcode.cn/problems/container-with-most-water/ var maxArea = function(height) {// 左右指针靠拢let left = 0;let right = height.length-1;let maxArea = 0; while(left<right){// 计算出 当前的容积 与最大容积比较,取出最大的const currentAre…

数据结构————栈、队列

系统栈与数据结构中的栈 数据结构的栈系统栈定义与性质特殊的线性表&#xff0c;后进先出操作系统空间中的一块区域&#xff0c;用于保存中断现场、调用参数等操作入栈&#xff08;push&#xff09;、出栈&#xff08;pop&#xff09;、查看栈顶&#xff08;top&#xff09;由…