温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#集合的链表怎么用

发布时间:2022-04-12 11:03:39 来源:亿速云 阅读:242 作者:iii 栏目:开发技术

这篇文章主要介绍了C#集合的链表怎么用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇C#集合的链表怎么用文章都会有所收获,下面我们一起来看看吧。

LinkedList<T>是一个双向链表,其元素会指向它前面和后面的元素。这样,通过移动到下一个元素可以正向遍历链表,通过移动到前一个元素可以反向遍历链表。

C#集合的链表怎么用


链表在存储元素时,不仅要存储元素的值,还必须存储每个元素的下一个元素和上一个元素的信息。这就是LinkedList<T>包含LinkedListNode<T>类型的元素的原因。使用LinkedListNode<T>,可以获得列表中的下一个和上一个元素。LinkedListNode<T>定义了属性List,Next,Previous和Value。List属性返回与节点相关的LinkedList<T>对象。Next和Previous属性用于遍历链表,访问当前节点之后和之前的节点。Value属性返回与节点相关的元素,其类型是T。
链表的优点是,如果将元素插入到列表的中间位置,使用链表就会很快。在插入一个元素时,秩序啊哟修改上一个元素的Next引用和下一个元素的Previous引用,使它们引用所插入的元素。在List<T>(https://www.yisu.com/article/244084.htm)中,插入一个元素,需要移动该元素后面的所以元素。
链表的缺点是,链表元素只能一个接一个的访问,这需要较长时间来查找位于链表中间或尾部的元素。
LinkedList<T>类定义的成员可以访问链表中的第一个和最后一个元素(First和Last);
在指定位置插入元素:AddAfter(),AddFirst()和AddLast();
删除指定位置的元素:Remove(),RemoveFirst(),RemoveLast();
搜索:Find(),FindLast()。
下面用一个例子演示链表。在链表中,文档按照优先级来排序。如果多个文档的优先级相同,这些元素就按照文档的插入时间来排序:
PriorityDocumentManager类使用一个链表LinkedList<Document> documentList和一个列表List<LinkedListNode<Document>> priorityNodes,链表包含Document对象,Document对象包含文档的标题和优先级。列表List<LinkedListNode<Document>> priorityNodes应最多包含10个元素,每个元素分别是引用每个优先级的最后一个文档对象。

C#集合的链表怎么用

      public class PriorityDocumentManager           {             private readonly LinkedList<Document> documentList;                             // priorities 0.9             private readonly List<LinkedListNode<Document>> priorityNodes;             public PriorityDocumentManager()             {               documentList = new LinkedList<Document>();               priorityNodes = new List<LinkedListNode<Document>>(10);               for (int i = 0; i < 10; i++)               {                 priorityNodes.Add(new LinkedListNode<Document>(null));               }             }             public 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");               //检查优先级列表priorityNodes中是否有priority这个优先级               if (priorityNodes[priority].Value == null)               {                 //如果没有,递减优先级值,递归AddDocumentToPriorityNode方法,检查是否有低一级的优先级                 --priority;                 if (priority >= 0)                 {                   AddDocumentToPriorityNode(doc, priority);                 }                 else //如果已经没有更低的优先级时,就直接在链表中添加该节点,并将这个节点添加到优先级列表                 {                   documentList.AddLast(doc);                   priorityNodes[doc.Priority] = documentList.Last;                 }                 return;               }               else //优先级列表priorityNodes中有priority这个优先级               {                 LinkedListNode<Document> prioNode = priorityNodes[priority];                 //区分优先级列表priorityNodes存在这个指定的优先级值的节点,还是存在较低的优先级值的节点                 if (priority == doc.Priority)                 // 如果存在这个指定的优先级值的节点                 {                   //将这个节点添加到链表                   documentList.AddAfter(prioNode, doc);                   // 将这个节点赋予优先级列表中的这个优先级值的节点,因为优先级节点总是引用指定优先级节点的最后一个文档                   priorityNodes[doc.Priority] = prioNode.Next;                 }                 else //如果存在较低的优先级值的节点                 {                   //在链表中找到这个较低优先级的第一个节点,把要添加的节点放到它前面                   LinkedListNode<Document> firstPrioNode = prioNode;                     //通过循环,使用Previous找到这个优先级的第一个节点                   while (firstPrioNode.Previous != null &&                      firstPrioNode.Previous.Value.Priority == prioNode.Value.Priority)                   {                     firstPrioNode = prioNode.Previous;                     prioNode = firstPrioNode;                   }                   documentList.AddBefore(firstPrioNode, doc);                   // 设置一个新的优先级节点                   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;             }           }           //存储在链表中的元素是Document类型           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;             }           }

客户端代码:

    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();         }

关于“C#集合的链表怎么用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“C#集合的链表怎么用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI