Linked list
brief introduction
-
Linked list is a very important data structure in data structure. It is not only important in itself, but also an important part of tree, graph and other data structures.
-
There are many kinds of linked list: one-way linked list, circular linked list, two-way linked list and so on.
-
Linked list is a kind of sequential structure which is randomly distributed in memory and is made up of memory addresses linked with each other through pointers. Because of its special memory distribution, the operation of deleting and adding is more efficient than array. Low efficiency in finding and modifying
Let's learn how to add, delete, modify and check some single linked lists.
Function realization
As for the list, it can be said that it is love and hate. Love is because it is really easy to use. Hate is because it is obscure and difficult to understand. I learned it back and forth several times before I understood it.
Define single link list node class
The linked list consists of two parts: data + address pointer. The head node generally does not store data, but serves as the starting index of a single linked list, which is convenient for related operations (the head node does not necessarily exist). The address pointer stores the memory address of the next node, and the address of the last node is empty. The data part can be defined on demand. Here, the data part consists of number (no) and name (name).
- The code is as follows:
/** * Hero node class * @author laona */ class HeroNode { /** * Node number */ int no; /** * Node information */ String name; /** * Next node */ HeroNode next; public HeroNode(int no, String name) { this.no = no; this.name = name; } // The toString method is rewritten to facilitate traversal of linked lists @Override public String toString() { return "HeroNode{" + "no=" + no + ", name='" + name + '\'' + '}'; } }
Realization of one way linked list
Define an empty header node. The header node only needs to store the memory address of the next node. The next == null of the empty chain header node
- The implementation code is as follows:
/** * class List * @author huaian */ class SingleLinked { /** * Empty header node, next == null at this time */ private HeroNode headNode = new HeroNode(0, ""); }
Add node at the end of the list
Add new nodes to the list. The most important thing is to find the end node of the list (that is, the last node of the list points to null). This requires traversing the list.
Traversing the linked list requires a temporary variable to obtain the node temp of which the linked list is not empty until the last node is empty.
- The condition of circulatory body is as follows:
// Get next node temp = temp.next
- Add a new node statement as:
temp.next = newNode;
- The function implementation code is as follows:
/** * Add new node to single chain table * @param newNode {@link HeroNode} New nodes */ public void addNode(HeroNode newNode) { HeroNode temp = headNode; while (true) { if (temp.next == null) { break; } temp = temp.next; } temp.next = newNode; }
The above adding function is completed. Can the data in the linked list be arranged in the order of no? It must be possible.
Add nodes in ascending order
It is not difficult to sort in ascending order. The implementation code is similar to the code for adding nodes at the end of the linked list. The core to be understood is that the linked list can only get the next node through the next of the previous node, so you need to compare the incoming no with the next.no of the previous node.
- Comparison conditions:
temp.next.no >= newNode.no
- The function code is as follows:
/** * Add nodes in ascending order by number * @param newNode {@link HeroNode} New nodes */ public void addNodeByOrder(HeroNode newNode){ HeroNode temp = headNode; while (true) { if (temp.next == null) { break; } if (temp.next.no >= newNode.no) { // Cannot have the same node if (temp.next.no == newNode.no) { System.out.printf("An element with the same index already exists: no = %d,Insert failure\n", newNode.no); break; } newNode.next = temp.next; temp.next = newNode; break; } else { temp = temp.next; } } temp.next = newNode; }
Modify node information
Modifying node information is to compare whether the information of the next node of the current node is the same as that of the node to be modified. If it is compatible, you can modify it directly. If it is different, you can continue to compare the next node.
- The function implementation code is as follows:
/** * Modify information according to node number * @param no Node number * @param newName Node information */ public void modifyByNo(int no, String newName) { HeroNode temp = headNode; while (true) { if (temp.next == null) { System.out.printf("No, No.:%d Node\n", no); break; } if (temp.next.no == no) { temp.next.name = newName; break; } temp = temp.next; } }
Delete node
With the foreshadowing, it's easy to delete nodes. To delete a node, you need to specify which node to delete. Here, you need to delete the node by no. here, you also need to compare whether the information of the next node of the current node is the same as that of the node to be deleted. If the information is the same, you need to delete it.
- Delete the code (by pointing the next of the current node to the next.next of the current node):
// By pointing the next of the current node to the next.next of the current node temp.next = temp.next.next;
- The function code is as follows:
/** * Delete nodes by no * @param no Node number */ public void dropNodeByINo(int no) { HeroNode temp = headNode; while (true) { if (temp.next == null) { System.out.printf("No, No.:%d Node\n", no); break; } if (temp.next.no == no) { temp.next = temp.next.next; break; } temp = temp.next; } }
Traversing linked list
It is convenient to view the contents of the linked list and write a function to traverse the linked list. The code is as follows:
/** * Traversing all nodes of a single chain table */ public void list() { HeroNode temp = headNode; if (headNode.next == null) { System.out.println("The list is empty~!"); return; } while (true) { if (temp.next == null) { break; } System.out.println(temp.next); temp = temp.next; } }
The whole of single chain table class
For the convenience of viewing, paste the whole code of SingleLinked code (save space, only add the header for the function, and omit the function body)
- The code of SingleLinked class is as follows:
/** * class List * @author huaian */ class SingleLinked { private HeroNode headNode = new HeroNode(0, ""); /** * Delete nodes by no * @param no Node number */ public void dropNodeByINo(int no) { ······ } /** * Modify information according to node number * @param no Node number * @param newName Node information */ public void modifyByNo(int no, String newName) { ······ } /** * Add nodes in ascending order by number * @param newNode {@link HeroNode} New nodes */ public void addNodeByOrder(HeroNode newNode){ ······ } /** * Add new node to single chain table * @param newNode {@link HeroNode} New nodes */ public void addNode(HeroNode newNode) { ······ } /** * Traversing all nodes of a single chain table */ public void list() { ······ } } /** * Hero node class * @author huaian */ class HeroNode { /** * Node number */ int no; /** * Node information */ String name; /** * Next node */ HeroNode next; // Structural transmission public HeroNode(int no, String name) { this.no = no; this.name = name; } @Override public String toString() { return "HeroNode{" + "no=" + no + ", name='" + name + '\'' + '}'; } }
Summary
The core of single chain table is to get the information of the next node through the next of the previous node. To understand this, the list is not difficult.
When searching and comparing, do you need to know whether to compare with the previous node or the current node? Is the current node or the next node to be operated on? Wait.
If you are nameless, concentrate on practicing sword! Like friends can leave your praise!