您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。