A queue is a special linear table , except that it allows for deletion only at the front end of the table (front), but in the back-end (rear) of the table, and, like the stack, the queue is a linear table with limited operations. The end of the insert operation is called the tail of the queue, and the end of the delete operation is called the team header. When there are no elements in the queue, it is called an empty queue. the data element of a queue is also known as a queue element. Inserting a queue element in the queue is called queuing, and removing a queue element from the queue becomes the team. Because the queue is only allowed to be inserted at the other end, only the first elements that enter the queue are first removed from the queue, so the queue is also known as the FIFO (Fifo-first in First out)  linear table .   overflow behavior in sequential queues:(1) "underflow" phenomenon: When the queue is empty, make the overflow phenomenon caused by the team operation. "Underflow" is a normal phenomenon and is commonly used as a condition for program control transfer. (2) "True overflow" phenomenon: when the queue is full, do the stack operation to create a space overflow phenomenon. A "true overflow" is an error state that should be managed to avoid. (3) "false overflow" phenomenon: because of the queue and the operation of the team, the end-to-end pointer only increase does not decrease, resulting in the deleted elements of the space can never be re-use. When the actual number of elements in the queue is much smaller than the size of the vector space, it is possible that the tail pointer is beyond the upper bounds of the vector space and cannot be queued. This phenomenon is called "false overflow" phenomenon. figure from BaikeCommon operations for queues:(1) Initialize queue: Initial condition: queue does not exist. Operation Result: An empty team or a queue with a rival is constructed;(2) Queued operation: Initial conditions: Queue exists. Operation Result: To the existing queue, insert an element x to the end of the team, the team changes;(3) Operation: Initial conditions: Queue existence and non-null, operation result: Delete the first element of the team, and return its value, the team changed;(4) Reading team head elements: Initial conditions: Queue existence and non-null, operation result: Read the team head element, and return its value, the team does not change;(5) Team empty operation: Initial conditions: Queue exists, operation result: If the queue is empty return to true, otherwise return to false;(6) Empty queue operation: initial condition: queue exists, operation result: empty queue, Queue length is 0;
(7) Calculate Queue Length: Initial conditions: Queue exists, operation result: Calculates and returns the queue length. the time and space complexity of common operation algorithms are O (1);Implementation code:
Package Com.wxisme.linkqueue;import java.util.scanner;/** * Chain Queue implementation * @time 2015/8/16 0:16 * @author wxisme * */public clas s linkqueue<t> {Private class Node {private T Data;private node Next;public node () {}public node (T data) {This.data = data;}}   Private Node Front;    /* Team head */private Node rear;     /* Team tail */private int size; /* Length */public linkqueue () {front = Null;rear = null;} Public Linkqueue (T data) {front = new Node (data); rear = front;size + +;} The length of the queue public int size () {return size;} Queue from the end of the team public void Add (T data) {if (front = = null) {front = new Node (data); rear = front;size + +;} else {node node = new Node (data); Rear.next = Node;rear = Node;size + +;}} From the team head out of the team public T remove () {T t = null;if (front! = null) {t = Front.data; Node node = Front;front = Front.next;node.next = null;//Releases the reference to the head element, waiting for garbage collection. Size--;} else {try {throw new Exception ("The queue is already empty and the element cannot be removed!") ");} catch (Exception e) {e.printstacktrace ();}} return t;} Read the HEAD element public T Peek () {if (front = = null) {try {throw new Exception ("The queue is empty, cannot read the primitiveOf ");} catch (Exception e) {e.printstacktrace ();} return null;} else {return front.data;}} Empty queue public void clear () {front = rear = Null;size = 0;} Determines whether the null public is a Boolean isEmpty () {return size==0;} Public String toString () {if (IsEmpty ()) {return "[]";} else {StringBuilder str = new StringBuilder ("[" + Front.data); for (Node Node=front.next; Node!=null; Node=node.next) {str.append ("," +node.data);} Str.append ("]"); return str.tostring ();}} /** * Test code * @param args */public static void main (string[] args) {linkqueue<character> queue = new Linkqueue<cha Racter> (); Scanner scan = new Scanner (system.in); for (int i=0; i<5; i++) {queue.add ((char) (' A ' +i)} System.out.println (queue); Queue.remove (); System.out.println (queue); System.out.println ("Queue is empty:" + queue.isempty ()); System.out.println ("Length of queue:" + queue.size ()); System.out.println ("Team head element is:" + queue.peek ()); System.out.println ("Empty queue"); Queue.clear (); System.out.println ("Queue is empty:" + queue.isempty ());}}
Test results:
[A,b,c,d,e]
[B,c,d,e]
Whether the queue is empty: false
Length of queue: 4
The team head element is: b
Empty queue
Whether the queue is empty: true
The implementation of chain queue