///////////////////////////////////////////////////////////////////// // Created by [Dennis Andersen] [2013] ///////////////////////////////////////////////////////////////////// #ifndef MISC_LINKED_LIST_H #define MISC_LINKED_LIST_H namespace Utility { namespace DynamicMemory { template class LinkedList { private: class Node { public: Type data; Node *next; Node() :next(0) { } Node(Type t) :next(0), data(t) { } ~Node() { next = 0; } }; public: LinkedList(); ~LinkedList(); Type& operator[](int index); void PushFront(Type value); Type PopFront(); void PushBack(Type value); Type PopBack(); Type PeakFront(); Type PeakBack(); void Remove(Type value); bool IsEmpty(); int Size(); void Clear(); private: void Destroy(Node* n); private: Node *first; Node *last; int nrOfNodes; }; template LinkedList::LinkedList() { this->first = 0; this->last = 0; this->nrOfNodes = 0; } template LinkedList::~LinkedList() { this->last = 0; this->Destroy(this->first); } template Type& LinkedList::operator[](int index) { if(this->index >= this->nrofNodes || index < 0) { throw; } int i = 0; LinkedList::Node* walker = this->first; while (i != index) { walker = walker->next; i++; } return walker->data; } template void LinkedList::PushFront(Type value) { Node *n = new Node(value); n->next = this->first; this->first = n; if(!this->nrOfNodes) this->back = this->front; this->nrOfNodes++; } template Type LinkedList::PopFront() { if(!this->first) { throw; } Type temp = this->first->data; Node* head = this->first; this->first = this->first->next; delete head; this->nrOfNodes--; return temp; } template void LinkedList::PushBack(Type value) { Node* n = new Node(value); if(!this->first) { this->first = n; this->last = n; } else { this->last->next = n; this->last = n; } this->nrOfNodes++; } template Type LinkedList::PopBack() { if(!this->first) throw; Type val = this->last->data; Node* walker = this->first; while (walker) { if(walker->next == this->last) { delete this->last; this->last = walker; walker = 0; this->nrOfNodes--; } } } template Type LinkedList::PeakFront() { if(!this->front) throw; return this->first->data; } template Type LinkedList::PeakBack() { if(!this->last) throw; return this->last->data; } template void LinkedList::Remove(Type value) { Node *w = this->first; if(this->nrOfNodes == 1) //Sepcial case { //First and last is equal if(this->first->data == value) { delete this->first; this->first = 0; this->last = 0; this->nrOfNodes--; } } else { Node *prev = 0; while (w) { if(w->data == value) { if(w == this->first) { this->first = this->first->next; delete w; w = 0; } else if (w == this->last) { this->last = prev; delete w; w = 0; } else { prev->next = w->next; delete w; w = 0; } } else { prev = w; w = w->next; } } } } template bool LinkedList::IsEmpty() { return (this->nrOfNodes == 0); } template int LinkedList::Size() { return this->nrOfNodes; } template void LinkedList::Clear() { this->Destroy(this->first); } template void LinkedList::Destroy(Node* n) { if(n) { Destroy(n->next); delete n; } } } } #endif // !MISC_LINKED_LIST_H