文章目录
list介绍和使用
list注意事项
list模拟实现
#pragma once
#include <assert.h>
namespace rq
{
template<class T>
struct list_node
{
T _data;
list_node<T>* _next;
list_node<T>* _prev;
list_node(const T& x = T())
:_data(x)
, _next(nullptr)
, _prev(nullptr)
{}
};
template<class T>
struct list_iterator
{
typedef list_node<T> Node;
typedef list_iterator<T> Self;
Node* _node;
list_iterator(Node* node)
:_node(node)
{}
T& operator*()
{
return _node->_data;
}
T* operator&()
{
return _node->_data;
}
Self& operator++()
{
_node = _node->_next;
return *this;
}
Self& operator--()
{
_node = _node->_prev;
return *this;
}
bool operator!=(const Self& x)
{
return _node != x._node;
}
bool operator==(const Self& x)
{
return _node == x._node;
}
};
template<class T>
struct list_const_iterator
{
typedef list_node<T> Node;
typedef list_const_iterator<T> Self;
Node* _node;
list_const_iterator(Node* node)
:_node(node)
{}
const T& operator*()
{
return _node->_data;
}
const T* operator&()
{
return _node->_data;
}
Self& operator++()
{
_node = _node->_next;
return *this;
}
Self& operator--()
{
_node = _node->_prev;
return *this;
}
bool operator!=(const Self& x)
{
return _node != x._node;
}
bool operator==(const Self& x)
{
return _node == x._node;
}
};
template<class T>
class list
{
typedef list_node<T> Node;
public:
typedef list_iterator<T> iterator;
typedef list_const_iterator<T> const_iterator;
void clear()
{
auto it = begin();
while (it != end())
{
it = erase(it);//erase以后会返回下一个位置
}
}
void emtpy_init()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
_size = 0;
}
list()
{
emtpy_init();
}
//拷贝构造
list(const list<T>& lt)
{
emtpy_init();
for (auto& e : lt)
{
push_back(e);
}
}
list<T>& operator=(list<T> lt)
{
swap(lt);
return *this;
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
iterator begin()
{
/*iterator it(_head->_next);
return it;*/
/* return iterator(_head->_next); 匿名对象*/
return _head->_next;//接收指针,但是返回会隐式转换成iterator类型
}
iterator end()
{
return _head;
}
const_iterator begin() const
{
/*iterator it(_head->_next);
return it;*/
/* return iterator(_head->_next); 匿名对象*/
return _head->_next;//接收指针,但是返回会隐式转换成iterator类型
}
const_iterator end() const
{
return _head;
}
size_t size() const
{
return _size;
}
void swap(list<int>& it)
{
std::swap(_head, it._head);
std::swap(_size, it._size);
}
bool empty() const
{
return _size == 0;
}
void push_back(const T& x)
{
/*Node* newnode = new Node(x);
Node* tail = _head->_prev;
tail->_next = newnode;
newnode->_prev = tail;
newnode->_next = _head;
_head->_prev = newnode;
++_size;*/
insert(end(), x);
}
void push_front(const T& x)
{
insert(begin(), x);
}
void pop_front()
{
erase(begin());
}
void pop_back()
{
erase(--end());
}
iterator erase(iterator pos)
{
assert(pos != end());//不能把头节点删了
Node* prev = pos._node->_prev;
Node* next = pos._node->_next;
prev->_next = next;
next->_prev = prev;
--_size;
return next;
}
iterator insert(iterator pos, const T& x)
{
//prev newnode cur
Node* newnode = new Node(x);
Node* prev = pos._node->_prev;
Node* cur = pos._node;
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = cur;
cur->_prev = newnode;
++_size;
return newnode;
}
private:
Node* _head;
size_t _size;
};
template<class Container>
void print_container(const Container& v)
{
list<int>::const_iterator it = v.begin();
while (it != v.end())
{
//*it += 10;
cout << *it << " ";
++it;
}
cout << endl;
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
}
void test_list01()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
print_container(lt);
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
}
test.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include<string>
#include <algorithm>
#include <list>
using namespace std;
void test_list1()
{
string s("sgfaiug");
cout << s << endl;
sort(s.begin(), s.end());
cout << s << endl;
}
void test_list2()
{
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;
auto it = lt.begin();
int k = 3;
while (k--)
{
++it;
}
lt.insert(it, 30);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
int x = 0;
cin >> x;
it = find(lt.begin(), lt.end(), x);
if (it != lt.end())//迭代器从前往后找,左闭右开,等于end就是没找到
{
lt.erase(it);
}
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
#include "list.h"
int main()
{
//test_list2();
rq::test_list01();
return 0;
}