#pragma once
#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
#include <assert.h>
using namespace std;
namespace TT
{
template<class 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<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)
{}
Ref operator*()
{
return node->data;
}
Ptr operator->()
{
return &node->data;
}
Self& operator++()
{
node = node->next;
return *this;
}
Self operator++(int)
{
Self tmp(*this);
node = node->next;
return tmp;
}
Self& operator--()
{
node = node->prev;
return *this;
}
Self operator--(int)
{
Self tmp(*this);
node = node->prev;
return tmp;
}
bool operator!=(const Self& it)
{
return node != it.node;
}
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);
}
size_t size()
{
return _size;
}
size_t size() const
{
return _size;
}
void empty_init()
{
head = new Node;
head->prev = head;
head->next = head;
_size = 0;
}
list()
{
empty_init();
}
list(initializer_list<T> lt)
{
empty_init();
for (auto& e : lt)
{
push_back(e);
}
}
list(const list<T>& lt)
{
empty_init();
for (auto e : lt)
{
push_back(e);
}
}
void swap(const list<T>& lt)
{
std::swap(head, lt.head);
std::swap(_size, lt._size);
}
list<T>& operator=(const list<T> lt)
{
swap(lt);
return *this;
}
void push_back(const T& x)
{
insert(end(), x);
}
void push_front(const T& x)
{
insert(begin(), x);
}
void pop_front()
{
erase(begin());
}
void pop_back()
{
erase(--end());
}
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;
++_size;
}
iterator erase(iterator pos)
{
assert(pos != end());
Node* cur = pos.node;
Node* PrevNode = cur->prev;
Node* NextNode = cur->next;
PrevNode->next = NextNode;
NextNode->prev = PrevNode;
delete cur;
--_size;
return iterator(NextNode);
}
~list()
{
clear();
delete head;
head = nullptr;
}
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);
}
}
private:
Node* head;
size_t _size;
};
}
#include "list.h"
#include<algorithm>
struct A
{
A(int a1 = 1, int a2 = 1)
:_a1(a1)
,_a2(a2)
{ }
int _a1;
int _a2;
};
void list_test1()
{
TT::list<int> lt1;
lt1.push_back(1);
lt1.push_back(2);
lt1.push_back(3);
lt1.push_back(4);
TT::list<int>::iterator it = lt1.begin();
while (it != lt1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
void Print(const TT::list<A>& lt)
{
TT::list<A>::const_iterator it = lt.begin();
while (it != lt.end())
{
cout << (*it)._a1 << " " << (*it)._a2 << endl;;
++it;
}
cout << endl;
}
void list_test2()
{
TT::list<A> lt1;
lt1.push_back({ 1,1 });
lt1.push_back({ 2,2 });
lt1.push_back({ 3,3 });
lt1.push_back({ 4,4 });
TT::list<A>::iterator it1 = lt1.begin();
while (it1 != lt1.end())
{
it1->_a1 += 1;
cout << it1->_a1 << ":" << it1->_a2 << endl;
++it1;
}
cout << endl;
Print(lt1);
}
void list_test3()
{
TT::list<int> lt1;
lt1.push_back(1);
lt1.push_back(2);
lt1.push_back(3);
lt1.push_back(4);
for (auto e : lt1)
{
cout << e << " ";
}
cout << endl;
lt1.push_back(78);
lt1.push_back(99);
lt1.push_front(100);
lt1.push_front(300);
for (auto e : lt1)
{
cout << e << " ";
}
cout << endl;
lt1.pop_back();
lt1.pop_front();
for (auto e : lt1)
{
cout << e << " ";
}
cout << endl;
}
void test_list4()
{
TT::list<int> lt1;
lt1.push_back(1);
lt1.push_back(2);
lt1.push_back(3);
lt1.push_back(4);
for (auto e : lt1)
{
cout << e << " ";
}
cout << endl;
TT::list<int> lt2(lt1);
lt1.clear();
for (auto e : lt1)
{
cout << e << " ";
}
cout << endl;
for (auto e : lt2)
{
cout << e << " ";
}
cout << endl;
TT::list<int> lt3 = lt2;
for (auto e : lt3)
{
cout << e << " ";
}
cout << endl;
TT::list<int> lt4 = { 10,20,30,40 };
for (auto e : lt4)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
test_list4();
return 0;
}