The linked list <t> is a two-way linked list defined by the system. It is linked by javaslistnode <t>. Its element javaslistnode <t> points to the former and later elements.
The advantage of a linked list is that when you want to insert an element to a linked list, you do not need to move all the elements behind the element. Instead, you only need to modify the next and previous values of the next element of the previous element.
The disadvantage of the linked list is that the elements in the linked list can only be accessed one by one, which is time-consuming to search for elements.
The linked list not only stores elements in the list, but also stores the information of the next and previous elements of each element. This is why the listnode upload list <t> contains the upload listnode.
Use the javaslistnode <t> class to obtain the next element and the previous element in the list. Linkedlistnode <t> defines the attributes next, pervious, list, and value, as shown in:
The list attribute returns the listlist object related to the listnode. The Value Attribute returns the value contained in the node. Next and previous indicate the next and previous nodes of the node in the upload list <t> respectively. These two attributes are used in the traversal chain table.
Member List <t> class-defined members can access the first and last elements (first and last) in the linked list and insert elements (addafter, addbefore, addfirst, and addlast) in the specified position) delete the elements at the specified position (the remove, removefirst, and removelast methods), and search for elements from the beginning or end of the linked list (the find and findlast methods ).
Example: List and linkerlist are used to represent documents with priority.
The following is an exampleProgramThe program uses a linked list and a list ). The linked list contains a document with a priority. Documents in the linked list are sorted by priority. If multiple documents have the same priority, they are sorted by the document insertion time.
To quickly access elements with different priorities, we define a list. The 10 elements in the List reference the last document with each priority. Therefore, the list <t> is defined as the list <javaslistnode <document> type. References to the last document of each priority are called priority nodes.
First, define the document class:
Namespace Mydomainlistsample { Public class Document { Public String Title { Get ; Private set ;} Public String Content { Get ;Private set ;} Public byte Priority { Get ; Private set ;} Public Document ( String Title, String Content, Byte Priority ){ This . Title = title; This . Content = content; This . Priority = Priority ;}}}
This class defines the file name, file content, and priority, and provides a constructor to initialize this file.
The core of the solution is the prioritydocumentmanager class. The common interface of this class allows you to add document elements to the linked list. You can retrieve documents with different priorities. This class contains two sets: A list <javaslistnode <document> set of up to 10 Element Types and a set of linkerlist <document> types containing all elements. They are the entry to add a new document with a specified priority.
Both sets are initialized using the prioritydocumentmanager constructor. The list set list <javaslistnode <document> is also initialized using NULL:
Public class Prioritydocumentmanager { Private readonly Shortlist < Document > Documentlist; Private readonly List < Linkedlistnode < Document > Prioritynodes; Public Prioritydocumentmanager (){ This . Documentlist = New Shortlist < Document > (); This . Prioritynodes = New List < Linkedlistnode < Document >>( 10 ); For (Int I = 0; I <10; I ++) {prioritynodes. Add ( New Linkedlistnode < Document > ( Null ));}}
}
Add an adddocument (document) method to the public interface of the class and call the private adddocumenttoprioritynode (document, int priority) method.
Adddocumenttoprioritynode (document, int priority) uses recursion.CodePlace separately:
Public voidAdddocument (DocumentDocument ){If(Document =Null)Throw newArgumentnullexception("Document is null");This. Adddocumenttoprioritynode (document, document. Priority );}
In the adddocumenttoprioritynode method, first check whether the priority is within the permitted range:
If(Priority> 9 | priority <0 ){Throw newArgumentexception("Excellent? Qinge? Yes | this? Between? 9? Between ?. Else");}
If the range is exceeded, an exception is thrown.
Then, check whether a priority node is the same as the passed priority node. If such a node does not exist in the list, call the adddocumenttoprioritynode method recursively to reduce the priority and check whether there are lower-level nodes. If the priority value of the Priority node is different from the transmitted priority value, if there is no node with a lower priority, call the addlast () method to add the document to the end of the linked list safely.
If(This. Prioritynodes [Priority]. value =Null) {-- Priority;If(Priority> = 0) {adddocumenttoprioritynode (document, priority );}Else{Documentlist. addlast (document); prioritynodes [document. Priority] = documentlist. Last ;}} if such a priority node exists, the position of the document inserted in the linked list is found. If a node with a specified priority exists, you can insert the new document to the end of the referenced position by the priority node. Because the priority node always references the last document of the priority value.
If(Priority = Document. Priority) {documentlist. addafter (prionode, document); prioritynodes [document. Priority] = prionode. Next ;}
If the referenced document priority node has a lower priority value, the new document must be inserted before all documents with the same priority value as the priority node. In order to find the first document with the same priority value, the while loop is used, and the previous attribute is used to traverse all linked list nodes, until a linked list node with different priority values is found. In this way, you can locate the inserted node and set the priority node.
Else{Linkedlistnode<Document> Firstprionode = prionode;While(Firstprionode. Previous! =Null& Firstprionode. previous. value. priority = prionode. value. priority) {firstprionode = prionode. previous; prionode = firstprionode;} documentlist. addbefore (firstprionode, document); prioritynodes [document. priority] = firstprionode. previous ;}
Then a displayallnode method is implemented to display all elements in the linked list on the console:
Public voidDisplayallnode (){Foreach(VaRItemInDocumentlist ){Console. Writeline ("Priority {0}, title {1 }", Item. Priority, item. Title );}}
The complete code of the prioritydocumentmanager class is as follows:
Namespace Mydomainlistsample { Public class Prioritydocumentmanager { Private readonly Shortlist < Document > Documentlist;Private readonly List < Linkedlistnode < Document > Prioritynodes; Public Prioritydocumentmanager (){ This . Documentlist = New Shortlist < Document > (); This . Prioritynodes = New List <Linkedlistnode < Document >>( 10 ); For ( Int I = 0; I <10; I ++) {prioritynodes. Add ( New Linkedlistnode < Document > ( Null ));}}
Public void Displayallnode (){ Foreach (VaR Item In Documentlist ){ Console . Writeline ( "Priority {0}, title {1 }" , Item. Priority, item. Title );}} Public Document Getdocument (){ Document Doc = documentlist. First. value; documentlist. removefirst (); Return Doc ;} Public void Adddocument ( Document Document ){ If (Document = Null ) Throw new Argumentnullexception ( "Document is null" ); This . Adddocumenttoprioritynode (document, document. Priority );} Private void Adddocumenttoprioritynode ( Document Document, Int Priority ){ If (Priority> 9 | priority <0 ){Throw new Argumentexception ( "Excellent? Qinge? Yes | this? Between? 9? Between ?. Else" );} If ( This . Prioritynodes [Priority]. value = Null ) {-- Priority; If (Priority> = 0) {adddocumenttoprioritynode (document, priority );} Else {Documentlist. addlast (document); prioritynodes [document. Priority] = documentlist. Last ;}}Else { Linkedlistnode < Document > Prionode = prioritynodes [Priority]; If (Priority = Document. Priority) {documentlist. addafter (prionode, document); prioritynodes [document. Priority] = prionode. Next ;} Else { Linkedlistnode < Document > Firstprionode = prionode; While (Firstprionode. Previous! =Null & Firstprionode. previous. value. priority = prionode. value. priority) {firstprionode = prionode. previous; prionode = firstprionode;} documentlist. addbefore (firstprionode, document); prioritynodes [document. priority] = firstprionode. previous ;}}}}}
Finally, check the results for tonight:
In the main method, add several documents with different priorities for the linked list, and then call the displayallnode method to display the entire linked list:
Static void Main (){ Prioritydocumentmanager PDM = New Prioritydocumentmanager (); PDM. adddocument (New Document ( "One" , "Sample" , 8); PDM. adddocument ( New Document ( "Two" , "Sample" , 3); PDM. adddocument ( New Document ( "Three" , "Sample" , 4); PDM. adddocument ( New Document ( "Four" , "Sample" , 8); PDM. adddocument ( New Document ( "Five" , "Sample" , 1); PDM. adddocument ( New Document ( "Six" , "Sample" , 9); PDM. adddocument ( New Document ( "Seven" ,"Sample" , 1); PDM. adddocument ( New Document ( "Eight" , "Sample" , 1); system. Console . Read ();}
Output:
Note: the code in this article is copied from C # advanced programming. To understand the linked list. What is hard to understand in this example is the adddocumenttoprioritynode method in prioritydocumentmanager. This method uses recursion to find the location to be inserted, and determines based on centralized payment requests.
Technorati label: Linked List