zoukankan      html  css  js  c++  java
  • 构造队列和三法则的编写<<0925

    #include <iostream>
    #include <string>
    #include <vector>
    #include <assert.h>
    using namespace std;
    
    class Node
    {
        public:
            Node()
                :date_(0),next_(0)
            {}
            typedef int datetype;//在类中使用typedef定义新类型,其有效范围为定义起始位置到类右大括号。
            datetype date_;
            Node *next_;
    };
    
    class Queue
    {
        public:
            Queue();
            void Queue_add(const Node::datetype &date);//const 不能漏掉,可以把一个no-const传给const,反过来就不可以
            Queue(const Queue &q);
            Queue &operator=(const Queue &s);
            void print();
            ~Queue();
        private:
            Node *first_;//类里面包含别的类
            Node *last_;     
    };
    
    Queue::Queue()
        :first_(new Node),last_(new Node)
    {
        first_ = last_ =  NULL;
    }
    
    void Queue::Queue_add(const Node::datetype &date)
    { 
        Node *tmp = new Node;
        tmp -> date_ = date;
        if (first_ == NULL)
        {
            first_ = last_ = tmp;
        }
        else
        {
            last_ -> next_ = tmp;
            last_ = tmp;
        }
    }
    
    Queue::Queue(const Queue &q)
    {
        //cout << first_ << endl;
        first_ = last_ = NULL;//复制构造函数也必须完成构造函数的步骤
        assert(first_ == NULL);
        Node *cru = q.first_;
        while (cru)
        {
            Node *tmp = new Node;
            tmp -> date_ = cru -> date_;
            tmp -> next_ = NULL;
            if (first_ != NULL)
            {
                last_ -> next_ = tmp;
                last_ = tmp;
            }
            else 
            {
                first_ = last_ = tmp;
            }
            cru = cru -> next_;
        }
    }
    
    Queue &Queue::operator=(const Queue &q)
    {
     /*  if (first_ == q.first_ && last_ == q.last_)
            return *this;
        Node *cru = first_;
        while (cru)
        {
            Node *tmp = cru;
            cru = cru -> next_;
            delete tmp;
            tmp = NULL;
        }
    
        Node *qcru = q.first_;
        while (qcru)
        {
            Node *tmp = new Node;
            tmp -> date_ = qcru -> date_;
            if (first_ == NULL)
                first_ = last_ = tmp;
            else
            {
                last_ -> next_ = tmp;
                last_ = tmp;
            }
            qcru = qcru -> next_;
        }
        return *this;
    */  
        
        Queue *sp = new Queue(q);//赋值运算符里面可以使用复制运算符,但是如果使用new的话,不使用delete,会造成内存泄露
        this->first_ = sp->first_;//最重要的一点:函数结束是sp销毁(是一个指向Queue的指针),下面那个在函数结束的时候是ps销毁,它是一个Queue类,销毁的时候会调用析构函数,释放内存
        this->last_ = sp->last_;
        return *this;
    
    /*    Queue ps(q);//malloc 和 new申请的空间是在"堆"上的,平时我们都是用声明变量来申请空间的,此时申请到的空间是"栈"上的,注意区分“堆”和“栈”。栈上的空间,不需要程序员负责释放,函数返回后自动就会释放;但是对上的空间,必须由程序员用 free 或 delete 释放,否则就会造成“内存泄露”。
        ps.print();
        this -> first_ = ps.first_;
        this -> last_ = ps.last_;
        return *this;
    */
    }
    
    
    Queue::~Queue()
    {
        while (first_)
        {
            Node *ptr = first_;
            first_ = first_ -> next_;
            delete ptr;
        }
    }
    
    void Queue::print()
    {
        Node* cur = first_;
        while (cur)
        {
            cout << cur -> date_<< endl;
            cur = cur -> next_;
        }
    }
    
    int main(int argc, const char *argv[])
    {
        Queue s1;
        s1.Queue_add(1);
        s1.Queue_add(2);
        s1.Queue_add(3);
        s1.Queue_add(4);
    
        s1.print();
    
        Queue s2(s1);
        s2.print();
    
        Queue s4,s3;
        s4 = s3 = s1;
        s3.print();
        s4.print();
        /*  
        Queue s2,s3;
        s2 = s3 = s1;
        Queue s4(s1);
        s1.print();
        s2.print();
        s3.print();
        s4.print();
        s2 = s2;
        s2.print();
        */
        return 0;
    }
    
    
  • 相关阅读:
    转发:前端新人少走弯路
    react 表单(受控组件和非受控组件)
    axios封装的拦截器的应用
    react与redux的一点心得(理解能力有限,蜗牛进度)
    redux一些自习时候自己写的的单词
    react路由的跳转和传参
    学以致用,react学习前奏准备阶段
    原型,原型链,给予原型和class的继承
    rpm安装和卸载
    .net项目发布到iis
  • 原文地址:https://www.cnblogs.com/sunstars/p/3999024.html
Copyright © 2011-2022 走看看