linkedlist.h
CXX99(native*)
#ifndef LINKEDLIST_H #define LINKEDLIST_H #include templateclass LinkedList; template struct LinkedListNode { public: LinkedListNode * Previous; LinkedListNode * Next; T Value; LinkedList * LinkedList; }; template class LinkedList { private: LinkedListNode * m_first; LinkedListNode * m_last; int m_count; public: LinkedList() { this->m_count = 0; this->m_first = NULL; this->m_last = NULL; } LinkedListNode * First(); LinkedListNode * Last(); int Count(); bool AddFirst(LinkedListNode * value); bool AddLast(LinkedListNode * value); bool AddAfter(LinkedListNode * node, LinkedListNode * value); bool AddBefore(LinkedListNode * node, LinkedListNode * value); bool RemoveFirst(); bool RemoveLast(); bool Remove(LinkedListNode * node); LinkedListNode * Find(T value); void Clear(); }; #endif template inline LinkedListNode * LinkedList ::First() { return this->m_first; } template inline LinkedListNode * LinkedList ::Last() { return this->m_last; } template inline int LinkedList ::Count() { return this->m_count; } template inline bool LinkedList ::AddFirst(LinkedListNode * value) { if (value == NULL) { return false; } value->LinkedList = NULL; value->Next = NULL; value->Previous = NULL; if (this->m_last == NULL) { this->m_last = value; this->m_first = value; this->m_count = 0; } else { LinkedListNode * current = this->m_first; value->Next = current; current->Previous = value; this->m_first = value; } this->m_count++; value->LinkedList = this; return true; } template inline bool LinkedList ::AddLast(LinkedListNode * node) { if (node == NULL) { return false; } node->LinkedList = NULL; node->Next = NULL; node->Previous = NULL; if (this->m_last == NULL) { this->m_first = node; this->m_last = node; this->m_count = 0; this->m_count++; node->LinkedList = this; return true; } else { return this->AddAfter(this->m_last, node); } } template inline bool LinkedList ::AddAfter(LinkedListNode * node, LinkedListNode * value) { if (node == NULL || value == NULL) { return false; } value->LinkedList = NULL; value->Next = NULL; value->Previous = NULL; LinkedListNode * current = node->Next; node->Next = value; if (current != NULL) { current->Previous = value; } value->Previous = node; value->Next = current; if (node == this->m_last) { this->m_last = value; } this->m_count++; value->LinkedList = this; return true; } template inline bool LinkedList ::AddBefore(LinkedListNode * node, LinkedListNode * value) { if (node == NULL || value == NULL) { return false; } value->LinkedList = NULL; value->Next = NULL; value->Previous = NULL; LinkedListNode current = node->Previous; if (current == NULL) { return this->AddFirst(value); } current.Next = value; node->Previous = value; value->Next = node; value->Previous = current; if (node == this->m_first) { this->m_first = value; } this->m_count++; value->LinkedList = this; return true; } template inline bool LinkedList ::RemoveFirst() { LinkedListNode * first = this->m_first; if (first == NULL) { return false; } LinkedListNode * current = first->Next; first->Previous = NULL; first->LinkedList = NULL; first->Next = NULL; if (current != NULL) { current->Previous = NULL; } this->m_count--; if (this->m_count <= 0) { this->m_count = 0; this->m_first = NULL; this->m_last = NULL; current = NULL; } this->m_first = current; return true; } template inline bool LinkedList ::RemoveLast() { LinkedListNode * last = this->m_last; if (last == NULL) { return false; } LinkedListNode * current = last->Previous; last->Previous = NULL; last->LinkedList = NULL; last->Next = NULL; if (current != NULL) { current->Next = NULL; } this->m_count--; if (this->m_count <= 0) { this->m_count = 0; this->m_first = NULL; this->m_last = NULL; current = NULL; } this->m_last = current; return true; } template inline bool LinkedList ::Remove(LinkedListNode * node) { if (node == NULL) { return false; } if (node == this->m_first) { return this->RemoveFirst(); } if (node == this->m_last) { return this->RemoveLast(); } LinkedListNode * previous = node->Previous; LinkedListNode * next = node->Next; previous->Next = next; next->Previous = previous; this->m_count--; if (this->m_count <= 0) { this->m_count = 0; this->m_first = NULL; this->m_last = NULL; } node->Next = NULL; node->Previous = NULL; node->LinkedList = NULL; return true; } template inline LinkedListNode * LinkedList ::Find(T value) { LinkedListNode * i = this->m_first; while (i != NULL) { if (i->Value == value) { return i; } i = i->Next; } return NULL; } template inline void LinkedList ::Clear() { LinkedListNode * i = this->m_first; while (i != NULL) { LinkedListNode * j = i->Next; { i->LinkedList = NULL; i->Next = NULL; i->Previous = NULL; } i = j; } this->m_first = NULL; this->m_count = 0; this->m_last = NULL; }
CXX11(std::shared_ptr<....>)
#ifndef LINKEDLIST_H #define LINKEDLIST_H #includetemplate class LinkedList; template struct LinkedListNode { public: std::shared_ptr > Previous; std::shared_ptr > Next; T Value; LinkedList * LinkedList_; }; template class LinkedList { private: std::shared_ptr > m_first; std::shared_ptr > m_last; int m_count; public: LinkedList() { this->m_count = 0; this->m_first = NULL; this->m_last = NULL; } std::shared_ptr > First(); std::shared_ptr > Last(); int Count(); bool AddFirst(std::shared_ptr > value); bool AddLast(std::shared_ptr > value); bool AddAfter(std::shared_ptr > node, std::shared_ptr > value); bool AddBefore(std::shared_ptr > node, std::shared_ptr > value); bool RemoveFirst(); bool RemoveLast(); bool Remove(std::shared_ptr > node); std::shared_ptr > Find(T value); void Clear(); }; #endif template inline std::shared_ptr > LinkedList ::First() { return this->m_first; } template inline std::shared_ptr > LinkedList ::Last() { return this->m_last; } template inline int LinkedList ::Count() { return this->m_count; } template inline bool LinkedList ::AddFirst(std::shared_ptr > value) { if (value == NULL) { return false; } value->LinkedList_ = NULL; value->Next = NULL; value->Previous = NULL; if (this->m_last == NULL) { this->m_last = value; this->m_first = value; this->m_count = 0; } else { std::shared_ptr > current = this->m_first; value->Next = current; current->Previous = value; this->m_first = value; } this->m_count++; value->LinkedList_ = this; return true; } template inline bool LinkedList ::AddLast(std::shared_ptr > node) { if (node == NULL) { return false; } node->LinkedList_ = NULL; node->Next = NULL; node->Previous = NULL; if (this->m_last == NULL) { this->m_first = node; this->m_last = node; this->m_count = 0; this->m_count++; node->LinkedList_ = this; return true; } else { return this->AddAfter(this->m_last, node); } } template inline bool LinkedList ::AddAfter(std::shared_ptr > node, std::shared_ptr > value) { if (node == NULL || value == NULL) { return false; } value->LinkedList_ = NULL; value->Next = NULL; value->Previous = NULL; std::shared_ptr > current = node->Next; node->Next = value; if (current != NULL) { current->Previous = value; } value->Previous = node; value->Next = current; if (node == this->m_last) { this->m_last = value; } this->m_count++; value->LinkedList_ = this; return true; } template inline bool LinkedList ::AddBefore(std::shared_ptr > node, std::shared_ptr > value) { if (node == NULL || value == NULL) { return false; } value->LinkedList_ = NULL; value->Next = NULL; value->Previous = NULL; LinkedListNode current = node->Previous; if (current == NULL) { return this->AddFirst(value); } current.Next = value; node->Previous = value; value->Next = node; value->Previous = current; if (node == this->m_first) { this->m_first = value; } this->m_count++; value->LinkedList_ = this; return true; } template inline bool LinkedList ::RemoveFirst() { std::shared_ptr > first = this->m_first; if (first == NULL) { return false; } std::shared_ptr > current = first->Next; first->Previous = NULL; first->LinkedList_ = NULL; first->Next = NULL; if (current != NULL) { current->Previous = NULL; } this->m_count--; if (this->m_count <= 0) { this->m_count = 0; this->m_first = NULL; this->m_last = NULL; current = NULL; } this->m_first = current; return true; } template inline bool LinkedList ::RemoveLast() { std::shared_ptr > last = this->m_last; if (last == NULL) { return false; } std::shared_ptr > current = last->Previous; last->Previous = NULL; last->LinkedList_ = NULL; last->Next = NULL; if (current != NULL) { current->Next = NULL; } this->m_count--; if (this->m_count <= 0) { this->m_count = 0; this->m_first = NULL; this->m_last = NULL; current = NULL; } this->m_last = current; return true; } template inline bool LinkedList ::Remove(std::shared_ptr > node) { if (node == NULL) { return false; } if (node == this->m_first) { return this->RemoveFirst(); } if (node == this->m_last) { return this->RemoveLast(); } std::shared_ptr > previous = node->Previous; std::shared_ptr > next = node->Next; previous->Next = next; next->Previous = previous; this->m_count--; if (this->m_count <= 0) { this->m_count = 0; this->m_first = NULL; this->m_last = NULL; } node->Next = NULL; node->Previous = NULL; node->LinkedList_ = NULL; return true; } template inline std::shared_ptr > LinkedList ::Find(T value) { std::shared_ptr > i = this->m_first; while (i != NULL) { if (i->Value == value) { return i; } i = i->Next; } return NULL; } template inline void LinkedList ::Clear() { std::shared_ptr > i = this->m_first; while (i != NULL) { std::shared_ptr > j = i->Next; { i->LinkedList_ = NULL; i->Next = NULL; i->Previous = NULL; } i = j; } this->m_first = NULL; this->m_count = 0; this->m_last = NULL; }



