首页 > 代码库 > 链表+优先级

链表+优先级

==================================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();
        }
    }
}

wKiom1QBoN_jEDXTAAFSaagmLDw100.jpg

本文出自 “程序猿的家” 博客,请务必保留此出处http://962410314.blog.51cto.com/7563109/1546904

链表+优先级