1.基本框架
namespace Apex
{
template<class T>
struct list_node
{
T _data;
list_node<T>* _next;
list_node<T>* _prev;
list_node(const T& data = T());
};
template<class T, class Ref, class Ptr>
struct __list_iterator
{
typedef list_node<T> Node;
typedef __list_iterator<T, Ref, Ptr> iterator;
Node* _node;
__list_iterator(Node* node);
Ref operator*();
Ptr operator->();
iterator& operator++();
iterator operator++(int);
iterator& operator--();
iterator operator--(int);
bool operator==(const iterator& it);
bool operator!=(const iterator& it);
};
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();
const_iterator begin() const;
iterator end();
const_iterator end() const;
list();
list(size_t n, const T& val = T());
template<class InputIterator>
list(InputIterator first, InputIterator last);
void clear();
~list();
list(const list<T>& lt);
list<T>& operator=(list<T> lt);
void insert(iterator pos, const T& x);
iterator erase(iterator pos);
void push_back(const T& x);
void push_front(const T& x);
void pop_back();
void pop_front();
private:
Node* _head;
};
}
2.list.h
#include
#include
using namespace std;
namespace apex
{
template<class T>
struct list_node
{
T _data;
list_node<T>* _next;
list_node<T>* _prev;
list_node(const T& data = T())
: _data(data)
, _next(nullptr)
, _prev(nullptr)
{
}
};
template<class T, class Ref, class Ptr>
struct __list_iterator
{
typedef list_node<T> Node;
typedef __list_iterator<T, Ref, Ptr> iterator;
typedef T value_type;
typedef Ref reference;
typedef Ptr pointer;
Node* _node;
__list_iterator(Node* node)
: _node(node)
{
}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
iterator& operator++()
{
_node = _node->_next;
return *this;
}
iterator operator++(int)
{
iterator tmp(*this);
_node = _node->_next;
return tmp;
}
iterator& operator--()
{
_node = _node->_prev;
return *this;
}
iterator operator--(int)
{
iterator tmp(*this);
_node = _node->_prev;
return tmp;
}
bool operator==(const iterator& it)
{
return _node == it._node;
}
bool operator!=(const iterator& 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);
}
void empty_init()
{
_head = new Node();
_head->_next = _head;
_head->_prev = _head;
}
list()
{
empty_init();
}
list(size_t n, const T& val = T())
{
empty_init();
for (size_t i = 0; i < n; i++)
{
push_back(val);
}
}
template<class InputIterator>
list(InputIterator first, InputIterator last)
{
empty_init();
while (first != last)
{
push_back(*first);
first++;
}
}
void clear()
{
iterator it = begin();
while (it != end())
{
erase(it++);
}
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
void swap(list<T>& x)
{
std::swap(_head, x._head);
}
list(const list<T>& lt)
{
empty_init();
list<T> tmp(lt.begin(), lt.end());
swap(tmp);
}
list<T>& operator=(list<T> lt)
{
swap(lt);
return *this;
}
void insert(iterator pos, const T& x)
{
Node* cur = pos._node;
Node* prv = cur->_prev;
Node* newnode = new Node(x);
prv->_next = newnode;
newnode->_prev = prv;
newnode->_next = cur;
cur->_prev = newnode;
}
iterator erase(iterator pos)
{
assert(pos != end());
Node* cur = pos._node;
Node* prv = cur->_prev;
Node* next = cur->_next;
delete cur;
prv->_next = next;
next->_prev = prv;
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());
}
private:
Node* _head;
};
void print_list(const list<int>& lt)
{
list<int>::const_iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
it++;
}
cout << endl;
}
void test_list1()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
*it *= 2;
cout << *it << " ";
it++;
}
cout << endl;
}
void test_list2()
{
list<int> lt;
lt.push_back(2);
lt.push_back(4);
lt.push_back(6);
lt.push_back(8);
print_list(lt);
}
struct Date
{
int _year;
int _month;
int _day;
Date(int year = 1, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{
}
};
void test_list3()
{
list<Date> lt;
lt.push_back(Date(2023, 7, 21));
lt.push_back(Date(2023, 7, 22));
lt.push_back(Date(2023, 7, 23));
list<Date>::iterator it = lt.begin();
while (it != lt.end())
{
cout << it->_year << "-" << it->_month << "-" << it->_day << endl;
it++;
}
cout << endl;
}
void test_list4()
{
list<int> lt1;
lt1.push_back(1);
lt1.push_back(2);
lt1.push_back(3);
list<int> lt2(lt1);
for (auto e : lt2)
cout << e << " ";
}
void test_list5()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
print_list(lt);
lt.clear();
print_list(lt);
}
}
3.test.c
#define _CRT_SECURE_NO_WARNINGS
#include
#include "list.h"
int main(void)
{
apex::test_list1();
return 0;
}