• C++数据结构类的自实现,封装栈,循环队列


    my_Queue.h

    #ifndef MY_QUEUE_H
    #define MY_QUEUE_H
    
    class My_Queue
    {
    private:
        int* m_queue;	//队列空间指针
        int front;		//队头
        int tail;		//队尾
        int m_length;	//队列长度
    
    public:
    	//构造函数
        My_Queue(int len);
        //构造拷贝函数
        My_Queue(const My_Queue& obj);
        //队列长度
        int length();
        //出队
        bool pop();
        //入队
        bool push(int value);
        //判空
        bool empty();
        //判满
        bool full();
        //遍历
        bool show();
        //析构函数,释放堆空间
        ~My_Queue();
    };
    
    #endif // MY_QUEUE_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    my_Queue.c

    #include "my_Queue.h"
    #include 
    
    using namespace std;
    
    My_Queue::My_Queue(int len):m_queue(nullptr),front(0),tail(0),m_length(0)
    {
        m_queue = new int[len+1];
    
        for(int i = 0; i < len+1; i++)
        {
            m_queue[i] = 0;
        }
    
        m_length = len+1;
    }
    
    My_Queue::My_Queue(const My_Queue& obj)
    {
        m_length = obj.m_length;
    
        m_queue = new int[obj.m_length];
    
        for(int i = 0; i < obj.m_length; i++)
        {
            m_queue[i] = obj.m_queue[i];
        }
    }
    
    int My_Queue::length()
    {
        return (tail + m_length - front) % m_length;
    }
    
    bool My_Queue::pop()
    {
        bool ret = true;
    
        if(empty())
        {
            cout << "队列为空" << endl;
            ret = false;
            return ret;
        }
    
        front = (front + 1) % m_length;
        cout << "出队成功" << endl;
    
        return ret;
    }
    
    bool My_Queue::push(int value)
    {
        bool ret = true;
    
        if(full())
        {
            cout << "队列已满" << endl;
            ret = false;
            return ret;
        }
    
        m_queue[tail] = value;
    
        tail = (tail + 1) % m_length;
        cout << "入队成功" << endl;
    
        return ret;
    }
    
    bool My_Queue::empty()
    {
        return front == tail;
    }
    
    bool My_Queue::full()
    {
        return (tail + 1) % m_length == front;
    }
    
    bool My_Queue::show()
    {
        bool ret = true;
    
        if( empty() )
        {
            ret = false;
            cout << "队列为空" << endl;
            return ret;
        }
    
        for(int i = front; i != tail; i = (i+1)%m_length)
        {
            cout << m_queue[i] << " ";
        }
    
        cout << endl;
    
        return ret;
    }
    
    My_Queue::~My_Queue()
    {
        delete[] m_queue;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    在这里插入图片描述

    my_Stack.h

    #ifndef MY_STACK_H
    #define MY_STACK_H
    
    class My_Stack
    {
    private:
        int* m_stack;	//栈空间指针
        int m_length;	//栈长度
        int m_max;		//栈容量
    
    public:
    	//构造函数
        My_Stack(int len);
        //构造拷贝函数
        My_Stack(const My_Stack& obj);
        //获取栈长度
        int length();
        //获取栈顶变量
        int gettop();
        //出栈
        bool pop();
        //入栈
        bool push(int value);
        //判空
        bool empty();
        //判满
        bool full();
        //遍历
        bool show();
        //清空栈
        bool clear();
        //析构函数释放堆空间
        ~My_Stack();
    };
    
    #endif // MY_STACK_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    my_Stack.c

    #include "my_Stack.h"
    #include 
    
    using namespace std;
    
    My_Stack::My_Stack(int len):m_stack(nullptr),m_length(0),m_max(0)
    {
        m_stack = new int[len];
    
        for(int i = 0; i < len; i++)
        {
            m_stack[i] = 0;
        }
    
        m_max = len;
    }
    
    My_Stack::My_Stack(const My_Stack& obj)
    {
        m_max = obj.m_max;
    
        m_stack = new int[obj.m_max];
    
        for(int i = 0; i < obj.m_max; i++)
        {
            m_stack[i] = obj.m_stack[i];
        }
    }
    
    int My_Stack::length()
    {
        return m_length;
    }
    
    int My_Stack::gettop()
    {
        if(empty())
        {
            return -1;
        }
    
        int &top = m_stack[m_length-1];
    
        return top;
    }
    
    bool My_Stack::pop()
    {
        bool ret = true;
    
        if(empty())
        {
            ret = false;
            cout << "栈为空" << endl;
            return ret;
        }
    
        m_length--;
        cout << "出栈成功" << endl;
    
        return ret;
    }
    
    bool My_Stack::push(int value)
    {
        bool ret = true;
    
        if(full())
        {
            ret = false;
            cout << "栈已满" << endl;
            return ret;
        }
    
        m_stack[m_length] = value;
        m_length++;
        cout << "入栈成功" << endl;
    
        return ret;
    }
    
    bool My_Stack::empty()
    {
        return m_length == 0;
    }
    
    bool My_Stack::full()
    {
        return m_length == m_max;
    }
    
    bool My_Stack::show()
    {
        bool ret = true;
        if(empty())
        {
            ret = false;
            cout << "栈为空" << endl;
            return ret;
        }
    
        for(int i = 0; i < m_length; i++)
        {
            cout << m_stack[i] << " ";
        }
        cout << endl;
    
        return ret;
    }
    
    bool My_Stack::clear()
    {
        bool ret = true;
    
        if(empty())
        {
            ret = false;
            cout << "栈为空" << endl;
            return ret;
        }
        m_length = 0;
    
        cout << "栈已清空" << endl;
    
        return ret;
    }
    
    My_Stack::~My_Stack()
    {
        delete[] m_stack;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132

    在这里插入图片描述

  • 相关阅读:
    LVS-DR模式工作过程及优缺点
    使用ChatGPT和Blender绘制金色球的完整指南
    【C/C++】你知道位段吗?段位?不,是位段!
    Android酒店客房预订系统 后台管理+前端app 包含视频教程
    第六章——Python数据容器
    沈阳农业大学计算机考研资料汇总
    【web前端开发】HTML知识点超详细总结
    微服务容错 Resilience4j 接口服务-容错原理
    全景应用程序监控
    第一章-处理器体系结构
  • 原文地址:https://blog.csdn.net/m0_72847002/article/details/132767094