C#中链表的用使用LinkedList
来源:互联网 发布:国家食品药品 数据查询 编辑:程序博客网 时间:2024/06/04 18:35
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace 集合
- {
- class 链表
- {
- public static void Main()
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- PriorityDocumentManager man = new PriorityDocumentManager() ;
- man.AddDocument( new Document( "一" , "nihao" , 8 ) ) ;
- man.AddDocument( new Document( "二" , "nihao" , 3 ) ) ;
- man.AddDocument( new Document( "三" , "nihao" , 4 ) ) ;
- man.AddDocument( new Document( "四" , "nihao" , 8 ) ) ;
- man.AddDocument( new Document( "五" , "nihao" , 1 ) ) ;
- man.AddDocument( new Document( "六" , "nihao" , 9 ) ) ;
- man.AddDocument( new Document( "七" , "nihao" , 1 ) ) ;
- man.AddDocument( new Document( "八" , "nihao" , 1 ) ) ;
- man.DispalyAllNodes();
- Console.Read();
- }
- }
- class Document
- {
- private string title;
- public string Title
- {
- get { return title; }
- set { title = value; }
- }
- private string content;
- public string Content
- {
- get { return content; }
- set { content = value; }
- }
- private int priority;
- public int Priority
- {
- get { return priority; }
- set { priority = value; }
- }
- public Document(string title, string content, byte priority)
- {
- this.priority = priority;
- this.title = title;
- this.content = content;
- }
- }
- class PriorityDocumentManager
- {
-
- private readonly LinkedList<Document> documentList ;
-
- 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)
- {
- 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( "优先级异常");
- }
- if (this.priorityNodes[priority].Value == null)
- {
- priority --;
- if (priority >= 0)
- {
- AddDocumentToPriorityNode(doc, priority);
- }
- else
- {
- this.documentList.AddLast(doc);
- this.priorityNodes[doc.Priority] = this.documentList.Last;
- }
- return;
- }
- else
- {
- LinkedListNode<Document> priorityNode = this.priorityNodes[priority];
- if (priority == doc.Priority)
- {
- this.documentList.AddAfter(priorityNode, doc);
- this.priorityNodes[doc.Priority] = priorityNode.Next;
- }
- else
- {
- LinkedListNode<Document> firstPriorityNode = priorityNode;
- while (firstPriorityNode.Previous != null && firstPriorityNode.Previous.Value.Priority == priorityNode.Value.Priority )
- {
- firstPriorityNode = priorityNode.Previous;
- }
- this.documentList.AddBefore(firstPriorityNode, doc);
- priorityNodes[doc.Priority] = firstPriorityNode.Previous;
- }
- }
- }
- public void DispalyAllNodes()
- {
- foreach( Document doc in this.documentList)
- {
- Console.WriteLine( "文档标题:{0} 文档内容:{1} 优先级:{2}" , doc.Title , doc.Content ,doc.Priority);
- }
- }
-
-
-
-
-
-
- }
- }