Basic operation of the [data structure] linked list

Source: Internet
Author: User

Linked list

You can use any set of storage units to store data elements in a single-linked list (storage units can be discontinuous), and, in addition to storing the value of each data element AI, you must store information that indicates its immediate successor. These two pieces of information are composed of data elements that the AI's storage image is called a node. A chain of n nodes is called a linked list, which is called a single linked list when the node contains only the information of its successor.

The linked list defines the following data classes to store the node information:

publicclass Node {    null;    int data;    publicNode(int data){        this.data = data;    }}
/** * Basic operation of the list * @author Dream * */ Public  class mylinkelist {Node head =NULL;//Link list header reference    /** * Inserting data into a linked list * @param D: Insert the contents of the data */     Public void AddNode(intd) {Node NewNode =NewNode (d);if(Head = =NULL) {head = NewNode;return; } Node tmp = head; while(Tmp.next! =NULL) {tmp = Tmp.next; }//add node to endTmp.next = NewNode; }/** * * @param index: Delete the first index node * @return: Successful return True, Failure returns false */     PublicBooleanDeletenode(intIndex) {if(Index <1|| Index > Length ()) {return false; }//Delete the first element of the list        if(Index = =1) {head = Head.next;return true; }inti =1;        Node Prenode = head; Node Curnode = Prenode.next; while(Curnode! =NULL) {if(i = = index) {prenode.next = Curnode.next;return true;            } Prenode = Curnode;            Curnode = Prenode.next;        i++; }return true; }/** * * @return Return the length of the node */     Public int length(){intLength =0; Node tmp = head; while(TMP! =NULL) {length++;        TMP = Tmp.next; }returnLength }/** * Sort the linked list * @return return the sorted head node.     PublicNodeorderlist() {Node NextNode =NULL;inttemp =0; Node Curnode = head; while(Curnode.next! =NULL) {nextnode = Curnode.next; while(NextNode! =NULL) {if(Curnode.data > Nextnode.data)                    {temp = Curnode.data;                    Curnode.data = Nextnode.data;                Nextnode.data = temp;            } nextnode = Nextnode.next;        } Curnode = Curnode.next; }returnHead }/** * Print node value * /     Public void printlist() {Node tmp = head; while(TMP! =NULL) {System.out.println (tmp.data);        TMP = Tmp.next; }    } Public Static void Main(string[] args) {Mylinkelist list =NewMylinkelist (); List.addnode (5); List.addnode (3); List.addnode (1); List.addnode (3); System.out.println ("listlen="+ list.length ()); System.out.println ("Before Order:");        List.printlist ();        List.orderlist (); System.out.println ("After order:");    List.printlist (); }}

GitHub Source Address:

Https://github.com/GeniusVJR/Algorithm-and-Data-Structure/tree/master/LinkedList

Basic operation of the [data structure] linked list

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.