LinkedListNode.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #pragma once
  2. #include <iostream>
  3. // Forward declaration of LinkedList
  4. template <typename T> class LinkedList;
  5. /// <summary>
  6. /// Template class to allow the LinkedListNode to store any data type
  7. /// </summary>
  8. /// <typeparam name="T">Value type</typeparam>
  9. template <typename T>
  10. class LinkedListNode {
  11. private:
  12. T _value; // Value of the node
  13. LinkedListNode* _next; // Pointer to the next node in the linked list
  14. public:
  15. /// <summary>
  16. /// Linked list item constructor
  17. /// </summary>
  18. /// <param name="value">The value of the node</param>
  19. /// <param name="next">The next node in the linked list</param>
  20. LinkedListNode(const T& value, LinkedListNode* next);
  21. /// <summary>
  22. /// Retrieve the value of the node
  23. /// </summary>
  24. /// <returns>The respective value for the node</returns>
  25. const T& getValue() const;
  26. /// <summary>
  27. /// Retrieve next node in the linked list
  28. /// </summary>
  29. /// <returns>Next linked node</returns>
  30. LinkedListNode* getNext();
  31. /// <summary>
  32. /// Assigns the item new value
  33. /// </summary>
  34. /// <param name="other">The value to be replaced with</param>
  35. /// <returns>New linked list item</returns>
  36. LinkedListNode<T>& operator=(const LinkedListNode<T>& other);
  37. template <typename U> friend class LinkedList;
  38. };
  39. // Implementation of LinkedListNode
  40. template <typename T>
  41. LinkedListNode<T>::LinkedListNode(const T& value, LinkedListNode* next)
  42. : _value(value), _next(next) {}
  43. template<typename T>
  44. const T& LinkedListNode<T>::getValue() const {
  45. return _value;
  46. }
  47. template <typename T>
  48. LinkedListNode<T>* LinkedListNode<T>::getNext() {
  49. return _next;
  50. }
  51. template <typename T>
  52. LinkedListNode<T>& LinkedListNode<T>::operator=(const LinkedListNode<T>& other) {
  53. if (this != &other) {
  54. _value = other._value;
  55. _next = other._next;
  56. }
  57. return *this;
  58. }