C#中链表的用使用LinkedList

来源:互联网 发布:国家食品药品 数据查询 编辑:程序博客网 时间:2024/06/04 18:35
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace 集合
  6. {
  7.     class 链表
  8.     {
  9.         public static void Main()
  10.         { 
  11.             //LinkedList<T>集合类没有非泛型类的版本,它是一个双向链表,它的元素指向元素的前一个与后一个元素
  12.             //链表的优点是:如果要插入一个元素到链表的中间位置,会非常的快,
  13.             //原因是,如果插入,只需要修改上一个元素的Next 与 下一个元素的Previous的引用则可。
  14.             //像ArrayList列表中,如果插入,需要移动其后的所有元素。
  15.             //链表的缺点是,链表只能是一个接着一个的访问,这样就要用较长的时间来查找定位位于链表中间的元素。
  16.             //LinkedListNode<T>被LinkedList类包含,用LinkedListNode类,可以获得元素的上一个与下一个元素的引用。
  17.             //简单介绍一下LinkedList<T>类的方法与属性
  18.             //Count 返回链表中的元素个数
  19.             //First 返回链表中的第一个节点,其返回的类型是一个节点类LinkedListNode<T>,用它可以迭代集合中的其它节点
  20.             //Last 返回最后一个节点。。。。略
  21.             //AddAfter() AddBefore() AddFirst() AddLast()
  22.             //使用AddXXXX方法,可以在链表中添加元素分别是链表的头部与尾部,还有一个节点的前面与后面
  23.             //Remove() RemoveFirst() RemoveLast() First与Last分别删除链表的头部元素与尾表元素 , Remove是删除指定的一个匹配对像
  24.             //Clear()清除所有的元素
  25.             //Contains() 搜索一个元素,如果找到返回TRUE找不到返回FALSE
  26.             //Find() 从链表头开始找一个元素,并返回他的节点类,LinkedListNode<T>
  27.             //FindLast() 与Find()类似,不同的是从尾部来搜
  28.             //下面开始写示例,示例中使用了一个链表LinkedList<T>与一个列表List<T>
  29.             //链表包含文档,与我们上一个队列的例子相同,但文档有一个优先级。在链表中,文档按优先级来排序,如查多个文档优先级相同
  30.             //则按插入时间来决定优先排序
  31.             //链表添加文档对象时,它们应放在优先级相同的最后一个文档后面,例如:要加一个文档,优先级数是3,那么,我们应放在3级文档组里的最后一个,因为,此时他一定最晚的
  32.             PriorityDocumentManager man = new PriorityDocumentManager() ;
  33.             man.AddDocument( new Document( "一" , "nihao" , 8 ) ) ;
  34.             man.AddDocument( new Document( "二" , "nihao" , 3 ) ) ;
  35.             man.AddDocument( new Document( "三" , "nihao" , 4 ) ) ;
  36.             man.AddDocument( new Document( "四" , "nihao" , 8 ) ) ;
  37.             man.AddDocument( new Document( "五" , "nihao" , 1 ) ) ;
  38.             man.AddDocument( new Document( "六" , "nihao" , 9 ) ) ;
  39.             man.AddDocument( new Document( "七" , "nihao" , 1 ) ) ;
  40.             man.AddDocument( new Document( "八" , "nihao" , 1 ) ) ;
  41.             man.DispalyAllNodes();
  42.             Console.Read();
  43.         }
  44.     }
  45.     class Document
  46.     {
  47.         private string title;
  48.         public string Title
  49.         {
  50.             get { return title; }
  51.             set { title = value; }
  52.         }
  53.         private string content;
  54.         public string Content
  55.         {
  56.             get { return content; }
  57.             set { content = value; }
  58.         }
  59.         private int priority;
  60.         public int Priority
  61.         {
  62.             get { return priority; }
  63.             set { priority = value; }
  64.         }
  65.         public Document(string title, string content, byte priority)
  66.         {
  67.             this.priority = priority;
  68.             this.title = title;
  69.             this.content = content;
  70.         }
  71.     }
  72.     class PriorityDocumentManager
  73.     {
  74.         //docList包含所有的文档
  75.         private readonly LinkedList<Document> documentList ;
  76.         //proiorityNodes包含最多10个元素的引用
  77.         private readonly List<LinkedListNode<Document>> priorityNodes ;
  78.         public PriorityDocumentManager()
  79.         {
  80.             //初始化
  81.             documentList = new LinkedList<Document>();
  82.             priorityNodes = new List<LinkedListNode<Document>>(10);
  83.            
  84.             for (int i = 0; i < 10; i++)
  85.             {
  86.                 priorityNodes.Add(new LinkedListNode<Document>(null));
  87.             }
  88.         }
  89.         /// <summary>
  90.         /// 本方法用来添加文档
  91.         /// </summary>
  92.         /// <param name="d">要添加的文档对象</param>
  93.         public void AddDocument(Document d)
  94.         {
  95.             if (d == null)
  96.             {
  97.                 //如果文档为空,抛出异常
  98.                 throw new ArgumentNullException("d");
  99.             }
  100.             //调用添加方法
  101.             AddDocumentToPriorityNode(d,d.Priority);
  102.         }
  103.         /// <summary>
  104.         /// 递归方法
  105.         /// </summary>
  106.         /// <param name="d">要添加的文档对象</param>
  107.         /// <param name="p">优先级</param>
  108.         private void AddDocumentToPriorityNode(Document doc, int priority)
  109.         {
  110.             if (priority > 9 || priority < 0)
  111.             {
  112.                 throw new ArgumentException( "优先级异常");
  113.             }
  114.             if (this.priorityNodes[priority].Value == null)
  115.             {
  116.                 priority --;
  117.                 if (priority >= 0)
  118.                 {
  119.                     AddDocumentToPriorityNode(doc, priority);
  120.                 }
  121.                 else
  122.                 {
  123.                     this.documentList.AddLast(doc);
  124.                     this.priorityNodes[doc.Priority] = this.documentList.Last;
  125.                 }
  126.                 return;
  127.             }
  128.             else
  129.             {
  130.                 LinkedListNode<Document> priorityNode = this.priorityNodes[priority];
  131.                 if (priority == doc.Priority)
  132.                 {
  133.                     this.documentList.AddAfter(priorityNode, doc);
  134.                     this.priorityNodes[doc.Priority] = priorityNode.Next;
  135.                 }
  136.                 else
  137.                 {
  138.                     LinkedListNode<Document> firstPriorityNode = priorityNode;
  139.                     while (firstPriorityNode.Previous != null && firstPriorityNode.Previous.Value.Priority == priorityNode.Value.Priority )
  140.                     {
  141.                         firstPriorityNode = priorityNode.Previous;
  142.                     }
  143.                     this.documentList.AddBefore(firstPriorityNode, doc);
  144.                     priorityNodes[doc.Priority] = firstPriorityNode.Previous;
  145.                 }
  146.             }
  147.         }
  148.         public void DispalyAllNodes()
  149.         { 
  150.             foreach( Document doc in this.documentList)
  151.             {
  152.                 Console.WriteLine( "文档标题:{0} 文档内容:{1} 优先级:{2}" , doc.Title , doc.Content ,doc.Priority);
  153.             }
  154.         }
  155.         //public Document GetDocument()
  156.         //{
  157.         //    Document doc = this.documentList.First.Value;
  158.         //    this.documentList.RemoveFirst();
  159.         //    return doc;
  160.         //}
  161.     }
  162. }