List&Listiterator

List.h

#pragma once
#define _CRT_SECURuE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<cassert>

//node
template<class T>
struct ListNode
{
    ListNode(const T& data)
    :_data(data)
    , _prev(NULL)
    , _next(NULL)
    {}
    ListNode<T>* _prev;
    ListNode<T>* _next;
    T _data;
};

//Iterator(迭代器)
template<class T, class Ref, class Ptr>
struct _List_Iterator
{
    typedef _List_Iterator<T, Ref, Ptr> self;
    typedef ListNode<T> Node;

    _List_Iterator(Node* node)
        :_node(node)
    {}

    //代码的复用(效率较高)
    //T&
    //const T&
    Ref operator*()
    {
        return _node->_data;
    }

    //const T*
    //T*
    Ptr operator->()
    {
        return &(_node->_data);
    }

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

    self operator++(int)
    {
        Node* tmp = _node;
        _node = _node->_next;
        return self(tmp);
    }

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

    self operator--(int)
    {
        Node* tmp = _node;
        _node = _node->_prev;
        return self(tmp);
    }

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

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

    Node* _node;
};

template<class T>
class List
{
public:
    typedef ListNode<T> Node;
    typedef _List_Iterator<T, T&, T*> Iterator;
    typedef _List_Iterator<T, const T&,const T*> Const_Iterator;

    Node* GetNode(const T& x)
    {
        return new Node(x);
    }

    List()//双向循环链表
    {
        _head = new Node(T());

        _head->_next = _head;
        _head->_prev = _head;
    }

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

    Const_Iterator Begin()const//这里需要注意this的类型
    {
        return Const_Iterator(_head->_next);
    }

    Iterator End()
    {
        return Iterator(_head);
    }

    Const_Iterator End()const
    {
        return Const_Iterator(_head);
    }
    //尾插
    void PushBack(const T& x)
    {
        //方法1:
        //Node* cur = GetNode(x);
        需要考虑(1.只有一个头结点时 2.有多个节点时  ------->在这里两种情况同种方法处理)
        //Node* tail = _head->_pxrev;
        //tail->_next = cur;
        //cur->_prev = tail;
        //cur->_next = _head;
        //_head->_prev = cur;

        //方法2:tail == _head
        Insert(_head,x);
    }

    void PopBack()
    {
        Erase(Iterator(_head->_prev));
    }

    void PushFront(const T& x)
    {
        Insert(_head->_next, x);
    }

    void PopFront()
    {
        Erase(Iterator(_head->_next));
    }

    //任意位置插入(pos的前面)
    void Insert(Iterator pos, const T& x)
    {
        //需要考虑:1.pos是否存在  2.只有头结点  3.只有一个节点及有多个节点(最终可用一种方法解决)
        assert(pos._node != NULL);

        Node* prev = pos._node->_prev;
        Node* cur = GetNode(x);

        prev->_next = cur;
        cur->_prev = prev;
        cur->_next = pos._node;
        pos._node->_prev = cur;
    }

    //删除链表中的任意节点
    Iterator Erase(Iterator& pos)
    {
        //需要考虑:1.pos的有效性 2.不能删除头节点 3.删除第一个节点 4.删除最后一个节点 5.删除中间节点
        //综之考虑:3、4、5是同一种情况
        assert(pos._node && pos != End());//检查情况1、2
        Node* prev = pos._node->_prev;
        Node* next = pos._node->_next;

        prev->_next = next;
        next->_prev = prev;
        delete pos._node;
        pos._node = NULL;
        return next;
    }

    Iterator Find(const T& x)
    {
        Iterator it = Begin();
        while (it != End())
        {
            if (*it == x)
            {
                return it;
            }
            it++;
        }
        return End();
    }

    //删除链表中的节点(最后剩余一个头结点)
    void Clear()
    {
        Iterator it = Begin();
        while (it != End())
        {
            Node* del = it._node;
            it++;
            delete del;
        }
        _head->_next = _head;//最后记得构建头结点成环
        _head->_prev = _head;
    }

    ~List()
    {
        Clear();
        delete _head;
        _head = NULL;
    }

protected:
    Node* _head;
};

list.cpp

#include"List.h"

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

//迭代器失效
void Print(List<int>& l)
{
    List<int>::Iterator it = l.Begin();
    while (it != l.End())
    {
        //迭代器失效
        /*if (*it % 2 == 0)
        {
            l.Erase(it);
        }
        it++;*/
        if (*it % 2 == 0)
        {
            it = l.Erase(it);
        }
        else
        {
            it++;
        }
    }
}

void Test()
{
    List<int> l1;
    l1.PushBack(1);
    l1.PushBack(2);
    l1.PushBack(3);
    l1.PushBack(4);
    l1.PushBack(5);
    PrintList(l1);
    //l1.Insert(l1.Find(5), 9);
    l1.PopBack();
    l1.PopBack();
    l1.PopBack();
    l1.PopBack();
    //l1.PopBack();
    //l1.PopBack();
    PrintList(l1);

}

void TestList()
{
    List<int > l;
    l.PushFront(1);
    l.PushFront(2);
    l.PushFront(3);
    PrintList(l);
    Print(l);
    PrintList(l);
}

int main()
{
    TestList();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值