The use of doubly linked list LinkedList

Source: Internet
Author: User

Document class:

classDocument { Public stringTitle {Get;Private Set; }  Public stringContent {Get;Private Set; }  Public bytePriority {Get;Private Set; }  PublicDocument (stringTitlestringContentbytePriority =0)        {             This. Title =title;  This. Content =content;  This. Priority =Priority ; }    }

Prioritydocumentmanager class:

classPrioritydocumentmanager {Private ReadOnlyLinkedlist<document>documentlist; Private ReadOnlyList<linkedlistnode<document>>Prioritynodes;  PublicPrioritydocumentmanager () {documentlist=NewLinkedlist<document>(); Prioritynodes=NewList<linkedlistnode<document>> (Ten);  for(inti =0; I <Ten; i++) {Prioritynodes.add (NewLinkedlistnode<document> (NULL)); }        }         Public voidadddocument (Document doc) {if(doc = =NULL)            {                Throw NewArgumentNullException ("Doc"); } adddocumenttoprioritynode (Doc, Doc.        priority); }        Private voidAdddocumenttoprioritynode (Document Doc,intPriority ) {            if(Priority >9|| Priority <0)            {                Throw NewArgumentException ("priority must be greater than 0 less than 9"); }            if(Prioritynodes[priority]. Value = =NULL)            {                --Priority ; if(Priority >=0) {Adddocumenttoprioritynode (doc, priority); }                Else{documentlist.addlast (doc); Prioritynodes[doc. Priority]=Documentlist.last; }                return; }            Else{LinkedListNode<Document> Prionode =Prioritynodes[priority]; if(Priority = =Doc.                    Priority) {Documentlist.addafter (Prionode, doc); Prioritynodes[doc. Priority]=Prionode.next; }                Else{LinkedListNode<Document> Firstprionode =Prionode;  while(Firstprionode.previous! =NULL&& FirstPrioNode.Previous.Value.Priority = =prioNode.Value.Priority) {Firstprionode=prionode.previous; Prionode=Firstprionode;                    } documentlist.addbefore (Firstprionode, doc); Prioritynodes[doc. Priority]=firstprionode.previous; }            }        }         Public voiddisplayallnodes () {foreach(varDocinchdocumentlist) {Console.WriteLine ("Priority : {0}, Title: {1}", Doc. Priority, Doc.            Title); }        }         PublicDocument GetDocument () {Document Doc=DocumentList.First.Value;            Documentlist.removefirst (); returnDoc; }    }

Application layer:

Prioritydocumentmanager PDM =NewPrioritydocumentmanager (); Pdm. Adddocument (NewDocument (" One","content",3)); Pdm. Adddocument (NewDocument (" Both","content",1)); Pdm. Adddocument (NewDocument ("three","content",2)); Pdm. Adddocument (NewDocument ("Six","content",4)); Pdm. Displayallnodes (); 

Attach the results of the application layer operation:

The use of doubly linked list LinkedList

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.