• C++ 类模板实现栈和循环队列


    栈头文件

    #ifndef MY_STACK_H
    #define MY_STACK_H
    template <typename T>
    class My_Stack
    {
    private:
        T* 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

    栈函数实现

    #include "my_Stack.h"
    #include 
    
    using namespace std;
    template <typename T>
    My_Stack<T>::My_Stack(int len):m_stack(nullptr),m_length(0),m_max(0)
    {
        m_stack = new T[len];
    
        for(int i = 0; i < len; i++)
        {
            m_stack[i] = 0;
        }
    
        m_max = len;
    }
    template <typename T>
    My_Stack<T>::My_Stack(const My_Stack& obj)
    {
        m_max = obj.m_max;
    
        m_stack = new T[obj.m_max];
    
        for(int i = 0; i < obj.m_max; i++)
        {
            m_stack[i] = obj.m_stack[i];
        }
    }
    template <typename T>
    int My_Stack<T>::length()
    {
        return m_length;
    }
    template <typename T>
    int My_Stack<T>::gettop()
    {
        if(empty())
        {
            return -1;
        }
    
        int &top = m_stack[m_length-1];
    
        return top;
    }
    template <typename T>
    bool My_Stack<T>::pop()
    {
        bool ret = true;
    
        if(empty())
        {
            ret = false;
            cout << "栈为空" << endl;
            return ret;
        }
    
        m_length--;
        cout << "出栈成功" << endl;
    
        return ret;
    }
    template <typename T>
    bool My_Stack<T>::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;
    }
    template <typename T>
    bool My_Stack<T>::empty()
    {
        return m_length == 0;
    }
    template <typename T>
    bool My_Stack<T>::full()
    {
        return m_length == m_max;
    }
    template <typename T>
    bool My_Stack<T>::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;
    }
    template <typename T>
    bool My_Stack<T>::clear()
    {
        bool ret = true;
    
        if(empty())
        {
            ret = false;
            cout << "栈为空" << endl;
            return ret;
        }
        m_length = 0;
    
        cout << "栈已清空" << endl;
    
        return ret;
    }
    template <typename T>
    My_Stack<T>::~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

    循环队列头文件

    #ifndef MY_QUEUE_H
    #define MY_QUEUE_H
    
    template <typename T>
    class My_Queue
    {
    private:
        T* 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

    循环队列函数实现

    #include "my_Queue.h"
    #include 
    
    using namespace std;
    
    template <typename T>
    My_Queue<T>::My_Queue(int len):m_queue(nullptr),front(0),tail(0),m_length(0)
    {
        m_queue = new T[len+1];
    
        for(int i = 0; i < len+1; i++)
        {
            m_queue[i] = 0;
        }
    
        m_length = len+1;
    }
    template <typename T>
    My_Queue<T>::My_Queue(const My_Queue& obj)
    {
        m_length = obj.m_length;
    
        m_queue = new T[obj.m_length];
    
        for(int i = 0; i < obj.m_length; i++)
        {
            m_queue[i] = obj.m_queue[i];
        }
    }
    template <typename T>
    int My_Queue<T>::length()
    {
        return (tail + m_length - front) % m_length;
    }
    template <typename T>
    bool My_Queue<T>::pop()
    {
        bool ret = true;
    
        if(empty())
        {
            cout << "队列为空" << endl;
            ret = false;
            return ret;
        }
    
        front = (front + 1) % m_length;
        cout << "出队成功" << endl;
    
        return ret;
    }
    template <typename T>
    bool My_Queue<T>::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;
    }
    template <typename T>
    bool My_Queue<T>::empty()
    {
        return front == tail;
    }
    template <typename T>
    bool My_Queue<T>::full()
    {
        return (tail + 1) % m_length == front;
    }
    template <typename T>
    bool My_Queue<T>::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;
    }
    template <typename T>
    My_Queue<T>::~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
    • 107
  • 相关阅读:
    儿童护眼灯AA好还是AAA好?双十二精选国AA儿童护眼灯
    docker-compose快速搭建Zookeeper集群
    【计算机图形学】期末考试课后习题重点复习(第3-4章)
    git流水线(Pipeline)导致分支(Branch)无法合并的解决方法
    逍遥自在学C语言 | 位运算符&的高级用法
    前端 js 常用的es5 数组方法
    Windows安装MySQL及Python操作MySQL数据库脚本实例详解
    为何基于树的模型在表格型数据中能优于深度学习?
    服务器制作RAID磁盘阵列并管理
    CSP2022
  • 原文地址:https://blog.csdn.net/m0_72847002/article/details/132866026