资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c#中链表+优先级的示例分析-创新互联

这篇文章给大家分享的是有关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 documentList;
        //优先级节点
        private readonly List> priorityNodes;
        public PriorityDocumentManage()
        {
            //初始化链表
            documentList = new LinkedList();
            //初始化优先级节点
            priorityNodes = new List>();
            //设置优先级为0~9
            for (int i = 0; i < 10; i++)
            {
                priorityNodes.Add(new LinkedListNode(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 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#中链表+优先级的示例分析

感谢各位的阅读!关于“c#中链表+优先级的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站标题:c#中链表+优先级的示例分析-创新互联
标题网址:http://cdkjz.cn/article/dcjjcj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220