#pragma once

#include<iostream>
using namespace std;
#include<assert.h>

template<class T>
struct __ListNode
{
    __ListNode<T>* _prev;
    __ListNode<T>* _next;
    T _data;

__ListNode(const T&x)
        :_data(x)
        , _prev(NULL)
        , _next(NULL)
    {}
};

template<class T,class Ref,class Ptr>
struct __ListIterator
{
    typedef __ListNode<T> Node;
    typedef __ListIterator<T, Ref, Ptr> Self;
    
    __ListIterator(Node* node)
        :_node(node)
    {}

__ListIterator()
    {}

Node* _node;

Ref operator*()
        {
            return _node->_data;
        }
        
        Ptr operator->()
        {
            return &_node->_data;
        }
        
        bool operator==(const Self&s)const
        {
            return _node == s._node;
        }

bool operator!=(const Self&s)const
        {
            return _node != s._node;
        }

Self& operator++()  //前置
        {
            _node = _node->_next;
            return *this;
        }

Self& operator++(int)
        {
            Self temp(_node);
            _node = _node->_next;
            return *this;
        }

Self& operator--()
        {
            _node = _node->_prev;
            return *this;
        }

Self& operator--(int)
        {
            Self tmp(_node);
            _node = _node->_prev;
            return *this;
        }
};

template<class T>
class List
{
    typedef __ListNode<T> Node;
public:
    typedef __ListIterator<T, T&, T*> Iterator;   //迭代器
    typedef __ListIterator<T, const T&, const T*> ConstIterator;   //const迭代器

Node* Buynode(const T&x)
    {
        Node* node = new Node(x);
        return node;
    }

List()
    {
        _head = Buynode(T());
        _head->_next = _head;
        _head->_prev = _head;
    }

//void PushBack(const T&x)
    //{
    //    Node* tail = _head->_prev;
    //    Node* tmp = Buynode(x);

//    tail->_next = tmp->_next;
    //    tmp->_prev = tail;

//    tmp->_prev = tail;
    //    tmp->_next = _head;
    //    //    //    tail->_next = tmp;
    //    //    //    tmp->_prev = tail;
    //    //
    //    //    //    tmp->_next = _head;
    //    //    //    _head->_prev = tmp;
    //}

void PushBack(const T&x)
    {
        Insert(End(),x);
    }

void PushFront(const T&x)
    {
        Insert(Begin(), x);
    }

void Pop()
    {
        Erase(--End());
    }

void PopFront()
    {
        Erase(Begin());
    }

void Insert(Iterator pos,const T&x)   //在pos前面插入
    {
        Node* cur = pos._node;
        Node* prev = cur->_prev;  //在prev后面插入

Node* tmp = Buynode(x);
        prev->_next = tmp->_next;
        tmp->_prev = prev;

prev->_next = tmp;
        tmp->_next = cur;
        //        tmp->_next = cur;
        //        cur->_prev = tmp;
        //
        //        prev->_next = tmp;
        //        tmp->_prev = prev;
    }

void Erase(Iterator pos)  //在pos前面删除
    {
        assert(pos != End());
        Node* cur = pos._node->_prev;  //要删除的元素
        Node* prev = cur->_prev;
        
        prev->_next = cur->_next;
        prev->_next = prev;
        delete cur;
    }

Iterator Begin()
    {
        return Iterator(_head->_next);
    }

Iterator End()
    {
        return Iterator(_head);
    }
private:
    Node* _head;
};

void main()
{
    //TestList();
    List<int> l;

l.PushBack(1);
    l.PushBack(2);
    l.PushBack(3);
    l.PushBack(4);

List<int>::Iterator it = l.Begin();
    while (it != l.End())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
}

