The list is also a basic data structure in our Java, which can be understood as an array structure of arrays of peers, as we know, when we use this set of ArrayList and LinkedList, Always learn the difference between the ArrayList of the underlying array implementation and the LinkedList of the bidirectional list implementation. Here, we are going to speak of a simple one-way list of the implementation, let us understand the list in the implementation of adding or removing changes when it is a operation, in and before the array of additions and deletions to compare, get the conclusion of our study, the efficiency of the array is less than the list structure, check and change efficiency is higher than the list structure!
What is called a one-way list, we can understand a chain linked to a node, starting from the first has a point to the next arrow, that is, the one-way list of each node we can understand as an object, which contains the date content property, but also contains the next object's properties. The code is as follows:
Public classMyList {//define a head node. PrivateNode Head; //the length of the linked list Private intsize; //constructs a node inner class classNode {//data for node storage intdate; //next field of the nodeNode Next; PublicNode (intdate) { This. Date =date; } } /*** Get the length of the list * *@return */ Public intGetSize () {returnsize; } /*** Get the node of the corresponding position according to the subscript * *@paramIndex *@return */ PublicNode Getnodebyindex (intindex) { //determine the validity of index if(Index < 0 | | index > GetSize ()-1) { return NULL; } Else { //To determine whether a linked list is empty if(IsEmpty ()) {return NULL; } Else{Node cur=Head; for(inti = 0; i < index-1; i++) {cur=Head.next; Head=cur; } returncur; } } } /*** Determine if a node exists based on node content * *@paramDate *@return */ Public BooleanContainsintdate) { for(inti = 0; I < GetSize (); i++) { if(Getnodebyindex (i). Date = =date) { return true; } } return false; } /*** Get node position based on node content has returned index, no return-1 * *@paramDate *@return */ Public intGetnodebydate (intdate) { //determine if a node exists Booleanb =contains (date); if(b) { for(inti = 0; I < GetSize (); i++) { if(getnodebydate (date) = =date) { returni; } } } return-1; } /*** Determine if the linked list is empty * *@return */ Public BooleanIsEmpty () {if(GetSize () > 0) { return false; } Else { return true; } } /*** Insert a node in the front of the list * *@paramDate*/ Public voidAddfirstnode (intdate) { //determine if there is a head node by whether the list is empty if(IsEmpty ()) {head=NewNode (date); } Else{node node=NewNode (date); Node.next=Head; } } /*** Add a node at the end of the list * *@paramDate*/ Public voidAddlastnode (intdate) { //Gets the node at the end of the listNode node = getnodebyindex (GetSize ()-1); //Get the node you want to insertNode Nownode =NewNode (date); //Point next on the previous node to the current nodeNode.next =Nownode; } /*** Insert node at index position * *@paramIndex *@paramDate*/ Public voidAddnodebyindex (intIndexintdate) { //make an effective judgment on index if(Index > GetSize () | | | Index < 0) { return; } if(Index = = 0) {addfirstnode (date); } Else if(Index = =GetSize ()) {Addlastnode (date); } Else { //Get Previous nodeNode Node1 = Getnodebyindex (index-1); //gets the currently inserted nodeNode Nownode =NewNode (date); //Get Next nodeNode Node2 = getnodebyindex (index + 1); //link to up and down nodesNode1.next =Nownode; Nownode.next=Node2; } } /*** Delete the corresponding location node according to index * *@paramIndex*/ Public voidDeletenode (intindex) { //Locate the node at the index position to determine if the node is empty if(Getnodebyindex (index)! =NULL) { //if the head node if(Index = = 0) {Head.next=Head; } Else if(Index = = GetSize ()-1) { //gets the previous node and points its next to the emptyGetnodebyindex (GetSize ()-2). Next =NULL; } Else { //Get Previous nodeNode Node1 = Getnodebyindex (index-1); //Get Next nodeNode Node2 = getnodebyindex (index + 1); //Get current nodeNode Nownode =Getnodebyindex (index); //Disconnect the current node and the upper and lower nodesNownode.next =NULL; Node1.next=Node2; } } } /*** Modify the node of index position * *@paramIndex *@paramDate*/ Public voidUpdatenode (intIndexintdate) { //determine if the node exists if(Getnodebyindex (index)! =NULL) { //get node at the corresponding locationNode Nownode =Getnodebyindex (index); //make a modification to its dateNownode.date =date; } }}
In the above code is a simple one-way list implementation, in general we need to note that in the input data, such as subscript, we need to judge the legitimacy of the subscript in the operation, we should consider a variety of anomalies, and then it is relatively simple. Tomorrow we will continue to talk about some simple operational issues with the one-way list, such as the following
1. Finding the number of nodes in a single linked list
2. Find the reciprocal k nodes in a single linked list
3. Find the middle node in a single linked list
4, merge two ordered single linked list, the linked list is still orderly after merging
5, single-linked list reversal
6. Print single linked list from tail to head
7. Determine if a single linked list has a ring
8. The length of the ring is removed from the linked list
9, single linked list, remove the starting point of the ring
10. Determine the first intersection of two single-linked lists
Data structure and algorithm--simple implementation of one-way chain List of chain list one