The realization of one-way list in Java: Adding, deleting, checking and modifying function

Source: Internet
Author: User

Write a data structure that everyone is familiar with: one-way linked list.

But let me tell you a little secret, Java API has provided a one-way list of classes, you can use directly, but the course of learning data structure must have been known, although the system will provide us with some commonly used data structure, but the custom can always bring different feelings of joy, and through their own writing also allows us to understand the process of implementation, and we can write some more personalized methods as their own data structure. This is mainly to introduce some common structure inside the use of methods, as well as the details of how the linked list is operated.

First of all, the advantage of a single linked list relative to the queue is that the storage address is not contiguous, which means that the elements in one of these locations do not need to perform memory operations on the other elements before, greatly reducing the pressure on our computers. Let's go straight to the bottom:

First you define a node class, as follows:

Java code

public class Node {  
        node next;//reference to the next node  
        object obj;//node element public  
        node (object obj) {  
            this.obj=obj;  
        }  
}

Then there's our LinkedList class, which defines an empty list first:

Node head=null;//create an empty linked list, header node

Node last=head;//tail node

There are two ways to print a list, either recursively or by using a method that is not recursive, as follows:

Java code

/** 
* Non-recursive method of printing elements */public
void print (Node head) {while  
    (head!=null) {  
        System.out.println ( Head.obj);  
        head=head.next;//Index Backward Shift  
    }  
}  
/** 
 * Recursive printing of linked-list elements * * Public
void Printnode (Node head) {  
    if (head!=null) {  
        System.out.println (head.obj);  
        Node Node=head.next;  
        Printnode (node);//Recursive Call  
    }  
}

Non-recursive methods have a fatal flaw, printing and changing the position of the head node, so we should be inclined to use the recursive method.

Said so much, additions and deletions to the official beginning of the search:

Adds an element to a linked list. To determine that a linked list has reached the end of the basis is that the node's next reference has been null, so to add a node to the end, the first point to put the new node at the end, and then the end of the node to shift backwards, the specific procedures as follows:

The code is as follows:

Java code

/** 
 * method to add elements to a specified list 
 * @param obj   inserted element 
/public
void Add (Object obj) {  
    node node=new node (obj);//New node if   
    (head==null) {//If the linked list is empty head  
        = node;  
    } else{  
        last.next=node;//first puts the new node in the last  
    }  
    last=node;//and then shifts the last node back.  

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.