• 单向链表实现


    通过重载new 和 delete 大大提高内存空间的分配速度。 

    template <class type> class Link
    {
     type elememt;
     Link* next;
     Link(const type& ele, Link* ne == NULL)
     {
      element = ele;
      next = ne;
     }
     Link(Link* ne = NULL)
     {
      next = ne;
     }
    }

    template <class type> class link_list: public Link<type>
    {
     private:
      Link<type>* head;
      Link<type>* tail;
      Link<type>* fence;
      int leftcnt;
      int rightcnt;
      void init()
      {
       fence = tail = head = new Link<type>;
       rightcnt = leftcnt = 0;
      }
      void remove_all()
      {
       while (head != NULL)
       {
        Link<type>* tmp = NULL;
        tmp = head;
        head = head->next;
        delete tmp;
       }
      }
      
      static Link<type>* freelist;
      void* operator new(size_t);
      void operator delete(void*);
     
     public:
      link_list()
      {
       init();
      }
      ~link_list()
      {
       remove_all();
      }
      void clear()
      {
       remove_all();
       init();
      }
      bool insert(const type&);
      bool append(const type&);
      bool remove(type&);
      void setStart()
      {
       fence = head;
       rightcnt += leftcnt;
       leftcnt = 0;
      }
      void setEnd()
      {
       fence = tail;
       leftcnt += rightcnt;
       rightcnt = 0;
      }
      bool prev();
      void next();
      {
       if (fence != tail)
       {
        fence = fence->next;
        rightcnt--;
        leftcnt++;
       }
      }
      bool setPos(int pos);
    }

    template <class type>
    bool link_list<type>::insert(const type& item)
    {
     Link<type>* tmp = new Link<type>(item, fence->next);
     fence->next = tmp;
     if (tail == fence)
      tail = fence->next;
     right++;
     return true;
    }

    template <class type>
    bool link_list<type>::append(const type& item)
    {
     tail = tail->next = new Link<type>(item, NULL);
     right++;
     return true;
    }

    template <class type>
    bool link_list<type>::remove(type& item)
    {
     if (fence->next == NULL)
      return false;
     item = fence->next->element;
     Link<type>* tmp = fence->next;
     fence->next = tmp->next;
     if (tail = tmp)
      tail = fence;
     delete tmp;
     rightcnt__;
     return true;
    }

    template <class type>
    bool link_list<type>::prev(void)
    {
     Link<type>* tmp = head;
     if (fence == head)
      return false;
     while (tmp->next != fence)
      tmp = tmp->next;
     fence = tmp;
     rightcnt++;
     leftcnt--;
     return true;
    }

    template <class type>
    bool link_list<type>::setPos(int pos)
    {
     int i = 0;
     while (i != pos)
     {
      fence = fence->next;
      i++;
     }
     return true;
    }

    template <class type>
    link_list<type>* link_list<type>::freelist = NULL;

    template <class type>
    void* link_list<type>::operator new(size_t)
    {
     if (free_list == NULL)
      return new Link<type>;
     Link<type>* tmp = freelist;
     freelist = freelist->next;
     return tmp;
    }

    template <class type>
    void link_list<type>::operator delete(void* ptr)
    {
     ((Link<type>*)ptr)->next = freelist;
     freelist = (Link<type>*)ptr;
    }

  • 相关阅读:
    #JavaScript 闭包问题详解 #打倒心魔
    Typora + cnblog 图片自动上传 (超详细哦)
    #FUNCTION#CALL对象中的函数内作用域问题.md
    #windows #Github #HOST
    #######对象迭代器######
    #为什么不建议使用for...in 去遍历数组
    #前后端附件传输,去重的一种方式#解决方案
    #页面滚动刷新的实现原理 #下拉刷新#上拉刷新#drag to fresh
    自己动手实现一个阻塞队列
    APC注入
  • 原文地址:https://www.cnblogs.com/seebro/p/2476554.html
Copyright © 2020-2023  润新知