In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how to use the linked list of the C# collection, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to use the linked list of the C# collection. Let's take a look.
LinkedList is a bi-directional linked list whose elements point to the elements before and after it. In this way, you can traverse the linked list forward by moving to the next element and backwards by moving to the previous element.
When a linked list stores an element, it must store not only the value of the element, but also the information of the next element and the previous element of each element. This is why LinkedList contains elements of type LinkedListNode. Using LinkedListNode, you can get the next and previous elements in the list. LinkedListNode defines the properties List,Next,Previous and Value. The List property returns the LinkedList object associated with the node. The Next and Previous attributes are used to traverse the linked list, accessing nodes after and before the current node. The Value attribute returns the element associated with the node, whose type is T.
The advantage of linked lists is that if you insert elements in the middle of the list, using linked lists can be quick. When inserting an element, modify the Next reference of the previous element and the Previous reference of the next element so that they reference the inserted element. In List (https://www.yisu.com/article/244084.htm), you insert an element and you need to move all the elements that follow it.
The disadvantage of linked lists is that linked list elements can only be accessed one after another, which takes a long time to find elements in the middle or tail of the linked list.
Members defined by the LinkedList class can access the first and last elements in the linked list (First and Last)
Insert elements at the specified location: AddAfter (), AddFirst (), and AddLast ()
Delete the elements at the specified location: Remove (), RemoveFirst (), RemoveLast ()
Search: Find (), FindLast ().
Let's use an example to demonstrate 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 time the document was inserted:
The PriorityDocumentManager class uses a linked list LinkedList documentList and a list List priorityNodes, which contains Document objects, and Document objects contain the title and priority of the document. The list List priorityNodes should contain up to 10 elements, each of which is the last document object referencing each priority.
Public class PriorityDocumentManager {private readonly LinkedList documentList; / / priorities 0.9 private readonly List priorityNodes; public PriorityDocumentManager () {documentList = new LinkedList (); priorityNodes = new List (10); for (int I = 0; I
< 10; i++) { priorityNodes.Add(new LinkedListNode(null)); } } public void AddDocument(Document d) { //Contract.Requires(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"); //检查优先级列表priorityNodes中是否有priority这个优先级 if (priorityNodes[priority].Value == null) { //如果没有,递减优先级值,递归AddDocumentToPriorityNode方法,检查是否有低一级的优先级 --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 of priority {LinkedListNode prioNode = priorityNodes [priority] / / prioritization list priorityNodes nodes with this specified priority value There is still a node if with a lower priority value (priority = = doc.Priority) / / if there is a node with this specified priority value {/ / add this node to the linked list documentList.AddAfter (prioNode, doc) / / assign this node to the node with this priority value in the priority list, because the priority node always refers to the last document priorityNodes [doc.Priority] = prioNode.Next of the specified priority node. } else / / if there is a node with a lower priority value {/ / find the first node with this lower priority in the linked list and put the node to be added in front of it LinkedListNode firstPrioNode = prioNode / / through the loop, use Previous to find the first node of this priority 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 element stored in the linked list is 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 () } this is the end of the article on "how to use the linked list of C# collections". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use the linked list of C# collection". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 213
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.