Teach you to play a list in Java

Source: Internet
Author: User
Tags int size sort

After learning the basics of a single linked list, we can customize some of the most interesting features, for example, the order of the elements in a single linked list, (the collation can be set by itself), the linked list flip and so on, here is mainly to talk about the teacher decorate a few questions, I think it is very interesting, we can also think about, Because these methods have been written a few days ago, 51 fake at home also did not make more changes to the previous list, so it is still used before the single linked list structure to continue to add functionality bar.

Before implementing all the features, the next two methods are critical to each of the following steps, the first is to get the length of the list:

The way to get the length of the list can actually be a copy of the Print list method, which I did at the beginning, but later found a better and less error-prone method to define a size attribute in the list class

int size=0;

The size is then modified in all methods that have a length modification to the list, such as adding an element to the last sentence, size++ the method of deleting the element, and then adding a size--and so on (of course the size cannot be less than 0).

The way to get size is to solve it in just one sentence:

int size () {

return size;

}

The other is the method to get the node element at the specified location, as follows:
Java code

/** 
* The method to find the node element at the specified position *
/public node get (int index) {  
    Node node=head;  
    int j=1;  
    Locate the index while  
    (node!=null&&j<index) {  
        node=node.next;  
        j + +;  
    }  
    return node;  
}

Here are some specific questions to see the application of these two methods.

1. Sort the elements in a linked list (if the elements in the list are integers)

Believe that after reading the first two methods, the method of sorting has appeared in the heart, if this is not a single linked list but an array, the sequence of arrays to believe that everyone will not be unfamiliar, so here is only the simplest bubble sort thought:

Java code

/** 
 * If the element in the list is an integer element, the permutation list 
/public
void sort () {  
    Object obj=null;  
    Node temp=new node (obj);/media node for  
    (int i=0;i<=size (); i++) {for  
        (int j=i+1;j<=size (); j +) {  
            // Bubble sort idea, swap data field  
            if (Integer.parseint (string) get (i). obj) >integer.parseint ((String) get (j). obj) {  
                Temp.obj=get (i). obj;  
                Get (i). Obj=get (j). obj;  
                Get (j). Obj=temp.obj;  
            }}}  

2. Encapsulates the elements of a list into an array

It may be strange to see here, why this method is not written on the above, the first method is not the use of this idea? In fact, just because I was after the first topic to think of this method, so put it later, I think this is the same as our learning process, many simple methods are done after a complex method to come up with:

Java code

/** 
 * Encapsulates the list into an object array 
/public
object[] ToArray () {  
    //Create  
    Object Obj[]=new object[size ()];  
    Node Node=head;  
    int j=0;  
    while (node!=null&&j<obj.length) {  
        obj[j++]=node.obj;  
        Node=node.next;  
    }  
    return obj;  
}

Related Article

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.