Search code examples
c++linked-listnodes

Parameterized Constructor for Node Based Linked List


I'm in CS2 and we're just learning about linked lists and I have to code the parameterized constructor of a linked list class (node based). I don't really understand the node lists, so any help of what's going on here or how to approach would be helpful! I have the following Node class given:

class Node {

friend class NodeList;

public:
 Node() : m_next(NULL) {}
 Node(const DataType& data, Node* next = NULL) :
        m_next(next), m_data(data) {}
 Node(const Node& other) : m_next(other.m_next),
                           m_data(other.m_data) {}

 DataType& data() { return m_data; }

 const DataType& data() const { return m_data; }

private:
 Node* m_next;
 DataType m_data;
};

and I'm trying to create the parameterized constructor for the following class:

Class NodeList {
    public:
     NodeList();
     NodeList(size_t count, const int value);
    private:
     Node* m_head;
}

, where the parameterized constructor is supposed to have 'count' nodes initialized to 'value'.

Thank you!


Solution

  • Solving a problem like this has 3 parts.

    • Break the problem down into smaller, simpler tasks that make it easier to solve
    • Do the smaller tasks
    • Use them to solve the big problem.

    What would make it easy to solve this problem? Adding one value to a list is easier than adding multiple values to a list, so let's write a function to do that.

    To add one value at the beginning of the list,

    • We can create a new node with that value
    • We have the new node point to the current first node
    • We set the current first node to the new node.

    Let's call our function prepend, since it prepends the value to the list.

    class NodeList {
       public:
        // The head should be set to null initially
        NodeList() 
          : m_head(nullptr) 
        {
        }
        // This function takes a value and adds it to the beginning of the list
        void prepend(const DataType& value) {
            // The new node uses the current head as the next value
            Node* newNode = new Node(value, m_head); 
            // We set the current head to the new node. 
            m_head = newNode; 
        }
    

    Now, adding a value multiple times is easy. We can just call prepend once for each time we want to add an item.

    class NodeList {
       public:
        // The head should be set to null initially
        NodeList() 
          : m_head(nullptr) 
        {
        }
        // This function takes a value and adds it to the beginning of the list
        void prepend(const DataType& value) {
            // The new node uses the current head as the next value
            Node* newNode = new Node(value, m_head); 
            // We set the current head to the new node. 
            m_head = newNode; 
        }
    
        NodeList(size_t count, const DataType& value) 
          : m_head(nullptr) // The head still has to be null initially
        {
            for(size_t i = 0; i < count; i++) 
            {
               prepend(value); 
            }
        }
    };