zoukankan      html  css  js  c++  java
  • 阉割的List

    实在忍不住,模仿STL写了个阉割版的List,还没加迭代器,等搞完STL源码再说吧。

    #pragma once
    
    #include <stdexcept>
    namespace vlyf
    {
        template <typename Key>
        struct Node
        {
            using pointer = Node*;
            Key   key;
            Node* prev{ nullptr };
            Node* next{ nullptr };
            Node() = default;
            Node(Key const& k) : key(k) {}
        };
    
        template <typename T>
        class List
        {
        public:
            typedef typename Node<T>::pointer linkType;
    
        private:
            linkType head{ new Node<T> };
    
        public:
            List()
            {
                head->prev = head;
                head->next = head;
            }
            ~List() = default;
            linkType Begin() const { return head->next; }
            linkType End() const { return head; }
            linkType Search(T const&) const;
            linkType Erase(linkType position);
            void     Insert(linkType position, T const& x);
            void     PushBack(T const& x) { Insert(End(), x); }
            void     PushFront(T const& x) { Insert(Begin(), x); }
            void     PopFront() { Erase(Begin()); }
            void     PopBack()
            {
                linkType tmp = End()->prev;
                Erase(tmp);
            }
            void Clear();
            void Remove(T const& x);
            void Unique();
            void Delete(T const& x);
    
        protected:
            void Transfer(linkType position, linkType first, linkType last);
    
    
        };
    
        template <typename T>
        inline typename List<T>::linkType List<T>::Search(T const& k) const
        {
            if (!head)
                throw std::out_of_range("List is empty");
            linkType p = head;
            while (p->key != k)
            {
                p = p->next;
            }
            return p;
        }
    
        template <typename T>
        inline void List<T>::Insert(List<T>::linkType position, T const& x)
        {
            linkType node = new Node<T>(x);
            node->next = position;
            node->prev = position->prev;
            position->prev->next = node;
            position->prev = node;
        }
    
        template <typename T>
        inline void List<T>::Delete(T const& x)
        {
            if (!x->prev)
                x->prev->next = x->next;
            else
                head->next = x->next;
            if (!x->next)
                x->next->prev = x->prev;
            delete x;
        }
    
        template <typename T>
        inline typename List<T>::linkType List<T>::Erase(typename List<T>::linkType position)
        {
            linkType nextNode = position->next;
            linkType prevNode = position->prev;
            prevNode->next = nextNode;
            nextNode->prev = prevNode;
            delete position;
            return nextNode;
        }
    
        template <typename T>
        inline void List<T>::Clear()
        {
            linkType cur = head->next;
            while (cur != head)
            {
                linkType tmp = cur;
                cur = cur->next;
                delete tmp;
            }
            head->next = head;
            head->prev = head;
        }
    
        template <typename T>
        inline void List<T>::Remove(T const& x)
        {
            linkType first = Begin();
            linkType last = End();
            while (first != last)
            {
                linkType tmp = first;
                tmp = tmp->next;
                if (x == first->key)
                {
                    Erase(first);
                }
                first = tmp;
            }
        }
    
        template <typename T>
        inline void List<T>::Unique()
        {
            linkType first = Begin();
            linkType last = End();
            if (first == last)
                return;
            linkType next = first->next;
            while (next != last)
            {
                if (next->key == first->key)
                    Erase(next);
                else
                    first = next;
                next = first->next;
            }
        }
    
        template <typename T>
        inline void List<T>::Transfer(linkType position,
            linkType first,
            linkType last)
        {
            last->prev->next = position;
            first->prev->next = last;
            position->prev->next = first;
            linkType tmp = position->prev;
            position->prev = last->prev;
            last->prev = first->prev;
            first->prev = tmp;
        }
    }  // namespace vlyf
    
    
  • 相关阅读:
    Druid 使用 Kafka 将数据载入到 Kafka
    Druid 使用 Kafka 数据加载教程——下载和启动 Kafka
    Druid 集群方式部署 —— 启动服务
    Druid 集群方式部署 —— 端口调整
    Druid 集群方式部署 —— 配置调整
    Druid 集群方式部署 —— 配置 Zookeeper 连接
    Druid 集群方式部署 —— 元数据和深度存储
    Druid 集群方式部署 —— 从独立服务器部署上合并到集群的硬件配置
    Druid 集群方式部署 —— 选择硬件
    Druid 独立服务器方式部署文档
  • 原文地址:https://www.cnblogs.com/vlyf/p/12040882.html
Copyright © 2011-2022 走看看