Java language Implementation queue

Source: Internet
Author: User

What is a queue:

A queue is a linear table that allows only one end of an insert operation, while a delete operation at the other end. A queue is a linear table that is FIFO (first out). One end of the allowed insert is called the tail of the queue, and the one end that allows deletion is called the team header.

Basic Operations for queues:

Enqueue (Object obj): queued operation

Dequeue (): Out of team operation

Loop queue:

Because the queue is in the form of an array, to ensure that the time complexity of the queue is O (1), the elements in the array cannot be moved (if moved, the time complexity becomes O (n)). As a result, the element space behind the stack is wasted. So we're going to connect the ends of the array so that we have a loop, and we call this queue a loop. The structure diagram looks like this:

The judgment queue is empty:The front pointer points to the team head element, and the rear pointer points to the next position of the tail element, so that the front equals rear when the queue is not yet left with an element, but an empty queue. So the empty queue is judged to be front = = rear.

judgment Team is full: when the team is full, we modify its conditions to preserve an element space. In other words, when the queue is full, there is also a free cell in the array. (This is why the size of the array in the following code is 5, but can be inserted up to 4 elements) so the full queue is judged (rear+1)%queuesize = = Front.

The code examples are as follows:

Package queue;/** * Array only stores the size of the array-1 elements, * ensure that rear will not be equal to head after a turn, * that is, when the queue is full, rear+1=head, the middle is just empty an element. * When Rear=head, the queue must be empty. * */public class Queuearray {private object[] objs;private int front;private int rear;public Queuearray () {this (10);} public Queuearray (int size) {Objs = new Object[size];front = 0;rear = 0;} public boolean enqueue (Object obj) {//Determines whether the queue is full if ((rear+1)%objs.length = = Front) {return false;} Objs[rear] = Obj;rear = (rear+1)%objs.length;return  true;} Public Object dequeue () {//Determines whether the queue is empty if (rear = = front) {return null;} Object obj = Objs[front];front = (front+1)%objs.length;return obj;} public void Traverse () {while (front! = rear) {System.out.print (objs[front]+ ""); front = ((front+1)%objs.length);}} public static void Main (string[] args) {Queuearray q = new Queuearray (5); Q.enqueue ("A"); Q.enqueue ("B"); Q.enqueue ("C"); System.out.println ("Deleted elements are:" +q.dequeue ()); Q.enqueue ("F"); SYSTEM.OUT.PRINTLN ("removed element is:" +q.dequeue ()); Q.traverse ();}}
How the queue is chained to storage:

Queue and Team structure chart:

Queue: The new node as the successor of the original node, and then set the new node as the tail node.    This.rear.next = NewNode; This.rear = NewNode;

Outbound: You will need to delete the subsequent direct assignment of nodes to the successor of the head node. This.front.next = Node.next;

The specific code examples are as follows:

Package Queue;public class Linkqueue<t> {private class Node{private T data;private node next;public node () {}}privat E node front;private node rear;private int count;//The number of elements in the queue public linkqueue () {Node p = new Node ();p. data = Null;p.next = N Ull;front = rear = p;} Inserting data using the tail interpolation method public void Equeue (T item) {Node NewNode = new Node (); newnode.data = Item;newnode.next = Null;this.rear.next = n Ewnode;this.rear = newnode;count++;} Public T dequeue () throws Exception{if (IsEmpty ()) {throw new Exception ("Queue is empty!");} T obj; Node node = this.front.next;obj = Node.data;this.front.next = node.next;if (Rear = = node) {rear = front;} Count--;return obj; public int size () {return count;} public Boolean isEmpty () {return front = = rear;} public void Traverse () {Node current = Front.next;while (current! = null) {System.out.println (current.data); Current.next;}} public static void Main (string[] args) throws Exception {linkqueue<string> LQ = new linkqueue<string> (); Lq.eq Ueue ("A"); Lq.equeue ("B"); Lq.equeue ("C"); Lq.equeue (" D "); Lq.traverse (); System.out.println ("The length of the queue is:" +lq.size ()); SYSTEM.OUT.PRINTLN ("removed element is:" +lq.dequeue ()); Lq.traverse ();}}
Whether it is a circular queue or a chain queue, the time is basically a constant time, that is, the time complexity is O (1). However, the loop queue is a pre-applied space and is not released during use. In the case of chained queues, there will be some overhead for each application or release of the node, and there are subtle differences if the queue or team is frequent, but the chain queues are more spatially flexible. Therefore, in cases where the maximum queue length can be determined, a circular queue is recommended, and if the length of the queue cannot be estimated, the chain queue is used.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java language Implementation queue

Related Article

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.