• day6_C++


    模板 栈

    stack.h

    #ifndef STACK_H
    #define STACK_H
    
    #include 
    #include 
    
    using namespace std;
    
    
    #define MAX 5
    
    template
    class Stack
    {
    public:
        /*构造函数*/
        Stack();
        /*拷贝构造函数*/
        Stack(const Stack& others);
        /*析构函数*/
        ~Stack();
    
        /*判满 true 满 */
        bool is_full();
        /*判满 true 空*/
        bool is_empty();
        /*入栈*/
        void in_stack(T e);
        /*出栈*/
        T out_stack();
        /*清空栈*/
        void clear_stack();
        /*求栈顶元素*/
        T get_stackTop_E();
        /*求栈的大小*/
        void get_stackSize();
    
    
    private:
        int top;
        T *data;
    };
    
    #endif // 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
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    stack.c

    #include "stack.h"
    
    template
    Stack::Stack():data(new T[MAX]),top(-1)
    {
        memset(this->data,0,MAX);
        //在堆区申请max个int大小的空间
        cout<<"栈容器初始化成功"<
    Stack::Stack(const Stack& others):data(new T[MAX]),top(others.top)
    {
        //深拷贝,将堆区内容也拷贝进来
        for(int i = 0;idata[i] = others.data[i];
        }
        cout<<"拷贝完成"<
    Stack::~Stack()
    {
        //释放堆区数据
        delete []data;
        cout<<"析构完成"<
    bool Stack::is_full()
    {
        if(this->top ==MAX-1)
            return true;
        else
            return false;
    }
    
    template
    bool Stack::is_empty()
    {
        if(this->top == -1)
            return true;
        else
            return false;
    }
    
    template
    void Stack::in_stack(T e)
    {
        if(this->is_full()==false){
            this->top++;
            this->data[this->top] = e;
            cout<<"入栈成功"<
    T Stack::out_stack()
    {
        if(this->is_empty()==false){
            T temp = this->data[this->top];
            this->top--;
            return temp;
        }else{
             cout<<"出栈失败,栈空"<
    void Stack::clear_stack()
    {
        if(this->is_empty()==false){
            this->top=-1;
            cout<<"清空成功"<
    T Stack::get_stackTop_E()
    {
         if(this->is_empty()==true)
             return NULL;
         return this->data[this->top];
    }
    
    template
    void Stack::get_stackSize(){
        cout<<"栈中有元素 "<top+1<<"个"<
    • 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

    }

    模板 队列

    queue.c

    #ifndef QUEUE_H
    #define QUEUE_H
    #include 
    #include 
    
    using namespace std;
    
    #define MAX 5
    
    template
    class Queue
    {
    
    public:
        /*构造函数*/
        Queue();
        /*拷贝构造函数*/
        Queue(const Queue& others);
        /*析构函数*/
        ~Queue();
    
        /*判满 true 满 */
        bool is_full();
        /*判满 true 空*/
        bool is_empty();
        /*入队*/
        void in_queue(T e);
        /*出队*/
        T out_queue();
        /*清空队*/
        void clear_queue();
        /*求队的大小*/
        void get_queueSize();
    
    
    private:
        T *data;
        int font;
        int tail;
    };
    
    #endif // 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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    queue.c

    #include "queue.h"
    
    template
    Queue::Queue():data(new T [MAX]),tail(0),font(0)
    {
        memset(this->data,0,MAX);
        cout<<"循环队列初始化成功"<
    Queue::Queue(const Queue& others):data(new T[MAX]),font(others.font),tail(others.tail)
    {
        //深拷贝
        int f = this->font;
        int t = this->tail;
        while ((f+MAX)%MAX==t) {
            this->data[f] = others.data[f];
            f++;
        }
        cout<<"拷贝完成"<
    bool Queue::is_full()
    {
        if((this->tail+1)%MAX == this->font){
            return true;
        }
        return false;
    }
    
    template
    bool Queue::is_empty()
    {
        if(this->font == this->tail){
            return true;
        }
        return false;
    }
    
    template
    Queue::~Queue()
    {
        //释放堆区数据
        delete []data;
        cout<<"析构完成"<
    void Queue::in_queue(T e)
    {
        if(this->is_full() == true){
            cout<<"队列满了"<data[this->tail] = e;
        this->tail =  (this->tail+1)%MAX;
        cout<<"入队成功"<
    T Queue::out_queue()
    {
        if(this->is_empty() == true){
            cout<<"队列空,无元素"<data[this->font];
        this->font = (this->font+1)%MAX;
        return temp;
    }
    
    template
    void Queue::clear_queue()
    {
        if(this->is_empty() == true){
            cout<<"队列空,无元素"<font = 0;
        this->tail = 0;
    }
    
    template
    void Queue::get_queueSize()
    {
        cout<<"队列的大小是:" <<(this->tail-this->font+MAX)%MAX<
    • 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

    思维导图

    请添加图片描述

  • 相关阅读:
    Android键盘监听
    Flv.js编译使用
    asp.net一套完整的实验室综合管理系统源码 LIMS
    解决k8s node节点报错: Failed to watch *v1.Secret: unknown
    使用 Lua 脚本和海康 VisionMaster 进行 TCP 通信
    HTTP 基础 (基于 HTTP 的系统的组件、客户端:用户代理、网络服务器)
    设计模式行为型-状态模式
    文件上传漏洞简介
    leetcode11-盛最多水的容器
    three.js学习笔记(二十二)——混合HTML和WebGL
  • 原文地址:https://blog.csdn.net/weixin_45790676/article/details/132864367