Two-terminal list of JAVA data structure

Source: Internet
Author: User
First, overview:

1, what when the two-terminal linked list:
List of linked lists that hold this to the last point reference

2. Insert from head
To make a judgment on the linked list, set the tail node to the newly added node if it is empty

3, from the tail to insert
If the list is empty, set the header node directly to the newly added node, otherwise set the last node of the tail node as the newly added node

4. Remove from head
Determine if the node has a next node, if not, set the node to null two, specifically implement

/** * @ Description of the linked list of the end-to-end * @ project name Java_datastruct * @ Package Name Com.struct.linklist * @ class name linklist * @au 
    Thor Chenlin * @date June 26, 2010 a.m. 8:00:28 * @version 1.0 * * public class Firstlastlinklist {//Head
    Private Node I;

    Tail private Node last;
        Public Firstlastlinklist () {i = null;
    last = null; /** * Insert Data * @param value/public void Insertfirst (Long value) {Node NewNode = new Nod
        E (value);
        if (A/= null) {last = NewNode;
        }else {//move the A-node down newnode.next = i;
    ///Put the inserted node as the new node, NewNode; /** * Insert Data * @param value/public void Insertlast (Long value) {node NewNode = new Node
        (value);
        if (A/= null) {a = NewNode;
        }else {last.next = NewNode; ///Set the last node to the latest node = newnOde
    public Boolean IsEmpty () {return-= NULL; /** * Delete Header node * @param value * @return/Public node Deletefirst () {if (first = = nul
        L) {throw new RuntimeException ("linked list data does not exist");
        } if (First.next = null) {last = null;
        Node temp = i;
        i = Temp.next;
    return temp;
        Public node Deletebykey (long key) {Node current = i;
        Node last = i;
                while (Current.data!= key) {if (Current.next = null) {System.out.println ("no node Found");
            return null;
            last = current;
        current = Current.next;
            } if (current = = i) {//return deletefirst ();
        Pointing to the next means removing the first first = First.next;
        }else {last.next = Current.next;
    return to current;
     /** * Show all the data*/public void display () {if (A/= null) {//throw new runtimeexception ("list data does not exist");
        Return
        Node current = i;
            while (current!= null) {Current.display ();
        current = Current.next;
    } System.out.println ("---------------"); /** * Lookup Node 1 * @param value * @return/Public node Findbyvalue (Long value) {node C
        urrent = A;
            While (the current!= null) {if (Current.data!= value) {current = Current.next;
            }else {break;
            } if (current = = null) {System.out.println ("not Found");
        return null;
    return to current; /** * Lookup Node 2 * * @param key * @return/Public node Findbykey (long key) {Nod
        e current = A; while (Current.data!= key) {if (Current.next = = null){System.out.println ("not Found");
            return null;
        current = Current.next;
    return to current;
        /** * Find the corresponding value based on index * @param position * @return/public Node findbyposition (int position) {
        Node current = i; 
            Why is position-1, because to use traversal, let current point to the next, so Position-1 's next node is the value for (int i = 0; i < position-1; i++) {
        current = Current.next;
    return to current;
        public static void Main (string[] args) {firstlastlinklist linklist = new Firstlastlinklist ();
        Linklist.insertfirst (21);
        Linklist.insertfirst (22);
        Linklist.insertfirst (23);
        Linklist.insertlast (24);
        Linklist.insertlast (25);
        Linklist.insertlast (26);

        Linklist.insertlast (27);

        Linklist.display ();

        SYSTEM.OUT.PRINTLN ("---find-------------------------------------"); Linklist.findbykey (25).Display ();

        System.out.println ("--delete the-------------------------------------");
        Linklist.deletefirst (). display ();
        Linklist.deletefirst (). display ();
        Linklist.deletefirst (). display ();

        Linklist.deletefirst (). display ();
        System.out.println ("-Delete specified value---------------------------------------");
        Linklist.deletebykey. display ();

        Linklist.deletebykey. display ();
        System.out.println ("----------------------------------------");


    Linklist.display (); }
}
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.