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

一.作业:

使用C++手动封装一个顺序表,包含成员数组一个,成员变量N个

复制代码
#include <iostream>
#include <cstring> // 引入cstring以使用memcpy
using namespace std;
// 类型重命名
using datatype = int; // typedef int datatype;
struct SeqList
{
private:
    // datatype data[MAX] = {0};                //顺序表的数组
    datatype *data; // 顺序表的数组
    int size = 0;   // 数组的大小
    int len = 0;    // 顺序表实际长度
public:
    // 初始化函数
    void init(int s);
    // 要实现的函数
    // 判空函数
    bool empty();
    // 判满函数
    bool full();
    // 添加数据函数
    bool add(datatype e);
    // 求当前顺序表的实际长度
    int length();
    // 任意位置插入函数
    bool insert_pos(int pos, datatype e);
    // 任意位置删除函数
    bool delete_pos(int pos);
    // 访问容器中任意一个元素 at
    datatype &at(int index);
    // 遍历整个数组输出
    void show();
    // 君子函数:二倍扩容
    void expend();
    // 释放函数
    void free();

};
// 初始化函数
void SeqList::init(int s)
{
    size = s;                  // 当前数组的最大容量
    data = new datatype[size]; // 在堆区申请一个顺序表容器
}
// 判空函数
bool SeqList::empty()
{
    if (len == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
// 判满函数
bool SeqList::full()
{
    if (size == len)
    {
        return true;
    }
    else
    {
        return false;
    }
}
// 添加数据函数
bool SeqList::add(datatype e)
{
    if (data == NULL)
    {
        cout << "添加数据失败" << endl;
        return false;
    }
    if (full())
    {
        expend();
    }
    data[len]=e;
    len++;
    cout << "添加数据成功" << endl;
    return true;
}
// 求当前顺序表的实际长度
int SeqList::length()
{
    return len;
}
// 任意位置插入函数
bool SeqList::insert_pos(int pos, datatype e)
{
    if (data == NULL || pos < 0 || pos > len)
    {
        cout << "插入数据失败" << endl;
        return false;
    }
    if (full())
    {
        expend();
    }
    for (int i = len - 1; i >= pos; i--)
    {
        data[i + 1] = data[i];
    }
    data[pos] = e;
    len++;
    cout << "插入数据成功" << endl;
    return true;
}
// 任意位置删除函数
bool SeqList::delete_pos(int pos)
{
    if (data == NULL || SeqList::empty() || pos < 0 || pos >= len)
    {
        cout << "删除数据失败" << endl;
        return false;
    }
    for (int i = pos + 1; i < len; i++)
    {
        data[i - 1] = data[i];
    }
    len--;
    cout << "删除数据成功" << endl;
    return true;
}
// 访问容器中任意一个元素 at
datatype &SeqList::at(int index)
{
    if (data == NULL || SeqList::empty() || index < 0 || index >= len)
    {
        cout << "访问数据失败" << endl;
    }
    return data[index];
}
// 遍历整个数组输出
void SeqList::show()
{
    if (data==NULL||empty())
    {
        cout << "遍历数组失败" << endl;
        return ;
    }
    cout << "数组中的数据:" << endl;
    for (int i = 0; i < length(); i++)
    {
        cout << data[i] <<'\t';
    }
    cout << endl;
}
// 释放函数
void SeqList::free()
{
    delete []data;
    data=NULL;
    cout << "释放空间成功"<<endl;
}
// 君子函数:二倍扩容
void SeqList::expend()
{
    datatype *temp;
    size = 2*size;
    temp =new datatype[size];
    std::memcpy(temp,data,size/2*sizeof(datatype));
    free();
    data =temp;
    cout << "申请空间已满" << endl;
    cout << "自动二倍扩容" << endl;
    cout << length() << endl;
}
int main()
{
    SeqList L;
    L.init(1);
    L.add(1);
    L.add(2);
    L.add(3);
    L.add(4);
    L.add(5);
    L.add(6);
    L.add(6);
    L.add(6);
    L.add(99);
    L.add(99);
    L.add(99);
    L.add(99);
    L.add(99);
    L.show();
    L.free();
    L.show();
    return 0;
}

效果图:

二.思维导图:

相关推荐
C语言不精8 分钟前
c语言-优雅的多级菜单设计与实现
c语言·开发语言·算法
Evan芙18 分钟前
用Shell脚本破解经典鸡兔同笼问题
linux·运维·网络
-森屿安年-32 分钟前
二叉平衡树的实现
开发语言·数据结构·c++
Q741_1471 小时前
C++ 高精度计算的讲解 模拟 力扣67.二进制求和 题解 每日一题
c++·算法·leetcode·高精度·模拟
水木姚姚1 小时前
C++ begin
开发语言·c++·算法
꧁坚持很酷꧂1 小时前
Ubuntu系统下Qt程序连接串口设备没有问题,但运行时出现Permission denied的解决方法
linux·qt·ubuntu
jerryinwuhan1 小时前
机器人控制程序
linux·运维·网络
honeysuckle_luo1 小时前
香橙派ai pro安装支持昇腾NPU的ollama
linux·运维·服务器
老王熬夜敲代码2 小时前
泛型编程的差异抽象思想
开发语言·c++·笔记
池央2 小时前
从“算子不支持”到“NPU高效执行”:CANN 8.0 TBE 自定义算子落地实践
linux·人工智能