Data Structure Java Implementation -- queue's "wonderful" second-priority queue, java queue

Source: Internet
Author: User

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 !! ");}}}






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.