#pragma once
#include<assert.h>
//#include<iostream>
//using std::cout;
//using std::cin;
//using std::endl;
namespace bit
{
template<class T>
struct __list_node
{
__list_node<T>* _next;
__list_node<T>* _prev;
T _data;
__list_node(const T& x = T())
:_data(x)
, _next(nullptr)
, _prev(nullptr)
{
}
};
//__list_iterator<T,T&,T*> ->iterator
//__list_iterator<T,const T&,const T*> ->const_iterator
template<class T,class Ref,class Ptr>
struct __list_iterator
{
typedef __list_node<T> Node;
typedef __list_iterator<T, Ref, Ptr> Self;
Node* _node;
__list_iterator(Node* node)
:_node(node)
{}
Ptr operator->()
{
return &_node->_data;
}
Ref operator*()
{
return _node->_data;
}
//++it
Self& operator++() {
_node = _node->_next;
return *this;
}
//it++
Self operator++(int)
{
Self temp(*this);
_node = _node->_next;
return temp;
}
//--it
Self& operator--() {
_node = _node->_prev;
return *this;
}
//it--
Self operator--(int)
{
Self temp(*this);
_node = _node->_prev;
return temp;
}
//it != end()
bool operator!=(const Self& it) {
return _node != it._node;
}
//it == end()
bool operator==(const Self& it) {
return _node == it._node;
}
};
template<class T>
class list
{
typedef __list_node<T> Node;
public:
typedef __list_iterator<T,T&,T*> iterator;
typedef __list_iterator<T, const T&, const T*> const_iterator;
iterator begin()
{
return iterator(_head->_next);
}
iterator end()
{
return iterator(_head);
}
const_iterator begin()const
{
return const_iterator(_head->_next);
}
const_iterator end()const
{
return const_iterator(_head);
}
//带头双向循环链表
list()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
//拷贝构造函数
list(const list<T>& It) {
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
for(auto e:It)
push_back(e)
/*const_iterator it = It.begin();
while (it != It.end()) {
this->push_back(*it);
++it;
}*/
}
list<T>& operator=(const list<T>& It) {
if (this != &It)
{
clear();
for (auto e : It)
push_back(e);
}
return *this;
}
//现代写法:
/*list<T>& operator=(list<T>& It)
{
swap(_head, It._head);
return *this;
}*/
~list()
{
clear();
delete _head;
_head = nullptr;
}
void clear() {
iterator it = begin();
while (it != end()) {
erase(it++);
}
}
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());
}
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;
}
void erase(iterator pos) {
assert(pos != end());//不可以删除头结点
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
delete cur;
prev->_next = next;
next->_prev = prev;
}
private:
Node* _head;
};
void print_list(const list<int>& a) {
list<int>::const_iterator it = a.begin();
while (it != a.end()) {
cout << (*it) << " ";
++it;
}
cout << endl;
}
void test_list1()
{
list<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(4);
list<int> b(a);
print_list(b);
}
//void test_list1()
//{
// list<int> a;
// a.push_back(1);
// a.push_back(2);
// a.push_back(3);
// a.push_back(4);
// list<int>::iterator it = a.begin();
// while (it != a.end()) {
// cout << *it << " ";
// ++it;
// }
// cout << endl;
//}
//struct Date
//{
// int _year = 0;
// int _month = 1;
// int _day = 1;
//};
//void test_list2() {
// list<Date> a;
// a.push_back(Date());
// //这里为什么用匿名对象啊啊啊啊??
// a.push_back(Date());
// list<Date>::iterator it = a.begin();
// while (it != a.end()) {
// cout << it->_year << "-" << it->_month << "-" << it->_day << endl;
// //cout << (*it)._year << "-" << (*it)._month << "-" << (*it)._day << endl;
// ++it;
// }
//}
}
STL---list实现(C++)
最新推荐文章于 2024-09-01 16:58:04 发布