Data Structure Java Implementation -- queue's "wonderful" second-priority queue, java queue
Preface
In many cases, some data storage not only requires FIFO, but also sorting based on the priority of the data, that is, the priority must first go out, the priority of the same FIFO, in this case, the priority queue is used.
Application
In fact, Priority Queues are widely used. For example, the construction of the Harman tree algorithm, and the use of priority queues in some computer operating systems to meet preemptible multi-task operating systems.
Code Implementation
1. Description of data elements stored in the priority queue
Package org. stone6762.entity; /*** @ ClassName_PriorityQData Description of the data section in the node of the priority queue * @ author_Stone6762 * @ creationtime_3:39:55, January 1, January 2, 2015 * @ Description _ */public class PriorityQData {/** @ data node data */private Object data; /** @ priority the priority of the node */private int priority;/** @ Title constructor */public PriorityQData (Object data, int priority) {super (); this. data = data; this. priority = 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_october 3:42:55 * @ Description _ */public class PriorityQueue implements MQueue {/*** @ front points to the first element of the queue * /private Node front; /*** @ rear points to the team end element */private Node rear;/*** @ bigFront priority queue Storage Storage sequence _ The default value is Team Leader */private boolean bigFront;/*** @ Title constructor */public PriorityQueue (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 processing of the first element front = rear = newNode;} else {// 1. locate the proper Node currNode = front, lastNode = front, and if (! BigFront) {while (currNode! = Null & tData. getPriority ()> = (PriorityQData) currNode. getData ()). getPriority () {lastNode = currNode; currNode = currNode. getNext () ;}// ----------- jump out of the loop in two cases, one, to the end of the team, two, found the appropriate location} else {while (currNode! = Null & tData. getPriority () <= (PriorityQData) currNode. getData ()). getPriority () {lastNode = currNode; currNode = currNode. getNext () ;}// ----------- jump out of the loop in two cases, one, to the end of the team, two, find the appropriate position} // 2. classification of inserted locations if (currNode = null) {// This node should be inserted at the end of the rear. setNext (newNode); rear = newNode;} else if (currNode = front) {// This node should be inserted in the first newNode of the team. setNext (front); front = newNode;} else {// This node is in a position in the middle of the queue newNode. setNext (currNode); las TNode. 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 ("the queue is empty !! ");}}}