Linked List + priority

Source: Internet
Author: User

======================================== Document. CS

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace consoleapplication4 {public class document // document class {Public String title {Get; private set ;}// title Public String content {Get; private set ;} // content public byte priority {Get; private set ;}// priority public document (String title, string content, byte priority) {This. title = title; this. content = content; this. priority = priority;} public override string tostring () {return string. format ("title: {0}, content: {1}, priority: {2}", this. title, this. content, this. priority );}}}

====================================== Prioritydocumentmanage. CS [core]

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 {// linked list private readonly shortlist <document> documentlist; // priority node private readonly list <shortlistnode <document> prioritynodes; public prioritydocumentmanage () {// initialize the linked list documentlist = New priority list <document> (); // initialize the priority node prioritynodes = new list <strong listnode <document> (); // set the priority to 0 ~ 9 For (INT I = 0; I <10; I ++) {prioritynodes. add (New synchronized listnode <document> (null) ;}// Add the document public void adddocument (document D) to the linked list {If (D = NULL) throw new argumentnullexception ("the object cannot be blank"); adddcoumenttoprioritynode (D, D. priority);} private void adddcoumenttoprioritynode (document DOC, int priority) {If (Priority> 9 | priority <0) throw new argumentexception ("Priority overflow"); If (prioritynodes [Priority]. value = NULL) // the value of this priority node is null, indicating that no element of this priority exists in the linked list {-- priority; If (Priority> = 0) // continue to look for {adddcoumenttoprioritynode (Doc, priority) below the lower priority;} else // enter this method, which indicates that it is the first element inserted into the linked list {documentlist. addlast (DOC); // Insert the element to the last position of the linked list prioritynodes [Doc. priority] = documentlist. last; // assign the passed element to the priority node} else {priority listnode <document> currentdoc = prioritynodes [Priority]; If (Doc. priority = Priority) // priority Node The corresponding priority already exists in the element [Priority nodes only store the last element added to the corresponding priority] {documentlist. addafter (currentdoc, DOC); // Insert the element to the end of the corresponding element prioritynodes [Doc. priority] = currentdoc. next; // assign the corresponding priority node to the last added element of the corresponding priority} else // It indicates that the priority node {While (currentdoc. previous! = NULL & currentdoc. previous. value. priority = Priority) // locate the element at the top of the priority {currentdoc = currentdoc. previous;} documentlist. addbefore (currentdoc, DOC); // insert prioritynodes [Doc. priority] = currentdoc. previous; // assign the corresponding priority node to the last added element of the corresponding priority} public ienumerator getenumerator () {return documentlist. getenumerator ();} // locate the first element and delete the public document getdocument () {document D = documentlist. first. value; documentlist. removefirst (); Return D ;}}}

======================================= Main program

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

650) This. width = 650; "Title =" spximage54.jpg "src =" http://s3.51cto.com/wyfs02/M01/47/DB/wKiom1QBoN_jEDXTAAFSaagmLDw100.jpg "alt =" wkiom1qbon_jedxtaafsaagmldw100.jpg "/>

This article from the "programmer's home" blog, please be sure to keep this source http://962410314.blog.51cto.com/7563109/1546904

Linked List + priority

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.