Data structure Java implementation--queue "wonderful" two-priority queue

Source: Internet
Author: User

Write in front

There are a lot of times, some data storage not only need to first-out, but also according to the priority of the data to sort, that is, priority must first go out, the priority of the same first-out, this time will be used to the priority queue


Application

In fact, the application of the priority queue is very broad, such as the construction of Huffman tree algorithm, such as in some computer operating systems with priority queue to meet the preemptive multi-tasking operating system, etc.


Code implementation
1. Description of the data element stored by the priority queue
Package Org. stone6762.entity;/** * Description of the data portion of the node in the @ClassName_PriorityQData priority queue * @author_Stone6762 * @CreationTime_2015 Year January 2 pm 3 : 39:55 * @Description_ */public class Priorityqdata {/** @data the data portion of the node */private Object data;/** @priority Priority of the junction */ private int priority;/** @Title constructor */public priorityqdata (Object data, int priority) {super (); this.data = Data;this.prior ity = priority;} Public Object GetData () {return data;} public void SetData (Object data) {this.data = data;} public int getpriority () {return priority;} public void setpriority (int "priority") {this.priority = priority;}}



2. Implementation of priority queue
Package Org. stone6762.mqueue.imple;import org. stone6762.mqueue.mqueue;import org. stone6762.entity.node;import org. stone6762.entity.priorityqdata;/** * @ClassName_PriorityQueue Priority queue * @author_Stone6762 * @CreationTime_2015 Year January 2 pm 3 : 42:55 * @Description_ */public class Priorityqueue implements Mqueue {/** * @front point to the first element of the team */private Node front;/** * @rear Point to the tail element */private Node rear;/** * @bigFront Priority Queue Storage Order _ default is team first small */private boolean bigfront;/** * @Title constructor */public Priorities Queue (Boolean Bigfront) {this (); this.bigfront = Bigfront;} Public Priorityqueue () {this.front = This.rear = null;} @Overridepublic void Clear () {This.front = This.rear = null;} @Overridepublic Boolean isEmpty () {return this.front = = null;} @Overridepublic int Length () {Node T = this.front;int length = 0;while (t! = null) {length++;t = T.getnext ();} return length;} @Overridepublic Object Peek () {if (front! = null) {return front.getdata ();} return null;} @Overridepublic void Offer (Object data) throws Exception {Priorityqdata Tdata = (priorityqdata) data;  Node NewNode = new Node (tdata), if (front = = null) {//special handling of the first element front = rear = NewNode;} else {//1. Locate the appropriate location to insert according to the priority Node Currnode = front, Lastnode = Front;if (!bigfront) {while (Currnode! = null&& tdata.getpriority () >= ((priority Qdata) Currnode.getdata ()). GetPriority ()) {lastnode = Currnode;currnode = Currnode.getnext ();} -----------jump out of the loop there are two cases, one, to the end of the team, two, found the appropriate position} else {while (Currnode! = null&& tdata.getpriority () <= (( Priorityqdata) Currnode.getdata ()). GetPriority ()) {lastnode = Currnode;currnode = Currnode.getnext ();} -----------jump out of the loop there are two cases, one, to the end of the team, two, to find the right position}//2. Classify the inserted position if (Currnode = = null) {//The node should be inserted at the end of the queue Rear.setnext (NewNode); Rear = NewNode;} else if (Currnode = = front) {//The node should be inserted in the first newnode.setnext (front); front = NewNode;} else {//The node is in a position in the middle of the team Newnode.setnext (CU Rrnode); Lastnode.setnext (NewNode);}} @Overridepublic Object Poll () {if (front! = null) {Node T = Front;front = Front.getnext (); return t;} return null;} public void disply (){if (front! = null) {Node t = front;while (t! = null) {Priorityqdata temp = (priorityqdata) t.getdata (); System.out.println ("" +temp.getdata () + "" + temp.getpriority ()); t = T.getnext ();}} else {System.out.println ("queue is empty!! ");}}}






Data structure Java implementation--queue "wonderful" two-priority queue

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.