Home java Node class in LinkedList

Node class in LinkedList




I run out the topic of the collection and LinkedList.
I read that the list (LinkedList) consists of links (nodes), but I do not particularly understand what kind of beast it is and when we use this class.
During training there was a task for checking the list for the presence of a cycle, where this class was used, but when adding an item to the list, we do not use this class.
For explanation and an example will be very grateful.

Answer 1, Authority 100%

In the Nutri LinkedList there is an invested class node , which is an element containing data, as well as a reference to the Introduction Elenty and Adduce, actually LinkedList and works on such nodes.
The code is taken from Open-Source JDK 14:

private static class node & lt; e & gt; {
  E Item;
  Node & lt; E & GT; NEXT;
  Node & lt; E & GT; prev;
  Node (Node & LT; E & GT; PREV, E ELEMENT, NODE & LT; E & GT; Next) {
    this.item = element;
    this.next = Next;
    this.prev = prev;

This is how the Node class looks like in the lard linkedlist . For future if you are interested in such things and how they arranged in the larger, go to the main directory jdk Next to the file lib there is an archive with the name src and in it a lot of files. Java from java.se

So you can go there in src Find a package java . Base Next Util ; And in it LinkedList and study in detail how it works in the inside. In general, src is the place where you can see how “Java’s grandfathers are programmed” and what will learn anybout.

There you can see what happens when adding how this inner class is used node .

Here, for example, how the add () method is arranged:

Public Boolean Add (E E) {
  LinkLast (E);

Well, and the LinkLast method (E E) which shows how this Node is used in Linkelist:

void linklast (E E) {
  Final Node & LT; E & GT; L = last;
  Final Node & LT; E & GT; newnode = new node & lt; & gt; (l, e, null);
  Last = newnode;
  if (L == NULL)
    first = newnode;
    L.NEXT = newnode;
  Size ++;
  modcount ++;

Answer 2, Authority 50%

LinkedList stores elements as a connected list. The connected list consists of elements that store data and references to the next and previous elements.
Let us say 10 elements in the list, which is how to insert an element in the middle:

bright red highlighted links that changed – they now indicate a new element.
Bright purple highlighted new links – links of the new element on his neighbors.
Pros: LinkedList collection can occupy scattered memory areas; is ordered (ordered). Cons: not a Synchronized collection (does not support the operation of several streams). It is recommended to use when it is often necessary to perform the options for adding and removing the elements

ArrayList and LinkedList comparison

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions