diff --git a/TinySTL/List.h b/TinySTL/List.h index 2bcd7d9..8cc9ed4 100644 --- a/TinySTL/List.h +++ b/TinySTL/List.h @@ -1,9 +1,84 @@ #ifndef _LIST_H_ #define _LIST_H_ +#include "Allocator.h" +#include "UninitializedFunctions.h" + namespace TinySTL{ template - class List{}; + class List; + namespace{ + //the class of node + template + struct node{ + T data; + node *next; + List *container; + node(const T& d, node *n, List *c): + data(d), node(n), container(c){} + bool operator ==(const node& n){ + return data == n.data && next == n.next && container == n.container; + } + }; + //the class of list iterator + template + struct listIterator{ + template + friend class List; + private: + typedef node* nodePtr; + nodePtr p; + public: + explicit listIterator(nodePtr ptr = nullptr) :p(ptr){} + + listIterator& operator++(){ + p = p->next; + return *this; + } + listIterator operator++(int){ + auto res = *this; + ++*this; + return res; + } + T& operator *(){ return p->data; } + T* operator &(){ return &(operator*()); } + bool operator ==(const listIterator& it){ + return *p == *(it.p); + } + bool operator !=(const listIterator& it){ + return !(*this == it); + } + }; + }//end of namespace + + //the class of List + template + class List{ + private: + typedef allocator> nodeAllocator; + typedef node *nodePtr; + public: + typedef T value_type; + typedef listIterator iterator; + typedef T& reference; + typedef size_t size_type; + private: + iterator head; + iterator tail; + public: + List(){ + head.p = newNode();//add a dummy node + tail.p = head.p; + } + private: + nodePtr newNode(const T& val = T()){ + nodePtr res = nodeAllocator::allocate(); + res->container = this; + res->data = val; + res->next = nullptr; + return res; + } + }; } #endif \ No newline at end of file