/** * Copyright (C) 2013-2014 Justin James. * * This license must be preserved. * Any applications, libraries, or code which make any use of any * component of this program must not be commercial, unless explicit * permission is granted from the original author. The use of this * program for non-profit purposes is permitted. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * In the event that this license restricts you from making desired use of this program, contact the original author. * Written by Justin James */ #if !defined _SLLIST_H_HEADER #define _SLLIST_H_HEADER #include "Jupiter.h" #include "List.h" /** * @file SLList.h * @brief Provides a generic Singly Linked List implementation using the List interface. */ namespace Jupiter { /** * @brief Provides a Singly Linked List implementation using the List interface. */ template class SLList : public List { public: /** * @brief Stores a pointer to data, and a pointer to the next node in the list. */ struct Node { Node *next = nullptr; T *data = nullptr; }; /** * @brief Returns the n'th Node in the list. * * @param n Index of the node to return. * @return n'th Node in the list. */ Node *getNode(unsigned int n) const; /** * @brief Gets the data at a specified index. * * @param index Index of the data to get. * @return Data stored at the specified index. */ T *get(unsigned int index) const; /** * @brief Removes the n'th Node in the list, and returns its contents. * * @param n Index of the node to remove. * @return Contents of the node removed. */ T *remove(unsigned int n); /** * @brief Removes the next node in the list. * * @param data Node that preceeds the node to be removed. * @return Contents of the node removed if the node exists, nullptr otherwise. */ T *removeNext(Node *data); /** * @brief Adds data to the list at a specified index. * * @param data Data to add to the list. * @param index Position in the list to add the data to. */ void add(T *data, unsigned int index); /** * @brief Adds data to the front of the list. * * @param data Data to add to the list. */ void add(T *data); /** * @brief Default constructor for the SLList class. */ SLList(); /** * @brief Copy constructor for the SLList class. */ SLList(const SLList &); /** * @brief Destructor for the SLList class. * Note: This does not delete data added to the list. */ ~SLList(); /** Private members */ private: Node *head; }; } // Implementation template Jupiter::SLList::SLList() { Jupiter::SLList::head = new Jupiter::SLList::Node(); Jupiter::List::length = 0; } template Jupiter::SLList::SLList(const Jupiter::SLList &source) { Jupiter::SLList::head = new Jupiter::SLList::Node(); Jupiter::SLList::Node *sourceNode = source.head; head->data = sourceNode->data; sourceNode = sourceNode->next; Jupiter::SLList::Node *n = Jupiter::SLList::head; while (sourceNode != nullptr) { n->next = new Jupiter::SLList::Node(); n = n->next; n->data = sourceNode->data; sourceNode = sourceNode->next; } Jupiter::List::length = source.length; } template Jupiter::SLList::~SLList() { Jupiter::SLList::Node *p; Jupiter::SLList::Node *c = head; do { p = c; c = c->next; delete p; } while (c != nullptr); } template typename Jupiter::SLList::Node *Jupiter::SLList::getNode(unsigned int index) const { Jupiter::SLList::Node *t = head->next; for (unsigned int i = 0; i != index; i++) t = t->next; return t; } template T *Jupiter::SLList::get(unsigned int index) { return Jupiter::SLList::getNode(index)->data; } template const T *Jupiter::SLList::get(unsigned int index) const { return Jupiter::SLList::getNode(index)->data; } template T *Jupiter::SLList::remove(unsigned int index) { Jupiter::SLList::Node *t = head; for (unsigned int i = 0; i != index; i++) t = t->next; Jupiter::SLList::Node *t2 = t->next; T *r = t2->data; delete t2; Jupiter::List::length--; return r; } template T *Jupiter::SLList::removeNext(Jupiter::SLList::Node *data) { Jupiter::SLList::Node *t = data->next; if (t == nullptr) return nullptr; T *r = t->data; data->next = t->next; delete t; Jupiter::List::length--; return r; } template void Jupiter::SLList::add(T *data, unsigned int index) { Jupiter::SLList::Node *n = new Jupiter::SLList::Node(); n->data = data; Jupiter::SLList::Node *t = Jupiter::SLList::head; for (unsigned int i = 0; i < index; i++) t = t->next; n->next = t->next; t->next = n; Jupiter::List::length++; } template void Jupiter::SLList::add(T *data) { Jupiter::SLList::Node *n = new Jupiter::SLList::Node(); n->data = data; n->next = Jupiter::SLList::head->next; Jupiter::SLList::head->next = n; Jupiter::List::length++; } #endif // _SLLIST_H_HEADER