代码拉取完成,页面将自动刷新
#pragma once
#include<assert.h>
#include"ReverseIterater.h"
namespace wx
{
template<class T>
struct ListNode//struct 的成员默认为共有的
{
ListNode<T>* _next;
ListNode<T>* _prev;
T _data;
ListNode(const T& x = T())//默认构造 + "普通构造"
: _next(nullptr)
, _prev(nullptr)
, _data(x)
{}
};
//iterator--封装node*
template<class T,class Ref,class Ptr>
struct __list_iterator//struct 的成员默认为共有的
{
typedef ListNode<T> Node;
typedef __list_iterator<T,Ref,Ptr> self;
Node* _node;
__list_iterator(Node* node)
:_node(node)
{}
//迭代器不需要释放结点 因此不需要自己写析构函数
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;
}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
bool operator!=(const self& s)
{
return _node != s._node;
}
};
/*
//const iterator-----指向的内容不可以修改
template<class T>
struct __list_const_iterator//struct 的成员默认为共有的
{
typedef ListNode<T> Node;
typedef __list_const_iterator<T> self;
Node* _node;
__list_const_iterator(Node* node)
:_node(node)
{}
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;
}
const T& operator*()
{
return _node->_data;
}
bool operator!=(const self& s)
{
return _node == s._node;
}
};
*/
//list
template<class T>
class list
{
typedef ListNode<T> Node;//typedef可以共有 也可以私有 看是否需要对外公布
public:
typedef __list_iterator<T, T&, T*> iterator;//list的iterator
typedef __list_iterator<T, const T&, const T*> const_iterator;//list的const_iterator
typedef ReverseIterator<iterator, T&, T*> recverse_iterator;
typedef ReverseIterator<iterator, const T&, const T*> const_reverse_iterator;
iterator begin()
{
return _head->_next;
}
iterator end()
{
return _head;
}
const_iterator begin() const
{
return _head->_next;
}
const_iterator end() const
{
return _head;
}
recverse_iterator rbegin()
{
return recverse_iterator(end());
}
recverse_iterator rend()
{
return recverse_iterator(begin());
}
const_reverse_iterator rbegin()const
{
return recverse_iterator(end());
}
const_reverse_iterator rend()const
{
return recverse_iterator(begin());
}
list()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
//list(list<T>& ls)
list(const list<T>& ls)
{
//代拷贝的函数制成 带哨兵位的双向链表
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
//插入数据
for (const auto& e : ls)
{
push_back(e);
}
}
//传统写法
//list<T>& operator=(const list<T>& ls)
/*list<T>& operator=(list<T>& ls)
{
if (this != &ls)
{
clear();
for (const auto& e : ls)
{
push_back(e);
}
return *this;
}
}*/
//现代写法
//list<T>& operator=(list<T> ls)//传值 调用拷贝构造产生拷贝
//类中可以不写模板参数:list operator=(list ls)------类名不是类型 类名+模板参数=类型
list<T>& operator=(const list<T> ls)
{
swap(ls);// 将拷贝交换 出了作用域 调用析构函数 拷贝销毁
return *this;
}
void swap(list<T>& tmp)
{
std::swap(_head, tmp._head);
}
//list 的迭代器不会失效----不需要扩容
iterator insert(iterator pos, const T& x)
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* newnode = new Node(x);
//prev newnode cur
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = cur;
cur->_prev = newnode;
//return iterator(newnode);//返回匿名对象
return newnode;//迭代器的构造函数 是 单参数的 可以隐式类型转换
}
iterator erase(iterator pos)//迭代器失效:pos是野指针
{
assert(pos != end());
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
//prev next
prev->_next = next;
next->_prev = prev;
delete cur;
return next;
}
void push_back(const T& val)
{
//传统写法:
//Node* newnode = new Node(val);//创建并且初始化
//Node* tail = _head->_prev;
//tail->_next = newnode;
//newnode->_prev = tail;
//newnode->_next = _head;
//_head->_prev = newnode;
//现代写法:
insert(end(), val);
}
void push_front(const T& val)
{
insert(begin(), val);
}
void pop_back()
{
erase(--end());//头节点的前一个
}
void pop_front()
{
erase(begin());
}
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);//erase后 迭代器指向下一个结点
}
}
private:
Node* _head;
};
void print_list(const list<int>& ls)
{
list<int>::const_iterator it = ls.begin();
while (it != ls.end())
{
//*it += 10; //const迭代器:指向的内容不可改变
cout << *it << " ";
++it;
}
cout << endl;
}
/*void test_list()
{
list<int> ls;
ls.push_back(1);
ls.push_back(2);
ls.push_back(3);
ls.push_back(4);
ls.push_back(5);
list<int>::iterator it = ls.begin();
while (it != ls.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}*/
/*void test_list1()
{
list<int> ls;
ls.push_back(1);
ls.push_back(2);
ls.push_back(3);
ls.push_back(4);
ls.push_back(5);
list<int>::iterator it = ls.begin();
while (it != ls.end())
{
cout << *it << " ";
++it;
}
cout << endl;
ls.push_back(5);
ls.push_front(0);
for (auto e : ls)
{
cout << e << " ";
}
cout << endl;
ls.pop_back();
ls.pop_front();
for (auto e : ls)
{
cout << e << " ";
}
cout << endl;
ls.clear();
for (auto e : ls)
{
cout << e << " ";
}
cout << endl;
ls.push_back(10);
ls.push_back(20);
for (auto e : ls)
{
cout << e << " ";
}
cout << endl;
}*/
/*void test_list2()
{
list<int> ls;
ls.push_back(1);
ls.push_back(2);
ls.push_back(3);
ls.push_back(4);
ls.push_back(5);
cout << "ls: ";
for (auto e : ls)
{
cout << e << " ";
}
cout << endl;
list<int>ls1(ls);
cout << "ls1: ";
for (auto e : ls1)
{
cout << e << " ";
}
cout << endl;
list<int> ls2 ;
ls2 = ls;
cout << "ls2: ";
for (auto e : ls2)
{
cout << e << " ";
}
cout << endl;
list<int> ls3 = ls;
cout << "ls3: ";
for (auto e : ls3)
{
cout << e << " ";
}
cout << endl;
}*/
/*void test_list2()
{
list<int> ls;
ls.push_back(1);
ls.push_back(2);
ls.push_back(3);
ls.push_back(4);
ls.push_back(5);
print_list(ls);
}*/
struct A
{
int _a1;
int _a2;
A(int a1 = 1, int a2 = 1)
:_a1(a1)
,_a2(a2)
{}
};
void test_list2()
{
list<A> ls;
ls.push_back(A());
ls.push_back(A(2,2));
list<A>::iterator it = ls.begin();
while (it != ls.end())
{
cout << it->_a1 << ":" << it->_a2 << endl;
cout << (it.operator->())->_a1 << ":" << (it.operator->())->_a2 << endl;
cout << (*it)._a1 << ":" << (*it)._a2 << endl;
cout << (it.operator*())._a1 << ":" << (it.operator*())._a2 << endl;
++it;
}
}
void test_list3()
{
list<int> ls;
ls.push_back(1);
ls.push_back(2);
ls.push_back(3);
ls.push_back(4);
ls.push_back(5);
list<int>::recverse_iterator it = ls.rbegin();
while (it != ls.rend())
{
cout << *it << " ";
++it;
}
cout << endl;
list<int> ls1 = ls;
list<int>::iterator it1 = ls1.begin();
while (it1 != ls1.end())
{
cout << *it1 << " ";
++it1;
}
cout << endl;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。