#pragma once
#include"reverse_iterator.h"
namespace zh
{
//list单个节点的类
template<class T>
struct ListNode
{
ListNode<T>* _next;
ListNode<T>* _prev;
T _data;
ListNode(const T& x = T())
:_data(x)
, _next(nullptr)
, _prev(nullptr)
{}
};
//迭代器的类
template<class T, class Ref, class Ptr>
struct __list_iterator
{
typedef ListNode<T> Node;
typedef __list_iterator<T, Ref, Ptr> self;
typedef Ref reference;
typedef Ptr pointer;
//成员变量
Node* _node;
//拷贝构造和赋值不需要自己实现
//析构不需要自己实现——>为什么呢?
//因为迭代器类是为了访问和修改list的节点,而不是释放该节点,
//这个节点的释放list自己会解决
//构造
__list_iterator(Node* x = Node*())
:_node(x)
{}
//后置++
self operator++(int)
{
self tmp = *this;
_node = _node->_next;
return tmp;
}
//前置++
self& operator++()
{
_node = _node->_next;
return *this;
}
//后置--
self operator--(int)
{
self tmp = *this;
_node = _node->prev;
return tmp;
}
//前置--
self& operator--()
{
_node = _node->_prev;
return *this;
}
//解引用
Ref operator*()
{
return _node->_data;
}
//箭头的重载
Ptr operator->()
{
//如果list的每个数据是日期类,这里就是返回日期类的地址,
//又因为日期类是struct类型,这里又是指针,从而支持->的使用。
return &_node->_data;
}
//==的判断
bool operator!=(const self& it) const
{
return _node != it._node;
}
bool operator==(const self& it) const
{
return _node == it._node;
}
};
//真正的list
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;
/*typedef reverse_iterator const_reverse_iterator;
typedef reverse_iterator reverse_iterator;*/
typedef reverse_iterator<const_iterator, const T&, const T*> const_reverse_iterator;
typedef reverse_iterator<iterator, T&, T*> reverse_iterator;
reverse_iterator rbegin()
{
return reverse_iterator(end());
}
reverse_iterator rend()
{
return reverse_iterator(begin());
}
const_reverse_iterator rbegin() const
{
return const_reverse_iterator(end());
}
reverse_iterator rend() const
{
return const_reverse_iterator(begin());
}
//构造
list()
{
//申请一个头结点
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
//拷贝构造和赋值的现代写法
//既然是现代写法,所以需要一个人帮忙
template<class InputIterator>
//这也是一个构造函数,只是有两个参数
list(InputIterator first, InputIterator last)
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
while (first != last)
{
push_back(*first);
++first;
}
}
//构造n个val值
list(size_t n, const T& val = T())
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
for (size_t i = 0; i < n; ++i)
{
push_back(val);
}
}
//构造一个int的类型
list(int n, const T& val = T())
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
for (size_t i = 0; i < n; ++i)
{
push_back(val);
}
}
//拷贝构造
list(const list<T>& lt)
{
//为什么建立一个头结点?
//因为tmp析构的时候直接析构随机值或者空指针会报错
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
list<T> tmp(lt.begin(), lt.end());
//交换两个节点的指针
std::swap(_head, tmp._head);
}
//赋值重载
list<T>& operator=(list<T> lt)
{
std::swap(_head, lt._head);
return *this;
}
拷贝构造和赋值的传统写法
拷贝构造
//list(list& lt)
//{
// _head = new Node();
// _head->_next = _head;
// _head->_prev = _head;
// for (auto e : lt)
// {
// push_back(e);
// }
//}
赋值
//list& operator=(list& lt)
//{
// if (this != <)
// {
// clear();
// for (auto e : lt)
// {
// push_back(e);
// }
// }
// return *this;
//}
//析构函数
~list()
{
clear();
delete _head;
_head = nullptr;
}
//清掉list所以数据
void clear()
{
iterator it = begin();
while (it != end())
{
iterator del = it++;
delete del._node;
}
_head->_next = _head;
_head->_prev = _head;
}
//尾插
void push_back(const T& x)
{
Node* tail = _head->_prev;
Node* newnode = new Node(x);
tail->_next = newnode;
newnode->_prev = tail;
_head->_prev = newnode;
newnode->_next = _head;
//也可以复用insert,在头节点前面插入一个节点就是尾插
//insert(end(), x);
}
//头插
void push_front(const T& x)
{
insert(begin(), x);
}
//迭代器
iterator begin()
{
return iterator(_head->_next);
}
//end是最后一个数据的下一个位置,所以就是_head;
iterator end()
{
return iterator(_head);
}
const_iterator begin() const
{
return const_iterator(_head->_next);
}
const_iterator end() const
{
return const_iterator(_head);
}
//在pos前面进行插入
iterator insert(iterator pos, const T& x)
{
//因为iterator是一个类,要找到其中的节点就需要这样
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;
//返回新的节点
return iterator(newnode);
}
//删除pos位置,当pos被erase之后,pos就失效了
//所以删除之后,要返回被删除了下一个节点的迭代器
iterator erase(iterator pos)
{
assert(pos != end());
Node* prev = pos._node->_prev;
Node* next = pos._node->_next;
delete pos._node;
prev->_next = next;
next->_prev = prev;
return iterator(next);
}
//尾删
void pop_back()
{
//end其实是头结点,--可以得到尾结点
erare(--end());
}
//头删
void pop_front()
{
erase(begin());
}
private:
Node* _head;
};
void test_list1()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
struct Data
{
int _year;
int _month;
int _day;
Data(int year = 1, int month = 1, int day = 1)
:_year(year)
, _month(month)
, _day(day)
{}
};
void test_list2()
{
list<Data> lt;
lt.push_back(Data());
lt.push_back(Data());
lt.push_back(Data());
lt.push_back(Data());
list<Data>::iterator it = lt.begin();
while (it != lt.end())
{
cout << it->_year << "/" << it->_month << "/" << it->_day << endl;
it++;
}
}
void test_list3()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
list<int>::iterator it = lt.begin();
lt.insert(it, 3);
list<int>::iterator now = lt.begin();
while (now != lt.end())
{
cout << (*now ) << " ";
++now;
}
cout << endl;
}
void test_list4()
{
list<int> lt1;
lt1.push_back(1);
lt1.push_back(2);
lt1.push_back(3);
lt1.push_back(4);
lt1.push_back(5);
list<int> lt2 = lt1;
for (auto &e : lt2)
{
cout << e << " ";
}
}
void test_list5()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
for (auto &e : lt)
{
cout << e << " ";
}
cout << endl;
lt.clear();
lt.push_back(1);
lt.push_back(2);
for (auto &e : lt)
{
cout << e << " ";
}
cout << endl;
}
void test_list6()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
zh::list<int>::reverse_iterator it = lt.rbegin();
while (it != lt.rend())
{
cout << *it << " ";
it++;
}
cout << endl;
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)