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