The article directories

  • Java – Bidirectional linked lists of data structures
  • 1. Realization of single linked list
    • (1) Define a node type
    • (2) Head insertion method
    • (3) Tail insertion method
    • (4) Insert nodes according to subscripts
    • (5) Search keywords
    • (6) Delete the first occurrence of the keyword
    • (7) Delete all the key words
    • (8) Node recovery
    • (9) Printed display of linked list
  • 2. Complete code display
  • Finished!

Java – Bidirectional linked lists of data structures

Move on to Java — singly linked lists of data structures

\

In the previous study, we mainly understand a lot of Basic Syntax of Java, but in the later Java learning, it is very important to understand the knowledge of basic data structure, the idea of data structure can help us more clearly understand the idea of Solving problems in Java and so on.

\

Today we are going to start learning how to implement a Java based bidirectional acyclic list without spearhead.

\

1. Realization of single linked list

\

(1) Define a node type

\

We create a ListNode class as the node type, so how do we define member attributes?

\

From the above structural analysis, we need to define two member variables val – as the stored value of the node, next – to hold the address/reference of the next node, and prev – to hold the address/reference of the previous node

\

And once defined, their default values are 0, NULL, null, so to assign a value to this node, write a constructor that saves the value first.

\



\

Here we provide two constructors, one to implement the node that provides the value, and the other to implement the node that does not provide the value, for our convenience later.

\

As a bidirectional list, we can traverse from the beginning node to the end node, as well as from the end node to the end node, so we define two nodes — head head node and last tail node

\

    public ListNode head;
    public ListNode last;
Copy the code

\

We then implement the various methods of a single LinkedList in the LinkedList class.

\

(2) Head insertion method

\

Take the above structure as an example, this single linked list has no special puppet node to act as the head node, the first node is defined as the head node, so the head interpolation method, we define a node, inserted at the front of the list, as the new head.

\

Ideas:

\

There are two cases:

\

First insertion

Not the first insertion

Code display:



\

(3) Tail insertion method

\

Similar to the head interpolation method, insert a node at the end of the list.

\

Code display:

\



\

(4) Insert nodes according to subscripts

\

The first data node is subscript 0 and the node is inserted anywhere.

Using the linked list above as an example, we want to insert the new node into position 2

\

Ideas:

\

1. Check whether the index subscript is valid

2. If index==0 is passed, insert the header

3. If index==sizeof() is passed in, insert the end method

4. When inserting an intermediate node, change the attributes of the nodes before and after cur

\

Code display:

\

\

(5) Search keywords

\

For example, we will now look for the presence of a node in the list with val=20 and return true if there is one, false if there is none.

\

If cur.val == key, return true. If key is not found, return false.

\

Ideas:

\

Code display:

\

\

(6) Delete the first occurrence of the keyword

\

Ideas:

Code display:

\

(7) Delete all the key words

\

Ideas:

Return (cur = cur.next); return (cur = cur.next); Continue traversing backwards.

\

Special case: Keywords appear on all nodes

Code display:

\

\

(8) Node recovery

Nodes can be reclaimed in two ways:

1. The method of violence

Empty head and last

2. Empty them one by one

Iterate through the single linked list and set next and PREv of each node to NULL.

\

Ideas:

Code display:

\

\

(9) Printed display of linked list

\



\

2. Complete code display

\



\

Well, today’s knowledge is shared here, I hope you can practice more, thank you for your appreciation and attention!

\

Thanks for your support!!


\

Finished!