list介绍:
- list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
- list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向
其前一个元素和后一个元素。 - list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能向前迭代,已让其更简单高效。
- 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。
- 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这
可能是一个重要的因素)
代码:
代码实现:
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
namespace ZYY
{
template<typename T>
struct _list_node
{
_list_node<T>* _prev;
_list_node<T>* _next;
T _data;
_list_node(const T& x = T()):_prev(nullptr),_next(nullptr),_data(x)
{}
};
template<typename T, typename Ref, typename Ptr>
class _list_iterator
{
typedef _list_node<T> node;
typedef _list_iterator<T, Ref, Ptr> Self;
public:
_list_iterator(node* node):_node(node)
{}
Ref& operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &(_node->_data);
}
Self& operator++()
{
_node = _node->_next;
return *this;
}
Self operator++(int)
{
_list_iterator<T,Ref,Ptr> tmp(*this);
_node = _node->_next;
return tmp;
}
Self& operator--()
{
_node = _node->_prev;
return *this;
}
Self operator--(int)
{
__list_iterator<T> tmp(*this);
_node = _node->_prev;
return tmp;
}
bool operator!=(const Self& it)
{
return it._node != _node;
}
bool operator==(const Self& it)
{
return it._node == _node;
}
node* _node;
};
template<class T>
class list
{
public:
typedef _list_node<T> node;
typedef _list_iterator<T, T&, T*> iterator;
typedef _list_iterator<T, const T&, const T*> const_iterator;
list()
{
_head = new node(T());
_head->_next = _head;
_head->_prev = _head;
}
list(const list<T>& l)
{
_head = new node;
_head->_next = _head;
_head->_prev = _head;
auto it = l.begin();
while(it != l.end())
{
push_back(*it);
it++;
}
}
list<T>& operator=(const list<T>& l)
{
if(this != &l)
{
clear();
auto it = l.begin();
while(it != l.end())
{
push_back(*it);
it++;
}
}
return *this;
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
T& front()
{
return _head->_next->_data;
}
const T& front()const
{
return _head->_next->_data;
}
T& back()
{
return _head->_prev->_data;
}
const T& back() const
{
return _head->_prev->_data;
}
void Swap(list<T>& l)
{
swap(_head,l._head);
}
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;
}
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;
}
void push_back(const T& x)
{
insert(end(),x);
}
void push_front(const T& x)
{
insert(begin(),x);
}
void pop_back()
{
erase(--end());
}
void pop_front()
{
erase(begin());
}
size_t size()
{
size_t count = 0;
auto it = begin();
while(it != end())
{
count++;
it++;
}
return count;
}
bool empty()
{
return begin()==end();
}
void resize(size_t newsize,T x = T())
{
size_t oldsize = size();
if(oldsize > newsize)
{
for(size_t i = newsize ; i < oldsize; i++)
{
pop_back();
}
}
else
{
for(size_t i = oldsize ; i < newsize; i++)
{
push_back(x);
}
}
}
void clear()
{
auto it = begin();
while(it != end())
{
it = erase(it);
}
}
void print_list()
{
for(auto it:*this)
{
cout<<it<<" ";
}
cout<<endl;
}
iterator begin()
{
return (iterator)_head->_next;
}
const_iterator begin() const
{
return (const_iterator)_head->_next;
}
iterator end()
{
return (iterator)_head;
}
const_iterator end() const
{
return (const_iterator)_head;
}
private:
node* _head;
};
}
代码测试:
int main()
{
cout<<"查看list1中元素个数:"<<endl;
ZYY::list<int> list1;
cout<<list1.size()<<endl;
ZYY::list<int> list2;
cout<<"向list1中插入元素:"<<endl;
for(int i = 0 ; i < 10;i++){
list1.push_back(i);
}
list1.print_list();
cout<<"向list2中插入元素:"<<endl;
for(int i = 10 ; i < 20;i++){
list2.push_back(i);
}
list2.print_list();
cout<<"交换list1与list2"<<endl;
list1.Swap(list2);
list1.print_list();
list2.print_list();
cout<<"将list2赋值给list1"<<endl;
list1 = list2;
list1.print_list();
cout<<"将list2清空"<<endl;
list2.clear();
list2.print_list();
cout<<"查看list2的元素个数"<<endl;
cout<<list2.size()<<endl;
cout<<"删除list1中的第二个元素"<<endl;
list1.erase(++list1.begin());
list1.print_list();
cout<<"将list1的元素个数变成20,扩充元素为99"<<endl;
list1.resize(20,99);
list1.print_list();
cout<<"将list1的元素个数变成10"<<endl;
list1.resize(10);
list1.print_list();
}
测试结果:
注意事项:
- list的迭代器并不是一个原生指针,而是进行封装过的
- list迭代器的失效只会发生在删除时(vector容器迭代器的失效是发生在插入删除与扩容时)并且失效的迭代器只是指向删除节点位置的迭代器失效