简体   繁体   中英

Why do I need the Node class in Java for LinkedList?

After going over different tutorials on Linked Lists I am seeing some mentioning the JavaNode class for linking to the previous and next nodes and some not using it at all when creating a linkedList.

Is the Node class needed for Linked Lists? Why do some tutorials seem to create Linked Lists without it? Also read that using the Node class is the "formal" way of creating a linkedlist

If you are asking whether (and why) you need to create Node instances to use a java.util.LinkedList , the answer is: No you don't. The list itself takes care of that.

(Note that the Node class that youlinked to is not a linked list node. It actually denotes a node in an DOM. The actual Node class used internally by java.util.LinkedList is a private class.)


If you were asking why linked lists in general require a Node type, the answer is that they don't.

The other way of creating a linked list (that doesn't involve a Node type) is to directly chain the elements of a list to each other. This has a couple of consequences:

  1. This requires the element class itself to have a next field (and possibly a prev field) for chaining the elements.

  2. It means that a given element instance can only be a member of one list at a time, and can't be a member of the same list twice.

Together, these mean that the nodeless approach is incompatible with the standard java.util.List API.

The nodeless approach is also bad from the OO design perspective:

  • By the adding next and prev fields to the element type, you are breaking down abstraction boundaries and the separation of concerns.
  • The element instance now knows about the list that the element is part of.
  • The list abstraction only works for certain types of element, and has to take account of which list an element is a member of.

These things are liable to make the nodeless list abstraction harder to use... and less reusable. (Though in limited circumstances, it may still be a good solution.)

You don't technically necessarily need a node class, but the design with a node class is the good design. The design without one is the poor design.

This answer is slightly opinionated, but based on what we should all have learned in the first or at least the second year of programming, so consensus-based.

Say that we have a list of students. It's now the natural responsibility of each Student object to have (“know”) the student's contact information, courses enrolled in, grades taken, etc. It is not the natural responsibility of a Student object to know that it is part of a linked list, not to mention whether that list is singly or doubly linked. For this responsibility we have the Node class.

The design with the Node class has the further potential advantage that you can design and code a generic linked list and use it to instantiate a list of students, a list of teachers, a list of courses, etc. Stephen C in the other answer mentions further advantages.

Historical background: Where I learned data structures around 1980, we would fit each student record with a next pointer. (We learned singly linked lists. Doubly linked lists were only mentioned in passing.) What is nowadays considered the poor design. I had hoped that it had long gone out of use.

Performance (skip this paragraph until you really need it:-): The poor design with next and previous references within the business objects like Student will typically perform slightly better. So if you are in a situation where performance is a Very Real Issue, you may consider it. It is no low-hanging fruit since it pollutes your design, so it will probably come near the bottom of your list of measures to take for better performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM