Question

From community college, I was told to implement linked list with starting node as empty node and append data node to the empty node, but in University, they don't use an empty node. I remember there were advantages of having an empty node but cannot recall it at this point.

What would be the benefit of having an empty node?

One that I can think of is that empty starting node can store list properties such as size of the linked list, and because it never gets deleted, we can extract list properties from it.

This is an example of having an empty node: (also refer to empty node implementation)

(EmptyNode)->(1st Data)->(2nd Data)->null

And this is an example of not having an empty node which is more common.

(1st Data)->(2nd Data)->null

Thank you in advance.

Was it helpful?

Solution

The advantage of an empty node is that it's easier to represent an empty list that still otherwise exists.

While you can sometimes represent an empty list as simply null, the disadvantage is that it assumes that lists are always represented as pointers. Another disadvantage is that you can't call any functions on null/ you make the interface awkward.

Imagine:

RootNodedListNode<char> list; // start empty
list.add('a');
list.add('b');

RootlessListNode<char> * list = null; // start empty
//list->add('a');
list = new RootlessListNode<char>('a');
list->add('b');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top