Below is a single linked list shared by the editor with you using Java. If you have any questions, please leave me a message.
First define a Node class
public class Node {protected Node next; //Pointer field public int data;//Data field public Node( int data) { this. data = data; } //Show this node public void display() { System. out.print( data + " "); } }Next, define a single linked list and implement the relevant methods:
public class LinkList {public Node first; // Define a header private int pos = 0;// The position of the node public LinkList() {this.first = null;}// Insert a header public void addFirstNode(int data) {Node node = new Node(data);node.next = first;first = node;}// Delete a header and return the header public Node deleteFirstNode() {Node tempNode = first;first = tempNode.next;return tempNode;}// Insert nodes at any location and insert public void after index add(int index, int data) {Node node = new Node(data);Node current = first;Node previous = first;while (pos != index) {previous = current;current = current.next;pos++;}node.next = current;previous.next = node;pos = 0;}// Delete nodes at any location public Node deleteByPos(int index) {Node current = first;Node previous = first;while (pos != index) {pos++;previous = current;current = current.next;}if (current == first) {first = first.next;} else {pos = 0;previous.next = current.next;}return current;}// Delete the node according to the node's data (only delete the first one) public Node deleteByData(int data) {Node current = first;Node previous = first; // Remember the previous node while (current.data != data) {if (current.next == null) {return null;}previous = current;current = current.next;}if (current == first) {first = first.next;} else {previous.next = current.next;}return current;}// Show all node information public void displayAllNodes() {Node current = first;while (current != null) {current.display();current = current.next;}System.out.println();}// Find node information based on location public Node findByPos(int index) {Node current = first;if (pos != index) {current = current.next;pos++;}return current;}// Find node information based on data public Node findByData(int data) {Node current = first;while (current.data != data) {if (current.next == null)return null;current = current.next;}return current;}}Finally, we can do related tests through the test class:
public class TestLinkList {public static void main(String[] args) { LinkList linkList = new LinkList(); linkList.addFirstNode(20); linkList.addFirstNode(21); linkList.addFirstNode(19); //print19,21,20 linkList.add(1, 22); //print19,22,21,20 linkList.add(2, 23); //print19,22,23,21,20 linkList.add(3, 99); //print19,22,23,99,21,20 //Calling this method will print 19,22,23,99,21,20 linkList.displayAllNodes(); }}At this point, the operation of single linked lists has ended here.
The above is the implementation code of Java single linked list that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!