Java Single chain Table implementation __java

Source: Internet
Author: User
Tags int size
Java Single link table implementation insert nodes at each point of a single linked list Delete nodes at each point in a single linked list A single linked list is backward traversing a node.

Single linked list is the implementation of stacks, queues, hash tables, and many other data structure of the basis, in this summary of the Java single linked table in different locations inserted nodes, different locations to delete nodes, is the reverse traversal of the node method for later review. I rookie one, if there are mistakes please big guys criticized. a single linked list is inserted into a node. Depending on the insertion position, it can be divided into: Inserting a node at the head node, time complexity O (1), AddFirst (int val) inserting a node at the end node, time complexity O (n), addlast (int val) inserting a node at any location , Time complexity O (n), add (int index,int val) single linked list delete nodes, depending on the insertion position can be divided into: at the end of the node to delete nodes, Time complexity O (1), Deletefirst () at the end of the node to delete nodes, time complexity O (n), Deletelast () deletes a node in any location, time complexity O (n), delete (int index,int val) traversal of a single linked list: forward traversal, display () Backward traversal, Displayfromtail ( )

The function of the stack (first in and out) can be realized by the single linked list, and the AddFirst () and Deletelast () methods are used to encapsulate the push () and Pop () method.
The function of queue (first in first out) can be realized by using AddFirst () and Deletefirst () method to be encapsulated as Enqueue () and Dequeue () method.

The code is as follows:

Package Chapter.two.bean; 
        public class ListNode {/*** * node internal class * @author Janet * * */private class node{int val;
        Node Next;
        Node (int val) {this.val = val;
    The}//private field is privately Node-a-null; 

    private int N = 0;
    /*** * To determine whether the list is empty * @return/public Boolean isempty () {return n==0;
    /** * Returns the list knot number * @return * */public int size () {return N;  /*** * add element at header node, time complexity O (1) * @param val/public void AddFirst (int val) {if IsEmpty ()
            {i = new Node (val);
        First.next = null;
            else {node Newfirst = new node (val);
            Newfirst.next = A;
        i = Newfirst;
    } n++;  /*** * adds element at the end of the list, time complexity O (n) * @param val/public void addlast (int val) {if IsEmpty () ) {a = newNode (Val);
        First.next = null;
            else {Node p = i;
            while (P.next!= null) {p = p.next;
            //p is the tail node P.next = new Node (val);
        P.next.next = null;
    } n++; /*** * Add elements at any location, time complexity O (n) * @param index * @param val * @throws Exception/Public void Add (int index,int val) throws Exception {if (index>=size () | | index<0) {throw new Except
        Ion ("The list does not exist in this location");
                else {if (index==0) {AddFirst (val);
            Return
            Node p = i;
            for (int i = 1; i < index; i++) {p = p.next;
            Add a new node q = (val) to P;
            Q.next = P.next;
            P.next = q;
        n++; }/*** * Delete header node and return, Time complexity O (1) * @return/public Node delEtefirst () {Node result = i;
        i = First.next;
        n--;
    return result;
        /*** * Deletes the last node and returns, Time complexity O (1) * @return/Public Node deletelast () {node p = single;
        while (P.next.next!= null) {p = p.next;
        //To delete p.next Node result = P.next;
        P.next = null;
        n--;
    return result; /*** * Delete nodes at any point and return, time complexity O (n) * @param index * @return * @throws Exception/public n Ode Delete (int index) throws Exception {if (index<0 | | index>=size ()) {throw new Exception ("Chain
        Table does not exist at this location ");
                } else {if (index==0) {n--;
            return Deletefirst ();
                else {Node p = i;
                for (int i = 1; i < index-1 i++) {p = p.next; //p.next (q) is the node node q = p.next for deletion;
                P.next = Q.next;
                Q.next = null;
                n--;
            return q; }}/*** * forward traversal list/public void display () {for (Node temp = A; temp!=null;
    Temp=temp.next) System.out.println (temp.val);
    /*** * Reverse Traversal link list */public void Displayfromtail () {displayfromtail (a);
                } private void Displayfromtail (Node p) {if (P!= null) {if (P.next!= null) {
            Displayfromtail (P.next);
        } System.out.println (P.val); }       
    }
}
ListNode a private field defined in a linked list classNode first: node N: Number of nodes in the list, obtained by method size ()

The position of the contract list (index) at any point in the insert deletion is from 0 on the Reverse Traversal link List reference to the offer face question 5: Print a list from the end of the head
To reverse traverse the linked list, you can consider forward traversal linked list, and then put the node elements into the stack, because the stack is a form of recursion, you can think of a recursive solution. If the node P has a next field, it is recursively called displayfromtail (P.next).

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.