Single linked list
The concept of a single linked list
A linked list is the most basic data structure, which stores your schematic diagram as shown in
Shown above is a single-linked storage schematic diagram, simple and easy to understand, head for the first node, he does not store any data, but to act as a pointer to the list of real data in the first node, and each node has a next reference, point to the next node, a section to the following record, Until the last node, where next points to null.
There are many kinds of linked lists, such as single linked list, double linked list, and so on. We learn the single-linked list, the others understand the principle is actually the same.
Second, using Java to implement a single-linked list
Language is just a tool, the structure of the real experience is that kind of thought, this sentence is true, no matter what to write, its thinking is unchanged. The previous use of C + +, now using Java, Step-by-step implementation.
2.1. Write a node class to act as a node model. We know that there are two properties, 1 of the data,2 storing the data, and the next node's reference,
View Code
2.2. Simple operation of single linked list (add, delete, get total length, list element sort, List traversal)
2.2.1, add node operation, AddNode (node)
Idea: In the beginning, I would have thought if there were any knots. is not necessary to determine the insertion is the first node of the problem, but after the completion of the discovery is not necessary, is not the first node operation is the same, so by moving the pointer to traverse the entire list, find the last node, add later. No difficulty.
View Code
2.2.2, inserts a node to the specified position in the list. Insertnodebyindex (int index,node Node)
Note: To know what the prerequisites are for the insert operation, you can write the code, and then consider if inserting it in a special position is the same. There is also the need to make the insertion position is feasible judgment.
View Code
2.2.3, delete node delnodebyindex (int index) at the specified location
View Code
2.2.4, single-linked list to select Sort Selectsortnode ()
The premise is to know what sort of selection is, and if not, check out the article I'm explaining sort of
Analysis
View Code
2.2.5, single-linked list for insertion sort insertsortnode ()
Premise: To know what an insert sort is. This uses the insertion sort to write me for a long time, the same state, and I think my own writing efficiency is not very high. Anyway, it's a mule, a horse, and a sneak.
View Code
2.2.6, of course, can also use bubble sort, merge sort, and so on, and so on, you can try, I will not write. If you don't understand these sorts, then look at the sort of articles I wrote.
2.2.7, calculating the length of a single linked list
View Code
2.2.8, traverse single linked list, print data
Java single-linked list