Skip to main content
added 378 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Your Node implementation is atypical of a common implementation. A node is really just a part of the linked list and just needs two data members: its data and a pointer to the next node (and a pointer to the previous node for a doubly-linked list). It does not need its own member functions, but it could also have its own constructor, instead of having the list itself take care of it.

template <typename T>
struct Node
{
    T data;
    Node* next;

    Node(T data) : data(data), next(nullptr) {}
};

Although this can be a class, it's okay to make it a struct because it can just be defined inside of the LinkedList class, safely as a private data member.

Misc.:

  • You should remove <iostream> since it's unused, unless you're going to implement a display function in LinkedList. If you overload operator<< instead, then you'll just need <ostream>. Regardless, this doesn't belong to Node.

  • getCount() should be const as it's not supposed to modify any data members:

      int getCount() const
      { 
          return _count;
      }
    

Your Node implementation is atypical of a common implementation. A node is really just a part of the linked list and just needs two data members: its data and a pointer to the next node (and a pointer to the previous node for a doubly-linked list). It does not need its own member functions, but it could also have its own constructor, instead of having the list itself take care of it.

template <typename T>
struct Node
{
    T data;
    Node* next;

    Node(T data) : data(data), next(nullptr) {}
};

Although this can be a class, it's okay to make it a struct because it can just be defined inside of the LinkedList class, safely as a private data member.

Your Node implementation is atypical of a common implementation. A node is really just a part of the linked list and just needs two data members: its data and a pointer to the next node (and a pointer to the previous node for a doubly-linked list). It does not need its own member functions, but it could also have its own constructor, instead of having the list itself take care of it.

template <typename T>
struct Node
{
    T data;
    Node* next;

    Node(T data) : data(data), next(nullptr) {}
};

Although this can be a class, it's okay to make it a struct because it can just be defined inside of the LinkedList class, safely as a private data member.

Misc.:

  • You should remove <iostream> since it's unused, unless you're going to implement a display function in LinkedList. If you overload operator<< instead, then you'll just need <ostream>. Regardless, this doesn't belong to Node.

  • getCount() should be const as it's not supposed to modify any data members:

      int getCount() const
      { 
          return _count;
      }
    
I'll just disregard std::unique_ptr altogether
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Your Node implementation is atypical of a common implementation. A node is really just a part of the linked list and just needs two data members: its data and a pointer to the next node (and a pointer to the previous node for a doubly-linked list). It does not need its own member functions, but it could also have its own constructor, instead of having the list itself take care of it.

template <typename T>
struct Node
{
    T data;
    Node* next;

    Node(T data) : data(data), next(nullptr) {}
};

Although this can be a class, it's okay to make it a struct because it can just be defined inside of the LinkedList class, safely as a private data member. My implementation utilizes a raw pointer, as you seem to be lacking basic memory management. After you become better at it, you can instead utilize the smart pointer std::unique_ptr (only available in C++11).

std::unique_ptr<Node> next;

Your Node implementation is atypical of a common implementation. A node is really just a part of the linked list and just needs two data members: its data and a pointer to the next node (and a pointer to the previous node for a doubly-linked list). It does not need its own member functions, but it could also have its own constructor, instead of having the list itself take care of it.

template <typename T>
struct Node
{
    T data;
    Node* next;

    Node(T data) : data(data), next(nullptr) {}
};

Although this can be a class, it's okay to make it a struct because it can just be defined inside of the LinkedList class, safely as a private data member. My implementation utilizes a raw pointer, as you seem to be lacking basic memory management. After you become better at it, you can instead utilize the smart pointer std::unique_ptr (only available in C++11).

std::unique_ptr<Node> next;

Your Node implementation is atypical of a common implementation. A node is really just a part of the linked list and just needs two data members: its data and a pointer to the next node (and a pointer to the previous node for a doubly-linked list). It does not need its own member functions, but it could also have its own constructor, instead of having the list itself take care of it.

template <typename T>
struct Node
{
    T data;
    Node* next;

    Node(T data) : data(data), next(nullptr) {}
};

Although this can be a class, it's okay to make it a struct because it can just be defined inside of the LinkedList class, safely as a private data member.

added 321 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Node

The Your Node classimplementation is too complicatedatypical of a common implementation. It's A node is really just a part of the linked list, and essentially just needs two data members: its data and a pointer to the next node (and a pointer to the previous node for a doubly-linked list). It does not need any otherits own member functions, but it could also have its own constructor, instead of having the list itself take care of it.

template <typename T>
struct Node
{
    T data;
    std::unique_ptr<Node>Node* next;

    Node(T data) : data(data), next(nullptr) {}
};

That's all it really needs. AlthoughAlthough this can be a class, I've madeit's okay to make it a struct because it can just be defined inside of the the LinkedList class, safely as a private data member. My implementation utilizes a raw pointer, as you seem to be lacking basic memory management. After you become better at it, you can instead utilize the smart pointer std::unique_ptr (only available in C++11).

std::unique_ptr<Node> next;

Node

The Node class is too complicated. It's really just a part of the linked list, and essentially just needs two data members: its data and a pointer to the next node. It does not need any other member functions.

template <typename T>
struct Node
{
    T data;
    std::unique_ptr<Node> next;

    Node(T data) : data(data), next(nullptr) {}
};

That's all it really needs. Although this can be a class, I've made it a struct because it can just be defined inside of the the LinkedList class, safely as a private data member.

Your Node implementation is atypical of a common implementation. A node is really just a part of the linked list and just needs two data members: its data and a pointer to the next node (and a pointer to the previous node for a doubly-linked list). It does not need its own member functions, but it could also have its own constructor, instead of having the list itself take care of it.

template <typename T>
struct Node
{
    T data;
    Node* next;

    Node(T data) : data(data), next(nullptr) {}
};

Although this can be a class, it's okay to make it a struct because it can just be defined inside of the LinkedList class, safely as a private data member. My implementation utilizes a raw pointer, as you seem to be lacking basic memory management. After you become better at it, you can instead utilize the smart pointer std::unique_ptr (only available in C++11).

std::unique_ptr<Node> next;
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading