Java implementation simple linked list instance tutorial

Source: Internet
Author: User

The linked list is different from the queues or arrays we have learned before. It is non-linear, that is, it is not stored continuously in the memory. The linked list can be understood as composed of many nodes. Many people will regard the linked list as a bicycle chain, I think this is a little inappropriate because Linked lists can also be unordered. For example, Zhang San's phone number, Wang Wu, and Li Si's phone number, if Michael wants to contact Wang Wu, he only needs to use Li Si. They can be in different locations. Of course, it may not be appropriate to make a metaphor here, we generally divide a node in the linked list into two parts: Data filed and Pointer field (these are the names used by the authors in C). The Data fields are used to store Data, the following pointer is used to store the address of the next node.

Here I will use a class to illustrate how to delete and add functions here. In order to make the code concise, I use an internal class.

The code is as follows: Copy code
Public class LinkList {
     
Node head;
Node end;
Int count;
     
Public void add (String s ){
Node n = new Node (s );
If (head = null ){
Head = n;
} Else {
End. next = n;
N. last = end;
        } 
End = n;
Count ++;
    } 
 
Public void delete (int index ){
Node n = getNode (index );
Node n2 = n. last;
Node n3 = n. next;
If (n2 = null ){
N3.last = null;
Head = n3;
} Else if (n3 = null ){
N2.next = null;
End = n2;
} Else {
N2.next = n3;
N3.last = n2;
        } 
Count --;
    } 
     
// Obtain the element value at the specified position
Public String get (int index ){
Node n = getNode (index );
String s = n. element;
Return s;
    } 
     
Private Node getNode (int index ){
If (index <0 | index> = size ()){
Throw new RuntimeException ("out of bounds... ");
        } 
Int num = 0;
Node n = head;
While (num <index ){
N = n. next;
Num ++;
        } 
Return n;
    } 
     
Public int size (){
Return count;
    } 
// Internal class
Class Node {
String element;
Node last;
Node next;
Public Node (String element ){
This. element = element;
        } 
    } 
}


Add method:

Put a node. If there is no node in the linked list, it is used as the head node. If there is a node in the linked list, it is placed as the end node.

Delete method:

Here we need to make three judgments. We need to judge that there is only one node and two nodes in the linked list, he cannot determine which node you want to delete based on the two nodes. The third case is simple.

Next I will talk about the differences between the linked list and the array (the difference is that it is used in different scenarios ):

From the logic structure
1. The array must have a fixed length (number of elements) defined in advance and cannot adapt to the dynamic increase or decrease of data. When data increases, it may exceed the number of elements defined previously. When data decreases, memory is wasted. Arrays can be directly accessed by subscript.
2. The linked list is dynamically stored and allocated, which can be used to dynamically increase or decrease data and facilitate data entry insertion and deletion. (When inserting or deleting data items in an array, it is very cumbersome to move other data items.) the linked list must find the next element based on the next pointer.
From the memory storage perspective.
(However, linked lists are also divided into two types: ArrayList and LinkList. ArrayList is actually a linked list using arrays. It is more suitable for determining the linked list size or less Performing addition or deletion operations on the linked list. At the same time, the access time to each linked list node is constant; the LinkList uses a List to implement the linked List, which is more suitable for scenarios where you need to perform operations on the linked List frequently. The access time to the linked List node is related to the length of the linked List O (N) here we need to analyze the specific problem set .)

1. The (static) array allocates space from the stack, which is convenient and fast for programmers, but has a low degree of freedom.
2. The linked list allocates space from the heap. It has a high degree of freedom, but it is troublesome to apply for management.

From the above comparison, we can see that if you need to quickly access data and insert or delete few or no elements, you should use an array. On the contrary, if you need to insert or delete elements frequently, you need to use the linked list data structure.

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.