List的模拟实现:
List.h
#include<iostream>
#include<list>
using namespace std;
template<class T>
struct _ListNode
{
T _data;
_ListNode<T>* _prev;
_ListNode<T>* _next;
_ListNode<T>(const T& x = T())
: _data(x)
, _prev(nullptr)
, _next(nullptr)
{}
};
template<class T ,class Ref , class Ptr>
struct _ListIterator
{
typedef _ListNode<T> Node; //调用上面的Struct
typedef _ListIterator<T, Ref, Ptr> Self; //实现的实现的的调用
Node* _node;
_ListIterator(Node* node)
:_node(node)
{}
/*const T& operator*()
{
return _node->_data;
}*/
Ref operator*()
{
return _node->_data;
}
//const T* operator->()
//{
// //return &_node->_data;
// return &(operator*());
//}
Ptr operator->()
{
//return &_node->_data;
return &(operator*());
}
Self& operator++() //前置++
{
_node = _node->_next;
return *this;
}
Self operator++(int) //后置++
{
Self tmp(*this);
_node = _node->_next;
return tmp;
}
Self& operator--() //前置--
{
_node = _node->_prev;
return *this;
}
Self operator--(int) //后置++
{
Self tmp(*this);
_node = _node->_prev;
return tmp;
}
bool operator != (const Self& s) //重载 !=
{
return _node != s._node;
}
bool operator == (const Self& s) //重载==
{
return _node == s._node;
}
};
template<class T>
class List
{
typedef _ListNode<T> Node;
public:
typedef _ListIterator<T,T&,T*> iterator; //这里调用迭代器的时候出错了,用成_ListNode,导致程序编不过
typedef _ListIterator<T,const T&,const T*> const_iterator; //调用const的迭代器
iterator begin() //迭代器中开始
{
return iterator(_head->_next);
}
iterator end() //迭代器的结束 ,这两个都是因为为了符合范围for,所以使用了小写;
{
return iterator(_head);
}
const_iterator begin() const //const迭代器中开始
{
return const_iterator(_head->_next);
}
const_iterator end() const //const迭代器的结束
{
return const_iterator(_head);
}
List()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
//l2(l1)
List(const List<T>& l) //const类型,使用const迭代器;
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
const_iterator it = l.begin();
while (it != l.end())
{
this->PushBack(*it);
++it;
}
cout << endl;
}
//l1 =l2
//List<T>& operator=(const List<T>& l)
List<T>& operator=(List<T> l) //现代写法
{
swap(_head, l._head);
return *this;
}
~List()
{
Clear();
delete _head;
_head = nullptr;
}
size_t Size() //个数
{
size_t size = 0;
for (const auto& e : *this)
{
++size;
}
return size;
}
bool Empty() //判断为空
{
//return _head->_next = _head;
return begin() == end();
}
void Clear() //清理
{
Node* cur = _head->_next;
while (cur != _head)
{
Node* next = cur->_next;
//cur = next;
delete cur;
cur = next;
}
_head->_next = _head;
_head->_prev = _head;
}
void PushBack(const T& x) //尾插
{
/*Node* newnode = new Node(x);
Node* tail = _head->_prev;
tail->_next = newnode;
newnode->_prev = tail;
newnode->_next = _head;
_head->_prev = newnode;*/
Insert(end(), x);
}
void PopBack() //尾删
{
Erase(--end());
}
void PushFront(const T& x) //头插
{
Insert(begin(), x);
}
void PopFront() //头删
{
Erase(begin());
}
void Insert(iterator pos, const T& x) //在任意位置插入
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* newnode = new Node(x);
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = cur;
cur->_prev = newnode;
}
//void Erase(iterator pos) //删除,清空;
iterator Erase(iterator pos)
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
prev->_next = next;
next->_prev = prev;
delete cur;
return iterator(next);
}
private:
Node* _head;
};
下面我们看一下执行调用的代码:
List.cpp
#include"List.h"
void 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;
for (auto e : l)
{
cout << e << " ";
}
cout << endl;
}
//const的迭代器
void PrintList(const List<int>& lt) //这里出现的是const的对象,调用非const的类型,报错;
{
List<int>::const_iterator it = lt.begin();
while (it != lt.end())
{
//*it = 10; //这里还出现了将里面的值改变了,所以我们应该使用const的类型的迭代器;
cout << *it << " ";
++it;
}
cout << endl;
}
void TestList1()
{
List<int> l;
l.PushBack(1);
l.PushBack(2);
l.PushBack(3);
l.PushBack(4);
PrintList(l);
}
void TestList2()
{
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())
{
*it = 10;
cout << *it << " ";
++it;
}
cout << endl;
}
//使用迭代器增 删 查 找
void TestList3()
{
List<int> l;
l.PushBack(1);
l.PushBack(2);
l.PushBack(3);
l.PushBack(4);
l.PushFront(0);
l.PopBack();
l.PopFront();
List<int>::iterator it = l.begin();
while (it != l.end())
{
//*it = 10;
cout << *it << " ";
++it;
}
cout << endl;
}
//调用拷贝构造 加 赋值
void TestList4()
{
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;
}
List<int> copy(l);
PrintList(l);
List<int> it2;
it2.PushBack(10);
it2.PushBack(20);
copy = it2;
PrintList(copy);
cout << copy.Size() << endl; //打印里面的个数;
}
//删除偶数
void TestList5()
{
List<int> l;
l.PushBack(1);
l.PushBack(2);
l.PushBack(3);
l.PushBack(4);
auto it = l.begin();
while (it != l.end())
{
if (*it % 2 == 0)
{
it = l.Erase(it);
}
else
{
++it;
}
}
for (auto e : l)
{
cout << e << " ";
}
}
int main()
{
//TestList();
//TestList2();
//TestList3();
//TestList4();
TestList5();
return 0;
}
上面的这些我已近测试过了,都能通过,希望可以借鉴,可以互相关注一下;