#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. 当你碰到一个网络中有多个PXE Server 肿么办?

    今天在用PXE 安装Openstack Compute节点时,郁闷得发现同一网段中还有一个PXE Server,而我的Compute 启动起来总会先找到它,但那个设置不受我控制,子网也不归我管,那个s ...

  2. POJ 1466

    #include<iostream> #include<stdio.h> #define MAXN 505 using namespace std; int edge[MAXN ...

  3. ESASP 业界第一个最为完善的 ASP MVC框架(待续)

    EchoSong 疯狂了,竟然整ASP框架. ASP就是抛弃的孩子,没人养没人疼的, 智力.四肢不全.何谈框架?? 很多ASP的前辈们要么放弃ASP 投入 ASP.net 或者 PHP怀抱.要么直接用 ...

  4. DMS平台从.NET 1.1升级到.NET 4.0的升级步骤

    1)复制新增的项目到4.0平台解决方案对应目录,添加到到解决方案中:2)合并公共文件(比如修改了FormMain主界面.基础类库.售后界面的修改)3)控件的修订(Dev少数属性可能需要手工调整为新的方 ...

  5. UVA 11076 Add Again 计算对答案的贡献+组合数学

    A pair of numbers has a unique LCM but a single number can be the LCM of more than one possiblepairs ...

  6. lintcode:玩具工厂

    题目 工厂模式是一种常见的设计模式.请实现一个玩具工厂 ToyFactory 用来产生不同的玩具类.可以假设只有猫和狗两种玩具.   样例 ToyFactory tf = ToyFactory(); ...

  7. POJ 1704 Staircase Nim 阶梯博弈

    #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int ...

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

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

  9. springMVC找不到JS等文件

     应用springMVC时 JS等文件找不到错误 应用springMVC时如果配置URL映射时如下配置 <servlet>        <servlet-name>appSe ...

  10. 【总结】java命令解析以及编译器,虚拟机如何定位类

    学Java有些日子了,一直都使用IDE来写程序.这样的好处就是能让我连如何用命令行编译,解释执行Java源代码都不知道,就更不清楚JDK中的编译器和虚拟机(包含字节码解释器)是如何定位到类文件的.悲哀 ...