Java Analog Queue (general, double-ended, priority queue)

Source: Internet
Author: User

Queue:

First-out, processing similar queuing problems, first row. First processing, the back of the front of the processing is finished, and then processing

The time complexity for insertion and removal operations is O (1). Insert from behind, remove from front

Dual-ended queues:

That is, both sides of the queue are able to insert and remove:insertleft, Insertright. Removeleft, Removeright

Features such as stacks and queues, such as removing Insertleft and removeleft, are the same as stacks. such as removing Insertleft, removeright. That's the same as the queue.

General usage frequency is low, time complexity O (1)

Priority queue:

Internally maintains a sequence sorted by priority. When inserting, you need to find the insertion position, time complexity O (N), delete O (1)

/* * Queue FIFO first. A pointer indicates the position of the insertion, and a pointer indicates the position of the data item being fetched */public class Queueq<t> {private int max;private t[] ary;private int front;//Team head pointer indicates take out  The location of the data item is private int rear; The tail pointer indicates the position of the inserted private int nitems; Number of actual data items public queueq (int size) {This.max = Size;ary = (t[]) New Object[max];front = 0;rear = -1;nitems = 0;} Insert the tail of public void insert (T t) {if (rear = = max-1) {//to the actual end of the team, starting from the beginning rear =-1;} Ary[++rear] = t;nitems++;} Remove team Header public T remove () {T temp = ary[front++];if (front = = max) {//queue to tail, start from front = 0;} Nitems--;return temp;} View Team head public T Peek () {return ary[front];} public Boolean isEmpty () {return nitems = = 0;} public Boolean isfull () {return nitems = = max;} public int size () {return nitems;} public static void Main (string[] args) {queueq<integer> queue = new Queueq<integer> (3), for (int i = 0; i < 5; i++) {Queue.insert (i); SYSTEM.OUT.PRINTLN ("Size:" + queue.size ());} for (int i = 0; i < 5; i++) {Integer peek = Queue.peek (); System.out.println ("Peek:" + peek); SYSTEM.OUT.PRINTLN ("Size:"+ queue.size ());} for (int i = 0; i < 5; i++) {Integer remove = Queue.remove (); System.out.println ("Remove:" + Remove); SYSTEM.OUT.PRINTLN ("Size:" + queue.size ());} SYSTEM.OUT.PRINTLN ("----"); for (int i = 5; i > 0; i--) {Queue.insert (i); SYSTEM.OUT.PRINTLN ("Size:" + queue.size ());} for (int i = 5; i > 0; i--) {Integer peek = Queue.peek (); System.out.println ("Peek:" + peek); SYSTEM.OUT.PRINTLN ("Size:" + queue.size ());} for (int i = 5; i > 0; i--) {Integer remove = Queue.remove (); System.out.println ("Remove:" + Remove); SYSTEM.OUT.PRINTLN ("Size:" + queue.size ());}}}

/* Double-ended queue <span style= "White-space:pre" ></span> both ends insert, delete */public class Queueqt<t> {private LinkedList <T> list;public queueqt () {list = new linkedlist<t> ();} Insert Team Header public void Insertleft (T t) {List.addfirst (t);} Insert team tail public void Insertright (t) {list.addlast (t);} Remove team Header public T Removeleft () {return List.removefirst ();} Remove the tail public T removeright () {return list.removelast ();} View Team Header public T Peekleft () {return List.getfirst ();} View Tail public T peekright () {return list.getlast ();} public Boolean isEmpty () {return list.isempty ();} public int size () {return list.size ();}}
/* Priority queue queues are sorted by priority. is an ordered queue */public class QUEUEQP {private int max;private int[] ary;private int nitems;//Actual data Item number public QUEUEQP (int size) { This.max = Size;ary = new Int[max];nitems = 0;} Insert the tail public void insert (int t) {int j;if (Nitems = = 0) {ary[nitems++] = t,} else {for (j = nItems-1; J >= 0; j--) {i F (T > Ary[j]) {ary[j + 1] = ary[j];//The previous assignment to a small after the equivalent of using the insertion sort. The given sequence is inherently orderly. So efficiency O (N)} else {break;}} Ary[j + 1] = t;nitems++;} System.out.println (arrays.tostring (ary));} Remove the team header public int remove () {return ary[--nitems];//Remove the}//with the lower priority view the lowest priority public int peekmin () {return ary[nitems-1];} public Boolean isEmpty () {return nitems = = 0;} public Boolean isfull () {return nitems = = max;} public int size () {return nitems;} public static void Main (string[] args) {QUEUEQP queue = new QUEUEQP (3); Queue.insert (1); Queue.insert (2); Queue.insert (3); int remove = Queue.remove (); System.out.println ("Remove:" + Remove);}}



Java analog Queue (general, double-ended, 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.