Question: I am just trying to make a very simple linked list, but for some reason I am getting a "can't access memory" error. I have all other methods built, but I can't actually create the first node. The language is C++.

The constructor looks like this:

```
IntListNode::IntListNode(){
    data = -1;
    next = this;
    prev = this;
}

```

the Linked List constructor looks like this:

```
IntList::IntList(){
    IntListNode* sentinel = new IntListNode(); 
}

```

Can anyone see the problem? Thanks.

Answer: 
This constructor

```
IntList::IntList(){
    IntListNode* sentinel = new IntListNode(); 
}

```

makes no sense. There is declared local variable `sentinel` that will be destroyed at once after exiting the constructor.

And this constructor 

```
IntListNode::IntListNode(){
    data = -1;
    next = this;
    prev = this;
}

```

is very confusing. It would be better not to declare explicitly a constructor and simply use an aggregate. Or at least the constructor could look like

```
IntListNode( int value, 
             IntListNode *next = nullptr, 
             IntListNode *prev = nullptr  ) 
                : data( value ), next( next ), prev( prev )
{
}

```

I suppose that data has type int.

And there is no sense to have a sentinel node. You should define two nodes: head and tail that initially will be set to nullptr.
You should conceptually split your linked list into two classes:

1. Container
2. Node

Linked List Container
---------------------

The container contains a pointer to the first node, often called the *head*, and optionally, a pointer to the last node, often called the tail: 

```
class Linked_List
{
  Node * head;
  Node * tail;
public:
  Linked_List : head(nullptr), tail(nullptr)
  { ; }
};

```

Linked List Node
----------------

The node class contains pointer to the next node. In the case of a doubly linked list class, it contains a pointer to the previous node. 

```
class Node
{
  Node * next;
  public:
    Node() : next(nullptr)
    { ; }
    void link_to(Node & other)
    {
      next = &other;
    }
    void remove_link()
    {
      next = nullptr;
    }
};

```

Specializing the Node
You can specialize the node class by:

* Adding a data field to the class
* A new class that contains a data field and inherits from Node
* Make it a `template` class.

Examples:

```
class Integer_Node_Inheritance : public Node
{
  public:
    int data;
};

class Node
{
  Node * next;
  int    data;
  public:
    Node() : next(nullptr)
    { ; }
    void link_to(Node & other)
    {
      next = &other;
    }
    void remove_link()
    {
      next = nullptr;
    }
};

template <typename Data_Type>
class Node
{
  Node *      next;
  Data_Type   data;
  public:
    Node() : next(nullptr)
    { ; }
    void link_to(Node & other)
    {
      next = &other;
    }
    void remove_link()
    {
      next = nullptr;
    }
};

```