开发者

Question about linked lists/pointers in c++

开发者 https://www.devze.com 2023-02-07 23:04 出处:网络
The nature of pointers being NULL in C++ seems to feel arbitrary.I\'m sure there\'s a method to it that I\'m missing, but the following makes sense to me, but doesn\'t seem to work.I have the followin

The nature of pointers being NULL in C++ seems to feel arbitrary. I'm sure there's a method to it that I'm missing, but the following makes sense to me, but doesn't seem to work. I have the following method for adding a node to a linked list:

LLNode *ll; // set to NULL in constructor.
void addToLL(Elem *e)
{
    LLN开发者_如何学运维ode *current = ll;
    while(true)
    {
        // edge case of an empty list.
        if (ll == NULL)
        {

            ll = new LLNode(e);
            break;
        }
        else if (current == NULL)
        {
            current = new LLNode(e);
            break;
        }
        else {
            current = current->next;
        }

    }
}

When adding a 2nd node to the list, the case for current == NULL does not get caught, so it tries to call current = current->next and crashes do to accessing invalid memory. Why would this be the case? A LLNode has a pointer to an Elem, and a pointer called next to another LLNode.


You probably didn't set the next pointer to NULL in the LLNode constructor.

Objects of the basic types in C++ (pointer types, numeric types, etc.) have indeterminate initial values: they don't get initialized by default. You need to explicitly initialize such objects before you use them.


For this sort of thing you need a pointer to a pointer in order to strip away a lot of the needless exceptions in your implementation:

LLNode *ll = NULL;

void addToLL(Elem *e)
{
  LLNode** current = ≪

  // While the current pointer to pointer is mapped to something,
  // step through the linked list.
  while (*current)
    current = &(*current->next);

  // At this point current is pointing to a NULL pointer and can
  // be assigned to.
  *current = new LLNode(e);
}

The reason pointers are NULL is because that evaluates to false and allows you to do simple checks such as while (*current) without a lot of overhead. In the CPU this usually ends up being implemented as a test-if-zero operation.

Pointers are only NULL if initialized as such. In C they are often undefined unless properly initialized and referencing an uninitialized pointer is recipe for disaster. You'll want to ensure any pointers you define are always initialized to something valid before using them.


1) You say that ll is set to NULL in the constructor. But what constructor? There's no class definition here. Is ll a global variable? And are you sure that the constructor for LLNode sets the next pointer to NULL?

2) The condition

    if (ll == NULL)

can and should be checked outside of the loop, as ll is not modified inside the loop.

3) current is a local stack variable, so assigning to it will have no effect once the function exits. In particular, current = new LLNode(e) is a memory leak.

4) To add a node to the linked list, you must find the last node of the existing list, and modify its next pointer. Something like this would work:

 // ll is a field representing the first node in your existing linked list.
 if (ll == NULL) {
     ll = new LLNode(e);
 }
 else {
     current = ll;
     while (current->next != NULL) {
         current = current->next;
     }
     current->next = new LLNode(e);
 }

EDIT: Modified the above based on your comment that ll is a class member.


The first thing I see in your code is that current is local, gets allocated with new but is never actually attached to the list.

Surely your code should be

else if( current->next == NULL )
{
   current->next = new LLNode( e );
   break;
}

LLNode must of course initialise next to NULL in its constructor.

Of course your list has O(N) insertion time, and if this is anything other than an exercise you should be almost certainly be using standard library containers.

You should also probably move the edge case out of the loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号