Java data structure-single linked list of linear tables LinkedList

Source: Internet
Author: User

The chain storage structure of linear table, also known as chain table, linked list, the storage unit of the list can be continuous or discontinuous.
The nodes in the list contain data fields and pointer fields, which are the fields where data element information is stored, and the Pointer field is the field where the direct successor is stored (commonly referred to as a pointer).

Note the difference between a head node and a head pointer:
Head pointer:

    • A pointer to the first node of the list, or a pointer to the head node if the list has a head node;
    • The head pointer has the function of identification, so the common head pointer is the name of the linked list;
    • The head pointer is not empty, regardless of whether the list is empty;
    • is a necessary element of a linked list.

Head node:

    • The head node is set up for the unification and convenience of operation, placed in front of the first element node, its data field is generally meaningless, and the length of the linked list can be stored;
    • The head node is not a necessary element of a linked list.

Let's start with a single-link list, and then talk about it later.
Linked list with headless nodes

Linked list with head node

Empty linked list

I tried to write a linkedlist code in Java, as follows:

Package com.phn.datestructure;/** * @author Pan Hainan * @Email [email protected] * @TODO chained table * @date July 18, 2015 */public CL    The first node of the folinkedlist<e> {//single-linked list private fonode<e> header = null;    The length of the single-linked list is private int size;        /** * @TODO Default parameterless constructor */public folinkedlist () {super ();        This.header = new fonode<e> ();    This.setsize (); }/** * @TODO single-linked list add element * @param e Data element type * @return True */public boolean Add (E e) {fonode&lt ;        e> node = new fonode<e> (E);        if (header.gete () = = null) {Header.sete (e);            } else {fonode<e> lastnode = This.last (This.header);        Lastnode.addnext (node);        } this.size++;    return true; }/** * @TODO single-linked list insert element * @param index Insert position * @param e data element type * @return True */public Boolean INS        ERT (int index,e E) {fonode<e> node = new fonode<e> (e); Fonode<e> PRenode = This.get (index-1);        Node.addnext (Prenode.next);        Prenode.addnext (node);        this.size++;    return true; }/** * @TODO single-linked list delete element * @param index position of the element to be deleted * @return E deleted element */public fonode<e> Remove (int index)        {fonode<e> Prenode = This.get (index-1);        fonode<e> node = prenode.next;        Prenode.addnext (PreNode.next.next);        Node.addnext (NULL);        this.size--;    return node; /** * @TODO Gets the element based on the index position of the element * @param index position of the index element * @return e element e */public fonode<e> get (int        Index) {validateindex (index);        fonode<e> temp = This.header;        int i = 0;                while (I < index-1) {if (temp! = null) {temp = Temp.next;            i++;            } else {throw new RuntimeException ("Node null value error");    }} return temp; }/** * @TODO Modify the element of index position I in a single linked list to element e * @param indexThe index position of the element * @param e needs to be modified to the element * @return true to modify the success flag */public boolean set (int index, E e) {Validateind        EX (index);        fonode<e> OldNode = this.get (index);        Oldnode.sete (e);    return true; }/** * @TODO Verify that the given index position is valid * @param index position given by * * private void Validateindex (int index) {if ( Index > This.size | |        Index < 0) {throw new RuntimeException ("Index error:" + index); }}/** * @TODO get the last node of the single-linked list * @param header node of the header single-linked list * @return The last point of the node single-linked list */private fonode& Lt        E> last (fonode<e> header) {fonode<e> temp = header;            while (true) {if (Temp.next = = null) {return temp;        } temp = Temp.next; }} @Override public String toString () {return ' [' + this.    Nodestostring (This.header) + "]"; }/** * @TODO set the length of the single-linked list * @param header node of the header single-linked list * @return nodes string sequence for single-linked lists */privAte String nodestostring (fonode<e> header) {StringBuffer sb = new StringBuffer ();            if (header = null) {Sb.append (Header.gete ());            fonode<e> temp = new fonode<e> ();            temp = Header.next;                while (temp! = null) {Sb.append ("," + Temp.gete ());            temp = Temp.next;    }} return sb.tostring ();    }/** * @TODO set the length of the single-linked list */private void SetSize () {this.size = 0;    }/** * @TODO Gets the length of the single-linked list * @return The length of the single-linked list */public int size () {return this.size; }}

The insertion and deletion of data elements is also talked about here.
The insert operation is shown below:



Code:

s->next = p->next;p->next = s;

Here's a piece of text explaining the big story data structure:

Delete operations such as:

One line of code:

p->next = p->next->next;

Combining the above code and the legend, you can see that the deletion and insertion of a single-linked list is made up of two parts:

    1. The traversal finds the element where the action is needed;
    2. The insert and delete operations are then performed.

Here is an excerpt from the "Big Talk Data Structure" analysis:

Full table creation for single-linked lists
Methods the head interpolation method and the tail interpolation method were used.
Head interpolation: The equivalent of a queue-jumping method. Such as

Compared with the head interpolation method, the tail interpolation method is more reasonable.

Whole table deletion of single linked list
Here is an excerpt from the "Big Talk Data Structure" analysis:

Here's a comparison of single-linked lists and sequential tables:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java data structure-single linked list of linear tables LinkedList

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.