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
    
    
  • 相关阅读:
    jchdl
    jchdl
    UVa 11134 (区间上的贪心) Fabled Rooks
    UVa (二分) 11627 Slalom
    POJ 1037 (计数 + DP) 一个美妙的栅栏
    HDU 3448 Bag Problem
    HDu 3449 (有依赖的01背包) Consumer
    POJ 1456 (贪心+并查集) Supermarket
    POJ 2236 (简单并查集) Wireless Network
    POJ 1703 Find them, Catch them
  • 原文地址:https://www.cnblogs.com/vlyf/p/12040882.html
Copyright © 2011-2022 走看看