• 有序链表实现集合的抽象数据类型


    template<class T>
    struct SetNode
    {
        T data;
        SetNode<T>* link;
        SetNode():link(NULL) {}
        SetNode(const T& x, SetNode<T>* next = NULL):data(x), link(next) {}
    };

    template<class T>
    class LinkedSet
    {
        public:
            LinkedSet() {first_ = last_ = new SetNode<T>;}
            LinkedSet(LinkedSet<T>& rhs);
            ~LinkedSet<T>() {makeEmpty(); delete first_;}
            void makeEmpty();
            bool addMember(const T& x);
            bool delMember(const T& x);
            LinkedSet<T>& operator = (const LinkedSet<T>& rhs);
            LinkedSet<T>& operator + (const LinkedSet<T>& rhs);
            LinkedSet<T>& operator - (const LinkedSet<T>& rhs);
            LinkedSet<T>& operator * (const LinkedSet<T>& rhs);
            bool Contains(const T& x);
            bool operator == (const LinkedSet<T>& rhs);
            bool Min(T& x);    // 返回集合最小元素的值
            bool Max(T& x);    // 返回集合最大元素的值
            bool subSet(const LinkedSet<T>& rhs);
        private:
            SetNode<T>* first_, last_;
    };

    template<class T>
    LinkedSet<T>::LinkedSet(LinkedSet<T>& rhs)
    {
        SetNode<T>* srcptr = rhs.first_->link;
        first_ = last_ = new SetNode<T>;
        while(srcptr != NULL)
        {
            last_->link = new SetNode<T>(srcptr->data); // 以rhs的结点为内容创建一个新结点
            last_ = last_->link;                        // first不变,改变last
            srcptr = srcptr->link;                      // 循环复制rhs的每个结点至*this
        }
        last_->link = NULL;
    }

    template<class T>
    bool LinkedSet<T>::Contains(const T& x)
    {
    /*    SetNode<T>* tmp = first_->link;
        while(tmp != NULL)
        {
            if(tmp->data == x)
                return true;
        }
        return false;
    */
        SetNode<T>* tmp = first_->link;
        while(tmp != NULL && tmp->data < x)
        {
            tmp = tmp->link;
        }
        if(tmp !=NULL && tmp->data == x)
            return true;
        return false;
    }

    template<class T>
    bool LinkedSet<T>::addMember(const T& x)
    {
        SetNode<T>* tmp = first_->link;
        SetNode<T>* tmp1 = 0;
        while(tmp != NULL && tmp->data < x)
        {
            tmp1 = tmp;
            tmp = tmp->link;
        }

        if(tmp->data == x)
            return false;
        else if(tmp != NULL)
        {
            SetNode<T>* p = new SetNode<T>(x);
            p->link = tmp1->link;
            tmp1->link = p;
            return true;
        }
        else if(tmp == NULL)
        {
            SetNode<T>* p = new SetNode<T>(x);
            last_->link = p;
            last_ = p;
            return true;
        }
    }

  • 相关阅读:
    技术选型总结
    这些年来收集的好用的好玩的软件,强烈推荐给大家
    如何解决markdown中图片上传的问题
    01.如何把.py文件打包成为exe,重点讲解pyinstaller的用法
    Asp.net中汉字转换成为拼音
    程序员常用网址收集
    通过IP来判断所在城市
    以太坊
    分布式系统领域经典论文翻译集
    T50
  • 原文地址:https://www.cnblogs.com/kex1n/p/2286588.html
Copyright © 2020-2023  润新知