Java data Structure--realization of linear single-link list __arcinfo

Source: Internet
Author: User
Java Data Structure--realization of linear single linked list

First, describe
The characteristics of a chain-type storage structure of a linear table : A set of arbitrary storage units that store data elements of a linear table (this set of storage units can be contiguous or discontinuous). As a result, it does not have the weaknesses of the sequential storage structure (the sequential storage structure inserts data or deletes the data), but it also loses the advantage of random access to the sequential table.

The single linked list consists of data information and pointers to the next node.

Second, the source code

2.1 node Information Node.java

Package com.yds.list;

public class Node<t> {
    protected Node next;
    protected T data;
    Public Node (T data) {
        this.data = data;
    }
}

The above t is generic

2.2 Basic Operation Linklist.java of single linked list

Package com.yds.list;
    public class Linklist<t> {public Node head;
    private int position;

    private int size = 0;
    Public linklist () {this.head = null;

        /** * Delete Header node and return the node * @return/Public node Deleteheadnode () {node Tempnode = head;
        head = Head.next;
        size--;
    return tempnode;
        /** * Deletes the specified node according to subscript * @param index/public void Delete (int index) {node Prenode = head;
        Node Tempnode = head; if (index<0| |
            index>size-1) {System.out.println ("array subscript out of bounds, delete failed");
        Return
                }else{while (position!=index) {prenode = Tempnode;
                Tempnode = Tempnode.next;
            position++;
        } prenode.next = Tempnode.next;
        } position=0;
    size--;
    public int Length () {return size; /** * Find node information by location * @param index * @return */Public T Findbypositon (int index) {T data;
        Node current = head;
                if (index>=0&&index<size) {while (Position!=index) {current = Current.next;
            position++;
        data = (T) current.data;
        }else{throw new Indexoutofboundsexception ("Beyond the chain Length");
        } position = 0;
    return data;
    /** * Based on the data to query the location of the data in the list * @param data to find @return-1 to size-1;-1 indicates not found, 0 to size-1 for the location of the data in the list of the position * *
        public int FindByData (T data) {int temp = position;
        Node Tempnode = head;
            while (data!=tempnode.data&&position<size-1) {tempnode = Tempnode.next;
        position++;
        if (data==tempnode.data) temp = position;
            else{System.out.println ("not Found");
            temp =-1;
        } position = 0;
    return temp;
     }


    /*** Insert node before index * @param index insertion position * @param data to be inserted * * public void Insert (int index,t data) {
        Node node = new node (data);
        Node current = head;
        Node Prenode = head;
        if (index<0&&index>size) {throw new Indexoutofboundsexception ("index position is not valid");
                }else{while (position!=index) {prenode = current;
                current = Current.next;
            position++;
            } prenode.next = node;
            Node.next = current;
            size++;
        position=0; }/** * Inserts data from tail * @param/public void Addfromtail (T data) {node<t> Node
        = new node<t> (data);
        Node Tempnode = head;
                if (Head!=null) {while (position!=size-1) {tempnode = Tempnode.next;
            position++;
            } node.next = Tempnode.next; Tempnode.next = nOde
            }else{node.next = head;
        Head = node;
        } size++;

    Position = 0;
        /** * Header interpolation * @param data/public void Addfromhead (T data) {node node = new node (data);
        Node.next = head;
        Head = node;
    size++;

 }
}

The

2.3 main function shows Javamain.java

Package com.yds.list; public class Javamain {public static void main (string[] args) {//TODO auto-generated method stub L
        INKLIST&LT;INTEGER&GT;LISTB = new linklist<integer> ();
        linklist lista = new linklist ();
        Int[] La = {3,5,8,11};
        int[] lb = {2,6,8,9,11,15,20};
        System.out.println ("--------tail interpolation---------");
        for (int i = 0; i < la.length i++) {lista.addfromtail (la[i]);
        for (int i = 0; i < lista.length (); i++) {System.out.println (Lista.findbypositon (i));
        } System.out.println ("---------head interpolation----------");
        for (int i = 0; i < lb.length i++) {listb.addfromhead (lb[i]);
        for (int i = 0; i < listb.length (); i++) {System.out.println (Listb.findbypositon (i));
        } System.out.println ("-------------------");
        System.out.println ("Find location according to data:" +listb.findbydata (2)); System.out.println ("according to locationLookup data: "+listb.findbypositon (Listb.findbydata (2))";
        System.out.println ("Get the length of the linked list Listb:" +listb.length ());
        Because the Lista data type is generic (T), you can insert any type of data, and LISTB can only insert integer (integer) Lista.insert (2, "ye");
        System.out.println ("Insert Data:" +lista.findbypositon (2));
        System.out.println ("-------------------");
        Listb.delete (6);
        for (int i = 0; i < listb.length (); i++) {System.out.println (Listb.findbypositon (i));

 }

    }

}

Third, screenshot of results

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.