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

C++day6

一、思维导图

二、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.kler.cn/a/291621.html

相关文章:

  • Idean 处理一个项目引用另外一个项目jar 但jar版本低的问题
  • 【C语言】动态内存管理:详解malloc和free函数
  • powershell美化
  • [计算机网络]唐僧的”通关文牒“NAT地址转换
  • 内核执行时动态的vmlinux的反汇编解析方法及static_branch_likely机制
  • 多音轨视频使用FFmpeg删除不要音轨方法
  • MySQL5.7.36之主从复制增强半同步复制-centos7
  • Linux下数据库相关知识点及SQLite3相关知识,和callback回调函数
  • 【区块链 + 供应链】长虹生产物料质量信息管理系统 | FISCO BCOS应用案例
  • 初始QT!
  • [线程]单例模式 及 指令重排序
  • CodeSys中动态切换3D模型
  • 计算机知识科普问答--1
  • QT教程:start()和startTimer()的区别
  • 运维学习————kafka(1)
  • 【LVI-SAM】激光点云如何辅助视觉特征深度提取
  • 前端黑科技:使用 JavaScript 实现网页扫码功能
  • ElasticSearch-关联关系
  • Ruoyi若依框架中工单管理(智能售货机运营管理系统)
  • 前端知识HTMLCSS
  • 软件测试 - 性能测试 (概念)(并发数、吞吐量、响应时间、TPS、QPS、基准测试、并发测试、负载测试、压力测试、稳定性测试)
  • 浙大数据结构:01-复杂度2 Maximum Subsequence Sum
  • Spring Boot:医疗排班系统开发的技术革新
  • java 给list对象根据给定条数进行分组工具类
  • Ai Illustrator 取消吸附到像素点,鼠标拖动的时候只能到像素点
  • 如何给Maven添加阿里云镜像