C # linked list of a set,

Source: Internet
Author: User

C # linked list of a set,

The linked list <T> is a two-way linked list. Its elements point to the elements before and after it. In this way, by moving to the next element, You can forward the traversal chain table, and move to the previous element to reverse the traversal chain table.
  
When storing elements, a linked list not only stores the values of elements, but also stores the information of the next and previous elements of each element. This is why the listlist <T> contains elements of the listnode <T> type. Use javaslistnode <T> to obtain the next and previous elements in the list. Inclulistnode <T> defines the attributes List, Next, Previous, and Value. The List attribute returns the vertex List <T> object related to the node. The Next and Previous attributes are used to access the nodes after and before the current node. The Value Attribute returns the elements related to the node, whose type is T.
The advantage of a linked list is that if you insert an element into the center of the list, using the linked list will be very fast. When an element is inserted, the order changes the Next reference of the Previous element and the Previous reference of the Next element so that they reference the inserted element. In List <T> (http://www.cnblogs.com/afei-24/p/6824791.html), insert an element that needs to move the element following it.
The disadvantage of the linked list is that the linked list elements can only be accessed one by one, which takes a long time to find the elements in the middle or end of the linked list.

Member List <T> class-defined members can access the First and Last elements (First and Last) in the linked list );
Insert the following elements at the specified position: AddAfter (), AddFirst (), and AddLast ();
Delete the elements at the specified position: Remove (), RemoveFirst (), RemoveLast ();
Search: Find (), FindLast ().

The following example shows the linked list. In the linked list, documents are sorted by priority. If multiple documents have the same priority, these elements are sorted by the document insertion time:
The PriorityDocumentManager class uses a linked List of objects <Document> documentList and a List of <strong listnode <Document> priorityNodes. The linked List contains Document objects, and the Document object contains the Document title and priority. List <strong listnode <Document> priorityNodes should contain up to 10 elements, each of which is the last Document object that references each priority.
  

  

Public class PriorityDocumentManager {private readonly documentList <Document> documentList; // priorities 0.9 private readonly List <inclulistnode <Document> priorityNodes; public PriorityDocumentManager () {documentList = new external List <Document> (); priorityNodes = new List <external listnode <Document> (10); for (int I = 0; I <10; I ++) {priorityNodes. add (new synchronized listnode <Document> (null) ;}} publi C void AddDocument (Document d) {// Contract. Requires <ArgumentNullException> (d! = Null, "argument d must not be null"); if (d = null) throw new ArgumentNullException ("d"); AddDocumentToPriorityNode (d, d. priority);} private void AddDocumentToPriorityNode (Document doc, int priority) {if (priority> 9 | priority <0) throw new ArgumentException ("Priority must be between 0 and 9"); // check whether the priority list priorityNodes has this priority if (priorityNodes [Priority]. value = null) {// if no Value exists, decrease first Level value, recursive AddDocumentToPriorityNode method, check whether there is a lower priority -- priority; if (priority> = 0) {AddDocumentToPriorityNode (doc, priority );} else // if there is no lower priority, add the node directly to the linked list and add the node to the priority list {documentList. addLast (doc); priorityNodes [doc. priority] = documentList. last;} return;} else // priority list priorityNodes has the priority {priority listnode <Document> prioNode = priorityNodes [priority]; // The priorityNodes parameter in the priority list has the specified priority value If (priority = doc. priority) // If a node with the specified Priority value exists {// Add this node to the documentList of the linked list. addAfter (prioNode, doc); // assign this node to the node in the priority list, because the priority node always references the last document priorityNodes [doc. priority] = prioNode. next;} else // If a node with a lower priority value exists {// find the first node with a lower priority in the linked list, put the node to be added in front of it and upload listnode <Document> firstPrioNode = prioNode; // find the first node with this priority using Previous through the loop while (firstPrioNode. previous! = Null & firstPrioNode. previous. value. priority = prioNode. value. priority) {firstPrioNode = prioNode. previous; prioNode = firstPrioNode;} documentList. addBefore (firstPrioNode, doc); // set a new priority node priorityNodes [doc. priority] = firstPrioNode. previous ;}} public void DisplayAllNodes () {foreach (Document doc in documentList) {Console. writeLine ("priority: {0}, title {1}", doc. priority, doc. title) ;}// returns the document with the highest priority // (that's first in the linked list) public Document GetDocument () {Document doc = documentList. first. value; documentList. removeFirst (); return doc ;}// the elements stored in the linked list are the Document type 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 ;}}

 

Client code:
  

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));          pdm.DisplayAllNodes();          Console.ReadKey();        }

 

Related Article

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.