模拟实现STL中的list的更多相关文章

  1. 用数组模拟STL中的srack(栈)和queue(队列)

    我们在理解stack和queue的基础上可以用数组来代替这两个容器,因为STL中的stack和queue有可能会导致程序运行起来非常的慢,爆TLE,所以我们使用数组来模拟他们,不仅可以更快,还可以让代 ...

  2. 适配器模式—STL中的适配器模式分析

    适配器模式通常用于将一个类的接口转换为客户需要的另外一个接口,通过使用Adapter模式能够使得原本接口不兼容而不能一起工作的类可以一起工作. 这里将通过分析c++的标准模板库(STL)中的适配器来学 ...

  3. 模拟实现STL库

    最近在复习STL,感觉再看的时候比刚开始学的时候通透很多.以前模拟实现了一个STL库,最近复习完又重构了一遍.代码放出来以供后面学习.如果有写的不好的地方欢迎大家批评指正. STL_List.h #p ...

  4. C++STL中的vector的简单实用

    [原创] 使用C++STL中的vector, #include <stdio.h> #include<stdlib.h> #include<vector> usin ...

  5. C++ 队列!还是要从 STL 中的说起……

    1. 前言 队列和栈一样,都是受限的数据结构. 队列遵循先进先出的存储原则,类似于一根水管,水从一端进入,再从另一端出去.进入的一端称为队尾,出去的一端称为队头. 队列有 2 个常规操作: 入队:进入 ...

  6. STL中的set容器的一点总结

    1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...

  7. STL中的next_permutation

    给定一个数组a[N],求下一个数组. 2 1 3 4 2 1 4 3 2 3 1 4 2 3 4 1 ..... 在STL中就有这个函数: 1.参数是(数组的第一个元素,数组的末尾),注意这是前闭后开 ...

  8. 3.2 STL中的函数对象类模板

    *: STL中有一些函数对象类模板,如下所示: 1)例如要求两个double类型的x 和y 的积,可以: multiplies<double>()(x,y); 该表达式的值就是x*y的值. ...

  9. C++的模板特化 和 STL中iterator_traits模板的偏特化

    C++中有类模板和函数模板,它们的定义如下所示: 类模板: template<class T1,class T2> class C { //... }; 函数模板: template< ...

随机推荐

  1. firefox浏览器live http headers无法使用

    手贱的将firefox升级后,很多的插件不能使用.我这里因为用到live http headers,所以以此为例子.主要表现为live http headers修改数据包后,尤其是post数据包后,r ...

  2. slot的含义

    1) slot就是槽的意思,是一个资源单位,只有给task分配了一个slot之后,这个task才可以运行.slot分两种,map slot沪蓉reduce slot.另外,slot是一个逻辑概念,一个 ...

  3. ExtJs之字段集FieldSet

    //Ext.form.FieldSet扩展自Ext.container.Container.其优点就是把相同字段集中在一起,在外面字段外面加了个线"围住"他们.        // ...

  4. SGU101

    Dominoes – game played with small, rectangular blocks of wood or other material, each identified by ...

  5. MapReduce实例

    1.WordCount(统计单词) 经典的运用MapReuce编程模型的实例 1.1 Description 给定一系列的单词/数据,输出每个单词/数据的数量 1.2 Sample a is b is ...

  6. LINUX进程控制

    1. 引言 一个程序是存储在文件中的机器指令序列.一般它是由编译器将源代码编译成二进制格式的代码.运行一个程序意味着将这个机器指令序列载入内存然后让处理器(cpu)逐条执行这些指令. 在Unix术语中 ...

  7. lintcode:快乐数

    快乐数 写一个算法来判断一个数是不是"快乐数". 一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为他每个位置上的数字的平方和,然后重复这个过程直到这个数变为1,或是 ...

  8. 用C语言写个程序推算出是星期几?(用泰勒公式实现)

    在日常生活中,我们常常遇到要知道某一天是星期几的问题.有时候,我们还想知道历史上某一天是星期几.比如: “你出生的那一天是星期几啊?” “明年五一是不是星期天?我去找你玩?” 通常,解决这个问题的最简 ...

  9. Java-数据结构与算法-逢3减1

    1.要求:有一群人围成一圈数数,逢3退1人,要求算出最后留下来的人的下标 2.代码: package Test; public class Count3Quit1 { //要求:有一群人围成一圈数数, ...

  10. Geoprocessor 使用

    在AO中使用Geoprocessor(ESRI.ArcGIS.Geoprocessor) 1.观察arcmap中的使用方法,明确各参数意义. 2.arctoolbox中参数对应为features/fe ...