1

This is the solution to printing elements of a linked list.

Why isn't it Node *current = new Node; and then current = head;?

void printLinkedList(Node* head)
{
    Node *current = head;    
    while(current!=NULL){
        cout << current -> data << endl;
        current = current -> next;
    }
}
templatetypedef
  • 328,018
  • 92
  • 813
  • 992
  • 8
    Why would you allocate a `Node` only to throw it away and let it leak? – Quentin Apr 07 '20 at 18:05
  • You may be confused about the use `new` in C++. Maybe you're coming from a language that uses it differently? – JohnFilleau Apr 07 '20 at 18:06
  • I think you misunderstand what `new` does. If the node already exists, you don't call `new` to store it or use it in a temporary variable. – PaulMcKenzie Apr 07 '20 at 18:07
  • Yes, I'm confused. So we use new only when we add a new element to the linked list? – Akram Mohammed Apr 07 '20 at 18:08
  • 1
    Yes, because `new` literally means "create me a brand new node". Simple. – PaulMcKenzie Apr 07 '20 at 18:08
  • Ohh, thanks a lot Brother Paul for taking the time out to clear my confusion. Silly me! Thanks once again! I understood it! – Akram Mohammed Apr 07 '20 at 18:10
  • 1
    You use `new` when you want to create a new object in dynamic storage. If you are pointing at an already existing object, there is no need for `new`. In fact, [you should save `new` for when you are forced to use it](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new). – user4581301 Apr 07 '20 at 18:10

2 Answers2

6

This is a great spot to draw pictures!

Imagine we have a linked list pointed at by head:

 head
   |
   v
+------+    +-----+    +-----+    +-----+
| i'm  | -> | the | -> | bad | -> | guy | -> null
+------+    +-----+    +-----+    +-----+

If we use the line of code

Node *current = new Node;

then memory looks like this:

 head                                                current
   |                                                    |
   v                                                    v
+------+    +-----+    +-----+    +-----+            +------+
| i'm  | -> | the | -> | bad | -> | guy | -> null    | duh! | -> ?
+------+    +-----+    +-----+    +-----+            +------+

The goal of the function is to print the existing list pointed at by head, but here we've got a pointer to a new linked list cell that isn't a part of the existing list. As a result, we've committed two Programming Sins:

  • We've allocated memory for an object we don't need.
  • We've broken the contract we made with the client.

On the other hand, if we write

Node *current = head;

then memory looks like this:

 head
   |
   v
+------+    +-----+    +-----+    +-----+
| i'm  | -> | the | -> | bad | -> | guy | -> null
+------+    +-----+    +-----+    +-----+
   ^
   |
current

Here, current is now pointing into the existing list, so we can walk the list to find what we need. No new nodes need to be created here, so we don't create any new nodes.

Generally speaking, in C++ you should avoid using new unless you really truly want to create a new linked list cell. In this case, we don't want to do that, which is why we create current and have it point to an existing linked list cell.

Hope this helps!

templatetypedef
  • 328,018
  • 92
  • 813
  • 992
1

Because the node already exists.

new would create a new one.

You don't need or want to create a new one.

You just want to "use" a pointer to an existing node.

Here's it's just the function argument being copied into a variable with a different name. It's actually completely unnecessary.

Asteroids With Wings
  • 16,164
  • 2
  • 17
  • 33