说明:list的erase存在迭代器失效问题,本文给出两种解决方法,其中的一种是stl中给出的解决方法。
//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;
}