- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace 集合
- {
- class 链表
- {
- public static void Main()
- {
- //LinkedList<T>集合类没有非泛型类的版本,它是一个双向链表,它的元素指向元素的前一个与后一个元素
- //链表的优点是:如果要插入一个元素到链表的中间位置,会非常的快,
- //原因是,如果插入,只需要修改上一个元素的Next 与 下一个元素的Previous的引用则可。
- //像ArrayList列表中,如果插入,需要移动其后的所有元素。
- //链表的缺点是,链表只能是一个接着一个的访问,这样就要用较长的时间来查找定位位于链表中间的元素。
- //LinkedListNode<T>被LinkedList类包含,用LinkedListNode类,可以获得元素的上一个与下一个元素的引用。
- //简单介绍一下LinkedList<T>类的方法与属性
- //Count 返回链表中的元素个数
- //First 返回链表中的第一个节点,其返回的类型是一个节点类LinkedListNode<T>,用它可以迭代集合中的其它节点
- //Last 返回最后一个节点。。。。略
- //AddAfter() AddBefore() AddFirst() AddLast()
- //使用AddXXXX方法,可以在链表中添加元素分别是链表的头部与尾部,还有一个节点的前面与后面
- //Remove() RemoveFirst() RemoveLast() First与Last分别删除链表的头部元素与尾表元素 , Remove是删除指定的一个匹配对像
- //Clear()清除所有的元素
- //Contains() 搜索一个元素,如果找到返回TRUE找不到返回FALSE
- //Find() 从链表头开始找一个元素,并返回他的节点类,LinkedListNode<T>
- //FindLast() 与Find()类似,不同的是从尾部来搜
- //下面开始写示例,示例中使用了一个链表LinkedList<T>与一个列表List<T>
- //链表包含文档,与我们上一个队列的例子相同,但文档有一个优先级。在链表中,文档按优先级来排序,如查多个文档优先级相同
- //则按插入时间来决定优先排序
- //链表添加文档对象时,它们应放在优先级相同的最后一个文档后面,例如:要加一个文档,优先级数是3,那么,我们应放在3级文档组里的最后一个,因为,此时他一定最晚的
- 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
- {
- //docList包含所有的文档
- private readonly LinkedList<Document> documentList ;
- //proiorityNodes包含最多10个元素的引用
- 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));
- }
- }
- /// <summary>
- /// 本方法用来添加文档
- /// </summary>
- /// <param name="d">要添加的文档对象</param>
- public void AddDocument(Document d)
- {
- if (d == null)
- {
- //如果文档为空,抛出异常
- throw new ArgumentNullException("d");
- }
- //调用添加方法
- AddDocumentToPriorityNode(d,d.Priority);
- }
- /// <summary>
- /// 递归方法
- /// </summary>
- /// <param name="d">要添加的文档对象</param>
- /// <param name="p">优先级</param>
- 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);
- }
- }
- //public Document GetDocument()
- //{
- // Document doc = this.documentList.First.Value;
- // this.documentList.RemoveFirst();
- // return doc;
- //}
- }
- }
C#中链表的用使用LinkedList
最新推荐文章于 2026-05-18 08:30:00 发布
本文介绍了一种利用双向链表实现的文档管理系统,该系统能够根据文档的优先级进行排序并高效地添加和检索文档。

1867

被折叠的 条评论
为什么被折叠?



