文件
#pragma once
#include <assert.h>
namespace mylist
{
template <typename T>
struct __list_node
{
__list_node(const T& x = T())
: _prev(nullptr)
, _next(nullptr)
, _data(x)
{}
__list_node<T>* _prev;
__list_node<T>* _next;
T _data;
};
template <class T, class Ref, class Ptr>
struct __list_iterator
{
typedef __list_node<T> node;
typedef __list_iterator<T, Ref, Ptr> self;
__list_iterator(node* it_node)
{
node_iterator = it_node;
}
Ref operator*()
{
return node_iterator->_data;
}
Ptr operator->()
{
return &(operator*());
}
self& operator++()
{
node_iterator = node_iterator->_next;
return *this;;
}
self operator++(int)
{
self tmp(*this);
node_iterator = node_iterator->_next;
return tmp;;
}
self& operator--()
{
node_iterator = node_iterator->_prev;
return *this;;
}
self operator--(int)
{
self tmp(*this);
node_iterator = node_iterator->_prev;
return tmp;;
}
bool operator!=(const self& x)
{
return node_iterator != x.node_iterator;
}
bool operator==(const self& x)
{
return node_iterator == x.node_iterator;
}
node* node_iterator;
};
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()
{
iterator tmp(_head->_next);
return tmp;
}
iterator end()
{
iterator tmp(_head);
return tmp;
}
const_iterator begin() const
{
const_iterator tmp(_head->_next);
return tmp;
}
const_iterator end() const
{
const_iterator tmp(_head);
return tmp;
}
void empyt_init()
{
_head = new node;
_head->_prev = _head->_next = _head;
}
list()
{
empyt_init();
}
template <class InputIterator>
list(InputIterator first, InputIterator last)
{
empyt_init();
while (first != last)
{
push_back(*first);
first++;
}
}
void swap(list<T>& x)
{
std::swap(_head, x._head);
}
list(const list<T>& x)
{
empyt_init();
list<T> tmp(x.begin(), x.end());
swap(tmp);
}
list<T>& operator=(list<T> x)
{
swap(x);
return *this;
}
void push_front(const T& x)
{
Insert(begin(), x);
}
void push_back(const T& x)
{
node* new_node = new node(x);
_head->_prev->_next = new_node;
new_node->_prev = _head->_prev;
_head->_prev = new_node;
new_node->_next = _head;
}
void Insert(iterator pos, const T& x)
{
node* new_node = new node(x);
node* pre = pos.node_iterator->_prev;
node* cur = pos.node_iterator;
pre->_next = new_node;
new_node->_prev = pre;
new_node->_next = cur;
cur->_prev = new_node;
}
void pop_front()
{
Erase(begin());
}
void pop_back()
{
Erase(--end());
}
iterator Erase(iterator pos)
{
assert(pos != end());
node* prev = pos.node_iterator->_prev;
node* next = pos.node_iterator->_next;
prev->_next = next;
next->_prev = prev;
delete pos.node_iterator;
return iterator(next);
}
void clear()
{
iterator it = begin();
while (it != end())
{
Erase(it++);
}
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
private:
node* _head;
};
}
测试
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include "list.h"
using namespace mylist;
void printlist(const list<int> l2)
{
list<int>::const_iterator it = l2.begin();
while (it != l2.end())
{
std::cout << *it << " ";
it++;
}
}
class A
{
public:
A(int a = 1, int b = 6)
{
_a = a;
_b = b;
}
A(const A& x)
{
}
int _a;
int _b;
};
int main()
{
list<int> l1;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
list<int>::iterator it = l1.begin();
while (it != l1.end())
{
std::cout << *it << " ";
it++;
}
std::cout << std::endl;
return 0;
}