• 【C++】day3学习成果:类


    1.自行封装一个栈的类,包含私有成员属性:栈的数组、记录栈顶的变量
    成员函数完成:构造函数、析构函数、拷贝构造函数、入栈、出栈、清空栈、判空、判满、获取栈顶元素、求栈的大小
    头文件stack.h:

    #ifndef STACK_H
    #define STACK_H
    
    #include 
    
    using namespace std;
    
    #define MAX 8
    
    class Stack
    {
    private:
        int *data;      //栈的数组,指向堆区空间,用于存储栈的容器
        int top;        //记录栈顶的变量
    public:
        //无参构造函数
        Stack();
        //有参构造函数
        Stack(int data);
        //析构函数
        ~Stack();
        //拷贝构造函数
        Stack(const Stack &other);
        //入栈
        bool stack_push(int e);
        //出栈
        bool stack_pop();
        //清空栈
        void stack_free();
        //判空
        bool stack_empty();
        //判满
        bool stack_full();
        //获取栈顶元素
        int stack_top();
        //求栈的大小
        int stack_size();
    };
    
    
    
    #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

    源文件stack.cpp:

    #include"stack.h"
    #include
    
    无参构造函数
    //Stack::Stack(){}
    有参构造函数
    //Stack::Stack(int data){}
    析构函数
    //Stack::~Stack(){}
    拷贝构造函数
    //Stack::Stack(const Stack &other){}
    //无参构造函数
    Stack::Stack():top(-1),data(new int[MAX]){
        cout<<"Stack::无参构造函数:初始化栈成功"<<endl;
    }
    有参构造函数
    //Stack::Stack(int data){
    //    cout<<"Stack::有参构造函数"<
    //    top=data;
    //}
    //析构函数
    Stack::~Stack(){
        cout<<"Stack::析构函数"<<endl;
        delete []data;
        data=nullptr;
    }
    //拷贝构造函数
    Stack::Stack(const Stack &other):data(other.data),top(other.top){
        cout<<"Stack::拷贝构造函数"<<endl;
    }
    
    //入栈
    bool Stack::stack_push(int e)
    {
        if(top<-1)
        {
            cout<<"所给栈不合法"<<endl;
            return false;
        }else if(stack_full())
        {
            cout<<"入栈失败,栈满"<<endl;
            return false;
        }
        //先加后压
        top++;
        this->data[top]=e;
        cout<<data[top]<<"入栈成功"<<endl;
        return true;
    }
    
    //出栈
    bool Stack::stack_pop()
    {
        if(top<-1)
        {
            cout<<"所给栈不合法"<<endl;
            return false;
        }
        //出栈逻辑:先弹后减
        cout<<data[top]<<"出栈成功"<<endl;
        top--;
        return true;
    }
    
    //清空栈
    void Stack::stack_free()
    {
        if(top<-1)
        {
            cout<<"所给栈不合法"<<endl;
            return;
        }
        for(top;top>-1;top--)
        {
            stack_pop();
        }
    
        top=-1;
    
        cout<<"清空成功"<<endl;
    }
    
    //判空
    bool Stack::stack_empty()
    {
        if(top==-1)
        {
            return true;
        }else
        {
            if(top<-1)
            {
                cout<<"所给栈不合法"<<endl;
            }
            return false;
        }
    }
    
    //判满
    bool Stack::stack_full()
    {
        if(top<-1)
        {
            cout<<"所给栈不合法"<<endl;
            return false;
        }else if(top==MAX-1)
        {
            return true;
        }else
        {
            return false;
        }
    
    }
    
    //获取栈顶元素
    int Stack::stack_top()
    {
        if(top<-1||stack_empty())
        {
            cout<<"获取失败"<<endl;
            return NULL;
        }
    
        return data[top];
    }
    
    //求栈的大小
    int Stack::stack_size()
    {
        if(!stack_empty())
        {
            return top+1;
        }
        cout<<"所给链表不合法"<<endl;
        return -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
    • 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
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138

    主函数main.cpp:

    #include "stack.h"
    
    int main()
    {
        Stack s;
        //入栈
        for(int i=MAX;i>=0;i--)
        {
            s.stack_push(i);
        }
        if(s.stack_full())
        {
            cout<<"栈满"<<endl;
        }
        //出栈
        for(int i=0;i<MAX;i++)
        {
            s.stack_pop();
        }
        s.stack_push(1);
        s.stack_push(2);
        s.stack_push(3);
        s.stack_push(11);
        s.stack_push(12);
        s.stack_push(111);
        cout<<"栈顶元素为:"<<s.stack_top()<<endl;
        s.stack_pop();
        cout<<"栈顶元素为:"<<s.stack_top()<<endl;
        s.stack_free();
        if(s.stack_empty())
        {
            cout<<"栈空"<<endl;
        }
        s.stack_push(1);
        cout<<"栈顶元素为:"<<s.stack_top()<<endl;
        s.stack_push(2);
        s.stack_push(3);
        cout<<"栈顶元素为:"<<s.stack_top()<<endl;
    
        Stack s2;
        s2.stack_push(3);
        s2.stack_push(2);
        s2.stack_push(1);
        s=s2;
        cout<<"栈顶元素为:"<<s.stack_top()<<endl;
        cout<<"&s2="<<&s2<<",&s="<<&s<<endl;
    
        return 0;
    }
    
    • 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

    运行结果:
    运行结果1

    2.自行封装一个循环顺序队列的类,包含私有成员属性:存放队列的数组、队头位置、队尾位置
    成员函数完成:构造函数、析构函数、拷贝构造函数、入队、出队、清空队列、判空、判满、求队列大小

    头文件linkqueue.h:

    #ifndef LINKQUEUE_H
    #define LINKQUEUE_H
    
    #include 
    
    using namespace std;
    
    #define MAX 8
    
    class ListQueue
    {
    private:
        int *data;      //存放队列的数组,初始化时向堆区申请数组空间
        int front;      //队头位置,记录对头所在的元素下标
        int tail;      //队尾位置,记录最后一个元素的下一个下标的位置
    public:
        //无参构造函数
        ListQueue();
        //有参构造函数
        ListQueue(int data);
        //析构函数
        ~ListQueue();
        //拷贝构造函数
        ListQueue(const ListQueue &other);
        //入队
        bool ListQueue_push(int e);
        //出队
        bool ListQueue_pop();
        //清空队
        void ListQueue_free();
        //判空
        bool ListQueue_empty();
        //判满
        bool ListQueue_full();
        //求队列的大小
        int ListQueue_size();
    };
    
    #endif // LINKQUEUE_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

    源文件linkQueue.cpp:

    #include "linkQueue.h"
    
    //无参构造函数
    ListQueue::ListQueue():
        data(new int[MAX]),front(0),tail(0)
    {
        cout<<"Stack::无参构造函数:初始化循环队列成功"<<endl;
    }
    //有参构造函数
    //ListQueue::ListQueue(int data){}
    
    //析构函数
    ListQueue::~ListQueue()
    {
        cout<<"Stack::析构函数"<<endl;
        delete []data;
        data=nullptr;
    }
    
    //拷贝构造函数
    ListQueue::ListQueue(const ListQueue &other):
        data(other.data),front(other.front),tail(other.tail)
    {
         cout<<"Stack::拷贝构造函数"<<endl;
    }
    
    //入队
    bool ListQueue::ListQueue_push(int e)
    {
        if(ListQueue_full())
        {
            cout<<"入队失败\n"<<endl;
            return false;
        }
        //将数据放在队尾所在地方
        data[tail]=e;
        cout<<"data["<<tail<<"]="<<e<<" 入队成功"<<endl;
        //队尾后移
        tail=(tail+1)%MAX;
        return true;
    }
    
    //出队
    bool ListQueue::ListQueue_pop()
    {
        if(ListQueue_empty())
        {
            cout<<"出队失败\n"<<endl;
            return false;
        }
        cout<<"data["<<front<<"]="<<data[front]<<" 出队成功"<<endl;
        //队头后移
        front=(front+1)%MAX;
        return true;
    }
    //清空队
    void ListQueue::ListQueue_free()
    {
        for(int i=front;i<tail;i++)
        {
            data[i]=NULL;
        }
        front=tail=0;
        cout<<"清空队列成功"<<endl;
    }
    
    //判空
    bool ListQueue::ListQueue_empty()
    {
        if(front==tail)
        {
            return true;
        }else
        {
            return false;
        }
    }
    
    //判满
    bool ListQueue::ListQueue_full()
    {
        if((tail+MAX)%MAX==front && !ListQueue_empty())
        {
            return true;
        }else
        {
            return false;
        }
    }
    //求队列的大小
    int ListQueue::ListQueue_size()
    {
        return (tail+MAX-front)%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
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95

    主函数main.c:

    #include "linkQueue.h"
    
    int main()
    {
        ListQueue queue;
    
        queue.ListQueue_push(1);
        queue.ListQueue_push(2);
        queue.ListQueue_push(3);
        queue.ListQueue_push(8);
        queue.ListQueue_push(9);
        queue.ListQueue_push(10);
        queue.ListQueue_push(11);
        queue.ListQueue_push(12);
        queue.ListQueue_push(13);
        queue.ListQueue_pop();
        queue.ListQueue_push(1);
    
        queue.ListQueue_free();
        queue.ListQueue_push(10);
        queue.ListQueue_push(11);
        queue.ListQueue_push(12);
        cout<<"size of queue="<<queue.ListQueue_size()<<endl;
    
        cout<<"*************************"<<endl;
        ListQueue queue2;
        queue2.ListQueue_push(1);
        queue2=ListQueue(queue);
        cout<<"size of queue2="<<queue2.ListQueue_size()<<endl;
    
    
        return 0;
    }
    
    
    • 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

    运行结果:
    运行结果2

    3.思维导图
    C++day3

  • 相关阅读:
    关于图纸的吸附布局、全屏填充、适配内容来做图元根据图纸页面在不同窗口显示尺寸下自适应
    搭建hadoop+spark完全分布式集群环境
    java生态体系
    【FFmpeg】视频与图片互相转换 ( 视频与 JPG 静态图片互相转换 | 视频与 GIF 动态图片互相转换 )
    【日志采集系统】python实现-附ChatGPT解析
    面试时Dubbo原理记不住?来看看《Dubbo原理浅析——从RPC本质看Dubbo》
    将uc/OS移植到stm32F103上实现LED灯和串口操作
    (174)Verilog HDL:设计一个计数器之Count10
    基于若依和flowable6.7.2的ruoyi-nbcio流程管理系统正式发布
    webrtc gcc算法(1)
  • 原文地址:https://blog.csdn.net/kyl_posible/article/details/132765980