#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. Sqli-labs less 32

    Less-32 利用上述的原理,我们可以进行尝试payload为: http://127.0.0.1/sqli-labs/Less-32/?id=-1%df%27union%20select%201, ...

  2. docker-py的配置与使用

    测试环境 75机:Red Hat Enterprise Linux Server 7.0,无外网访问权限 73机:Red Hat Enterprise Linux Server 7.0,无外网访问权限 ...

  3. 线上问题 - MySQL SQL state [HY000]; error code [1366]

    一.问题描述 另外一个系统调用服务接口api:/xxx/create?aName=&time=&...,数据没有保存成功提示SQL state [HY000]; error code ...

  4. POJ 1552

    #include<iostream> using namespace std; int main() { ]; int i,j; ; do{ sum=; ;num[i-]!=&&a ...

  5. C# static方法-使用迭代器循环遍历文件中的额行

    //封装的方法 //读取文件的值,放入集合中 public static IEnumerable<string> ReadLines(string fileName) { using (T ...

  6. Microsoft SDK 中Sample案例之Amcap項目 的运行方法(转)

    http://blog.csdn.net/erick08/article/details/7194575 Microsoft  SDK 中Sample之Amcap 的运行方法      写这篇文章的由 ...

  7. hdu 3537 Daizhenyang's Coin (翻硬币游戏)

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

  8. MySQL数据库的基本操作命令

    MySQL数据库的基本操作命令 [mysql]mysql 常用建表语句 一.mysql服务操作 net start mysql //启动mysql服务 net stop mysql //停止mysql ...

  9. Spring框架学习之第9节

    aop编程 aop(aspect oriented programming)面向切面(方面)编程,是所有对象或者是一类对象编程,核心是(在不增加代码的基础上,还增加新功能) 汇编(伪机器指令 mov ...

  10. React属性和状态对比

    一.相似点 二.区别 三.如何区分 PS:所有的数据都可以变成属性