0

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.

3
  • It would be helpful if you specified what language this is. Commented Feb 26, 2015 at 18:53
  • The language is C++. Commented Feb 26, 2015 at 18:55
  • It looks like your "first node" is a local variable inside the IntList constructor, but you haven't shown enough code to be sure. Commented Feb 26, 2015 at 19:01

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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;
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.