After working overtime, the schedule finally came to an end.

Let’s start today’s article with hands-on code to implement one-way lists and some basic operations on them. (See here if you’re not familiar with linked lists.)

Linked list node class

public class Node {

  / / data domain
  public String data;

  // pointer field (pointing to the next node)
  public Node next;

  /** * constructor */
  public Node(String data) {
    this.data = data; }}Copy the code

The basic code for a one-way linked list (lead node)

public class SingleLinkedList {

  / / head node
  private Node herd = new Node("HerdNode");

  public Node getHerd(a) {
    return herd;
  }

  public void setHerd(Node herd) {
    this.herd = herd; }}Copy the code

Add nodes at the end of the list

public void addAfter(Node newNode) {
  // Secondary node
  Node temp = herd;
  while (true) {
    // End the loop by finding the last node in the list
    if (temp.next == null) {
      break;
    }
    // Move the secondary node back
    temp = temp.next;
  }
  // Add the new node to the end of the list
  temp.next = newNode;
}
Copy the code

Inserts data after the specified node content

Before you start coding, look at the following image to help you understand the code.

/** * Insert data ** after specified node content@paramNodeData specifies the contents of the node (after which node is inserted) *@paramNewNode newNode */
public void insertAfterNode(String nodeData, Node newNode) {
    // Check parameters
    if (nodeData == null) {
      throw new RuntimeException("Wrong entry!");
    }
    // Secondary node
    Node temp = herd;
    // Define the identifier (indicating that the specified node is found)
    boolean flag = false;
    while (true) {
      // get to the end of the list
      if (temp == null) {
        break;
      }
      // End the loop by finding the specified node.
      if (nodeData.equals(temp.data)) {
        flag = true;
        break;
      }
      // Move the secondary node back
      temp = temp.next;
    }
    if (flag) {
      // The new node is connected to the next node of the specified node.
      newNode.next = temp.next;
      // Connect the node to the new node
      temp.next = newNode;
    } else {
      throw new RuntimeException("Cannot insert node without finding specified node!"); }}Copy the code

Delete a node based on the specified node content

Before you start coding, look at the following image to help you understand the code.

/** * Deletes the specified node **@paramData Specifies the node content (deleted node content) */
public void delete(String data) {
    // Check parameters
    if (data == null) {
      throw new RuntimeException("Wrong entry!");
    }
    // Define the secondary node
    // Note: This node must point to the previous node of the node to be deleted
    Node temp = herd;
    // Define the identifier (indicating that the specified node is found)
    boolean flag = false;
    while (true) {// get to the end of the list
      if(temp.next == null) {break;
      }
      // The specified node is found
      if(data.equals(temp.next.data)){
        flag = true;
        break;
      }
      temp = temp.next;
    }
    if(flag){
      // Delete the specified node
      temp.next = temp.next.next;
    }else {
      throw new RuntimeException("Unable to delete node found!"); }}Copy the code

Single linked list operation is more difficult to insert nodes and delete nodes, through the above code and picture I believe you have a single linked list of their own understanding, if there is a problem you can tell me. The query and modify methods are no longer demonstrated in code. Through the above code, I believe you can also write their own.

Today’s sharing is over here, if there is a problem in the article, I hope you can point out. Now that you’ve seen it all, give it a thumbs up and go!