1

I created a linked list and when I tried to print values of the nodes and used NULL as a bound, it didn't work. For example:

#include <iostream>

typedef struct Node;
typedef Node* Node_ptr;
struct Node
{
    int i;
    Node_ptr next;
};

int main()
{
    Node_ptr ptr, head;
    ptr = new Node;
    head = ptr;

    // load
    for(int j = 0; j < 4; j++)
    {
        ptr->next = new Node;
        ptr->i = j;
        ptr = ptr->next;
    }

    // print
    ptr = head;
    while(ptr->next != NULL)
    {
        std::cout << "print: " << ptr->i << std::endl;
        ptr = ptr->next;
    }
}

However, when I run this code, the code gets stuck in an endless loop in the while loop. It never understands that the linked list is only 5 nodes long, it just keeps on going. I can't understand why that happens.

6
  • That's not valid C or C++ code. There's missing semicolons, and a stray typedef, missing }... Commented Mar 30, 2012 at 21:52
  • You're checking for NULL but have you set it anywhere? Commented Mar 30, 2012 at 21:55
  • ..and when you've fixed that, you need to do some debugging. Commented Mar 30, 2012 at 21:56
  • Not counting the missing quote after print. And ptr = ptr->next obviously causes an endless loop as ptr equals ptr->next so the while loop never moves any further... Please show us your real code. Commented Mar 30, 2012 at 21:57
  • Now that the question has been answered, I have edited the code so that it is valid, and yet still illustrates the root cause of the problem Commented Mar 30, 2012 at 22:12

3 Answers 3

5

You probably just need to initialize your pointers (to NULL), otherwise they'll just contain garbage, and will thus also appear as being valid pointers.

For instance:

for(j = 0; j < 4; j++)
{
   ptr->next = new Node;
   (ptr->next)->next = NULL;
   ptr->i = j;
   ptr = ptr->next;
}
Sign up to request clarification or add additional context in comments.

8 Comments

+1 for identifying the cause of the problem. However, I would lean towards using a constructor rather than setting (ptr->next)->next = NULL; manually.
@e.James you don't need a constructor to initialize POD types. See my suggestion.
@Luchian : I upvoted your answer, but to be fair, a constructor would certainly make things less error-prone (unless the OP needs his type to be a C++03 POD for some reason).
@LuchianGrigore: Point taken, and I upvoted your answer as well. Although, tecnically speaking, adding those parentheses is just invoking the default constructor, isn't it?
@e.James : Node is a POD type, so there is no default constructor; new Node uses default-initialization, which would already invoke the default constructor if there was one. Adding the parenthesis value-initializes the object, which in turn effectively zero-initializes its data members.
|
4

Try value initializing your Node:

ptr = new Node();

instead of

ptr = new Node;

Otherwise, you'll just have garbage in the members.

1 Comment

Oh, that's a much simpler solution then isn't it?
3
while(ptr->next != NULL)

You clearly coded it to continue until ptr->next is NULL. Maybe you should set ptr->next to NULL for at least one item in the list? This is why it is common in C to memset(&object, 0, sizeof(object));, or in C++ to have a constructor.

typedef struct Node
{
  int i;
  Node* next;
  Node() : i(0), next(NULL) {} //prevents this problem
}

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.