这篇文章给大家分享的是有关c#中链表+优先级的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
==================================Document.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { 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; } public override string ToString() { return string.Format("标题:{0},内容:{1},优先级:{2}", this.Title, this.Content, this.Priority); } } }
==================================PriorityDocumentManage.cs【核心】
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication4 { public class PriorityDocumentManage:IEnumerable { //链表 private readonly LinkedList<Document> documentList; //优先级节点 private readonly List<LinkedListNode<Document>> priorityNodes; public PriorityDocumentManage() { //初始化链表 documentList = new LinkedList<Document>(); //初始化优先级节点 priorityNodes = new List<LinkedListNode<Document>>(); //设置优先级为0~9 for (int i = 0; i < 10; i++) { priorityNodes.Add(new LinkedListNode<Document>(null)); } } //向链表中添加文档 public void AddDocument(Document d) { if (d == null) throw new ArgumentNullException("对象不能为空"); AddDcoumentToPriorityNode(d, d.Priority); } private void AddDcoumentToPriorityNode(Document doc, int priority) { if (priority > 9 || priority < 0) throw new ArgumentException("优先级溢出"); if (priorityNodes[priority].Value == null)//该优先级节点的值为空,说明链表中还没有存在该优先级的元素 { --priority; if (priority >= 0)//继续往更低的优先级下面找 { AddDcoumentToPriorityNode(doc, priority); } else//进入此方法,说明是第一个插入链表的元素 { documentList.AddLast(doc);//将元素插入到链表的最后位置 priorityNodes[doc.Priority] = documentList.Last;//把传入的元素赋值给对应优先级的优先级节点 } } else { LinkedListNode<Document> currentDoc = priorityNodes[priority]; if (doc.Priority == priority)//优先级节点存对应的优先级已存在元素【优先级节点只存对应优先级最后添加的元素】 { documentList.AddAfter(currentDoc, doc);//将元素插入到对应元素的后面 priorityNodes[doc.Priority] = currentDoc.Next;//将对应的优先级节点赋值为对应优先级最后添加的元素 } else//说明不是传入元素对应的优先级节点 { while (currentDoc.Previous != null && currentDoc.Previous.Value.Priority == priority)//找到该优先级最前面的元素 { currentDoc = currentDoc.Previous; } documentList.AddBefore(currentDoc, doc);//插入该元素的前面 priorityNodes[doc.Priority] = currentDoc.Previous;//将对应的优先级节点赋值为对应优先级最后添加的元素 } } } public IEnumerator GetEnumerator() { return documentList.GetEnumerator(); } //找到第一个元素,并删除该元素 public Document GetDocument() { Document d = documentList.First.Value; documentList.RemoveFirst(); return d; } } }
==================================主程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { PriorityDocumentManage pdm = new PriorityDocumentManage(); pdm.AddDocument(new Document("a", "a", 5)); pdm.AddDocument(new Document("b", "b", 5)); pdm.AddDocument(new Document("c", "c", 8)); pdm.AddDocument(new Document("d", "d", 5)); pdm.AddDocument(new Document("e", "e", 6)); foreach (var item in pdm) { Console.WriteLine(item); } Console.ReadKey(); } } }
感谢各位的阅读!关于“c#中链表+优先级的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。