Java single-linked list

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.