目录
C++:list及其模拟实现
成员函数接口总览
结点类的模拟实现
构造函数
迭代器类的模拟实现
构造函数
++运算符的重载
--运算符的重载
==运算符的重载
!=运算符的重载
*运算符的重载
->运算符的重载
list的模拟实现
默认成员函数
构造函数
拷贝构造函数
赋值运算符重载
析构函数
迭代器相关函数
begin和end
访问容器相关函数
front和back
插入、删除函数
insert
erase
push_back和pop_back
push_front和pop_front
其他函数
size
resize
clear
empty
swap
list模拟实现整体代码
#include
#include
#include
using namespace std;
namespace wjq
{
//模拟实现list当中的结点类
template
struct list_node
{
//成员函数
list_node(const T& x = T());
//成员变量
list_node* _next;
list_node* _prev;
T _data;
};
//模拟实现list迭代器
template
struct __list_iterator
{
typedef list_node node;
typedef __list_iterator Self;
//构造函数
__list_iterator(node* p);
//迭代器运算符的重载函数
Ref operator*();
Ptr operator->();
Self& operator++();
Self operator++(int);
Self& operator--();
Self operator--(int);
bool operator!=(const Self& it) const;
bool operator==(const Self& it) const;
//成员变量
node* _pnode;
};
//链表类
template
class list
{
public:
typedef list_node node;
typedef __list_iterator iterator;
typedef __list_iterator const_iterator;
void empty_initialize();
//构造函数
list();
//迭代器区间构造
template
list(InputIterator first, InputIterator last);
//析构函数
~list();
//拷贝构造
list(const list& lt);
//赋值运算符重载
list& operator=(list lt);
//插入删除函数
void push_back(const T& x);
void pop_back();
void push_front(const T& x);
void pop_front();
iterator insert(iterator pos, const T& x);
iterator erase(iterator pos);
//访问容器相关函数
T& front();
T& back();
const T& front() const;
const T& back() const;
//链表小接口
bool empty() const;
void clear();
size_t size() const;
void swap(list& lt);
void resize(size_t n, const T& val = T());
//普通begin(),end()迭代器
iterator begin();
iterator end();
//const begin(),end()迭代器
const_iterator begin() const;
const_iterator end() const;
private:
node* _head;
size_t _size;
};
}
list_node(const T& x = T())
:_next(nullptr)
, _prev(nullptr)
, _data(x)
{}
_list_iterator(node* p)
:_pnode(p)
{}
Self& operator++()
{
_pnode = _pnode->_next;
return *this;
}
Self operator++(int)//后置++
{
Self tmp(*this);
_pnode = _pnode->_next;
return tmp;
}
Self& operator--()
{
_pnode = _pnode->_prev;
return *this;
}
Self operator--(int)//后置--
{
Self tmp(*this);
_pnode = _pnode->_prev;
return tmp;
}
bool operator==(const Self& it) const
{
return _pnode == it._pnode;
}
bool operator!=(const Self& it) const
{
return _pnode != it._pnode;
}
Ref operator*()
{
return _pnode->_data;
}
Ptr operator->()
{
return &_pnode->_data;
}
void empty_initialize()
{
_head = new node;
_head->_next = _head;
_head->_prev = _head;
_size = 0;
}
//构造函数
list()
{
empty_initialize();
}
//迭代器区间构造
template
list(InputIterator first, InputIterator last)
{
empty_initialize();
while (first != last)
{
push_back(*first);
++first;
}
}
传统写法:
void swap(list& lt)
{
std::swap(_head, lt._head);
std::swap(_size, lt._size);
}
list(const list& lt)
{
empty_initialize();
for (const auto& e : lt)//取lt的数据给e
{
push_back(e);
}
}
现代写法:
void swap(list& lt)
{
std::swap(_head, lt._head);
std::swap(_size, lt._size);
}
list(const list& lt)
{
list tmp(lt.begin(), lt.end());
empty_initialize();
swap(tmp);
}
传统写法:
list& operator=(list lt)
{
if (this != <)
{
clear();
for (const auto& e : lt)
{
push_back(e);
}
}
return *this;
}
现代写法:
void swap(list& lt)
{
std::swap(_head, lt._head);
std::swap(_size, lt._size);
}
list& operator=(list lt)
{
swap(lt);
return *this;
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
//普通begin(),end()迭代器
iterator begin()
{
return iterator(_head->_next);
}
iterator end()
{
return iterator(_head);
}
//const begin(),end()迭代器
const_iterator begin() const
{
return const_iterator(_head->_next);
}
const_iterator end() const
{
return const_iterator(_head);
}
T& front()
{
return *begin();
}
T& back()
{
return *(--end());
}
const T& front() const
{
return *begin();
}
const T& back() const
{
return *(--end());
}
iterator insert(iterator pos, const T& x)
{
node* newnode = new node(x);
node* cur = pos._pnode;
node* prev = cur->_prev;
newnode->_prev = prev;
newnode->_next = cur;
prev->_next = newnode;
cur->_prev = newnode;
++_size;
return iterator(newnode);
}
iterator erase(iterator pos)
{
assert(!empty());
node* prev = pos._pnode->_prev;
node* next = pos._pnode->_next;
prev->_next = next;
next->_prev = prev;
delete pos._pnode;
--_size;
return iterator(next);
}
void push_back(const T& x)
{
insert(end(), x);
}
void pop_back()
{
erase(--end());
}
void push_front(const T& x)
{
insert(begin(), x);
}
void pop_front()
{
erase(begin());
}
size_t size() const
{
return _size;
}
resize函数的规则:
1.若当前容器的size小于所给n,则尾插结点,直到size等于n为止
2.若当前容器的size大于所给n,则只保留前n个有效数据
resize函数的实现方法:
设置一个变量n,用于记录当前所遍历的数据个数,然后开始遍历容器
void resize(size_t n, const T& val = T())
{
iterator cur = begin();
size_t len = 0;
while (len < n && cur != end())
{
++len;
++cur;
}
if (len == n)
{
while (cur != end())
{
cur = erase(cur);
}
}
else
{
while (len < n)
{
push_back(val);
++len;
}
}
}
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);
}
}
bool empty() const
{
return _head->_next == _head;
}
void swap(list& lt)
{
std::swap(_head, lt._head);
std::swap(_size, lt._size);
}
#include
#include
#include
using namespace std;
namespace wjq
{
template
struct list_node
{
list_node(const T& x = T())
:_next(nullptr)
, _prev(nullptr)
, _data(x)
{}
list_node* _next;
list_node* _prev;
T _data;
};
//用类封装普通/const迭代器
template
struct __list_iterator
{
typedef list_node node;
typedef __list_iterator Self;
//构造函数
__list_iterator(node* p)
:_pnode(p)
{}
//迭代器运算符的重载
Ref operator*()
{
return _pnode->_data;
}
Ptr operator->()
{
return &_pnode->_data;
}
Self& operator++()
{
_pnode = _pnode->_next;
return *this;
}
Self operator++(int)//后置++
{
Self tmp(*this);
_pnode = _pnode->_next;
return tmp;
}
Self& operator--()
{
_pnode = _pnode->_prev;
return *this;
}
Self operator--(int)//后置--
{
Self tmp(*this);
_pnode = _pnode->_prev;
return tmp;
}
bool operator!=(const Self& it)const
{
return _pnode != it._pnode;
}
bool operator==(const Self& it)const
{
return _pnode == it._pnode;
}
//成员变量
node* _pnode;
};
//链表类
template
class list
{
public:
typedef list_node node;
typedef __list_iterator iterator;
typedef __list_iterator const_iterator;
void empty_initialize()
{
_head = new node;
_head->_next = _head;
_head->_prev = _head;
_size = 0;
}
//构造函数
list()
{
empty_initialize();
}
//迭代器区间构造
template
list(InputIterator first, InputIterator last)
{
empty_initialize();
while (first != last)
{
push_back(*first);
++first;
}
}
//析构函数
~list()
{
clear();
delete _head;
_head = nullptr;
}
//拷贝构造
list(const list& lt)
{
list tmp(lt.begin(), lt.end());
empty_initialize();
swap(tmp);
}
void swap(list& lt)
{
std::swap(_head, lt._head);
std::swap(_size, lt._size);
}
//赋值运算符重载
list& operator=(list lt)
{
swap(lt);
return *this;
}
//插入删除函数
void push_back(const T& x)
{
insert(end(), x);
}
void pop_back()
{
erase(--end());
}
void push_front(const T& x)
{
insert(begin(), x);
}
void pop_front()
{
erase(begin());
}
iterator insert(iterator pos, const T& x)
{
node* newnode = new node(x);
node* cur = pos._pnode;
node* prev = cur->_prev;
newnode->_prev = prev;
newnode->_next = cur;
prev->_next = newnode;
cur->_prev = newnode;
++_size;
return iterator(newnode);
}
iterator erase(iterator pos)
{
assert(!empty());
node* prev = pos._pnode->_prev;
node* next = pos._pnode->_next;
prev->_next = next;
next->_prev = prev;
delete pos._pnode;
--_size;
return iterator(next);
}
//访问容器相关函数
T& front()
{
return *begin();
}
T& back()
{
return *(--end());
}
const T& front() const
{
return *begin();
}
const T& back() const
{
return *(--end());
}
//链表小接口
bool empty()const
{
return _head->_next == _head;
}
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);
}
}
size_t size()const
{
return _size;
}
void resize(size_t n, const T& val = T())
{
iterator cur = begin();
size_t len = 0;
while (len < n && cur != end())
{
++len;
++cur;
}
if (len == n)
{
while (cur != end())
{
cur = erase(cur);
}
}
else
{
while (len < n)
{
push_back(val);
++len;
}
}
}
//普通begin(),end()迭代器
iterator begin()
{
return iterator(_head->_next);
}
iterator end()
{
return iterator(_head);
}
//const begin(),end()迭代器
const_iterator begin()const
{
return const_iterator(_head->_next);
}
const_iterator end()const
{
return const_iterator(_head);
}
private:
node* _head;
size_t _size;
};
}