I was asked about the linked list recently and a friend said it when discussing Java with me. To be honest, I have learned very little in the past year I have learned programming. I learned Java and C# in languages, and I learned a little about Html+css+javascript about the Web. Because I prefer it, I am more serious when learning WinForm, and I have studied database operations myself. But I have never studied or studied the linked list. In addition, I have been reading WPF recently and the course has reached JSP, which is relatively tight.
But I still took a night and half a day to look at the one-way linked list. And I tried to write an example using Java. Friends who have not been exposed to the linked list can use it as a reference. I hope you will give more valuable suggestions.
Let’s first explain what a linked list is. As far as I know, a linked list is a data structure, which is at the same level as an array. For example, the implementation principle of ArrayList we use in Java is array. The implementation principle of LinkedList is linked list. My teacher said that linked lists are not efficient when circulating, but have obvious advantages when inserting and deleting. Then he has more than ten years of programming experience, I believe it. But I don’t know if he is talking about two-way linked lists. We are here to understand only one-way linked lists.
Linked lists (Chain mentioned in this article are all unidirectional linked lists, and the following are all referred to as unidirectional linked lists) are actually composed of nodes (Nodes), and a linked list has an uncertain number of nodes. There is only one head node (Head) exposed outward, and all our operations on the linked list are performed directly or indirectly through its head node.
A Node is composed of an object that needs to be stored and a reference to the next node. That is, a node has two members: a stored object, a reference to the next node.
You may not understand this very well, and it may be easier for you to understand if I post a picture.
The key codes for implementing basic operations of Java single linked lists are as follows:
package com.tyxh.link; //Node class public class Node { protected Node next; //Pointer field protected int data;//Data field public Node( int data) { this. data = data; } //Show this node public void display() { System. out.print( data + " "); } } package com.tyxh.link; //Single-linked list public class LinkList { public Node first; // Define a header private int pos = 0;// The location of the node public LinkList() { this. first = null; } // Insert a head node public void addFirstNode( int data) { Node node = new Node(data); node. next = first; first = node; } // Delete a head node and return the head node public Node deleteFirstNode() { Node tempNode = first; first = tempNode. next; return tempNode; } // Insert a node 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; } } package com.tyxh.link; //Test class public class TestLinkList { public static void main(String[] args) { LinkList linkList = new LinkList(); linkList.addFirstNode(20); linkList.addFirstNode(21); linkList.addFirstNode(19); //19,21,20 linkList.add(1, 22); //19,22,21,20 linkList.add(2, 23); //19,22,23,21,20 linkList.add(3, 99); //19,22,23,99,21,20 linkList.displayAllNodes(); // Node node = linkList.deleteFirstNode(); // System.out.println("node: " + node.data); // linkList.displayAllNodes(); // node = linkList.deleteByPos(2); // System.out.println("node : " + node.data); // linkList.displayAllNodes(); // linkList.deleteFirstNode(); Node node = linkList.deleteByData(19); // Node node = linkList.deleteByPos(0); System. out.println( "node : " + node. data); linkList.displayAllNodes(); Node node1 = linkList.findByPos(0); System. out.println( "node1: " + node1. data); Node node2 = linkList.findByData(22); System. out.println( "node2: " + node2. data); } }The above is the implementation of the basic operations of Java single linked lists introduced to you by the editor. 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!