Java implementation array queue, loop array queue, chained queue

Source: Internet
Author: User

/** * File name: Queuetext.java * Time: October 22, 2014 9:05:13 * Xiu Kang */package chapter3;/** * Class Name: Arrayqueue * Description: Array implementation of queue */class array queue<anytype>{private static final int default_capacity = ten; private int front;//team Head private int rear;//team tail Private in T thesize;private anytype[] theitems;public arrayqueue () {clear ();} public void Clear () {front = 0;rear = 0;thesize = 0;ensurecapacity (default_capacity);} public Boolean isEmpty () {return thesize = = 0;//or Front = = rear;} public void Enqueue (AnyType x) {theitems[rear++] = x;thesize++;} Public AnyType dequeue () {if (thesize = = 0) return Null;thesize--;return theitems[front++];} Returns the element that precedes the queue public AnyType element () {if (IsEmpty ()) return Null;return Theitems[front];} /** * Method Name: Ensurecapacity * Description: Ensure capacity */public void ensurecapacity (int newcapacity) {if (Newcapacity < thesize) return; anytype[] Old = Theitems;theitems = (anytype[]) new object[newcapacity];for (int i = 0;i < thesize;i++) Theitems[i] = old [i];}} /** * Class Name: Cirarrqueue * Description: Loop queue (because the queue implemented by the array is moved back when the pointer is deleted this causesThe previous waste, * The loop array can solve this problem, but the loop queue may make the run time doubled) */class cirarrqueue<anytype>{private static final int default_capacity = 3; private int front;//team Head private int rear;//team tail private int thesize;private anytype[] theitems;public cirarrqueue () {clear ();} public void Clear () {Theitems = (anytype[]) New Object[default_capacity];front = 0;rear = 0;thesize = 0;} public Boolean isEmpty () {return thesize = = 0;//or Front = = rear;} public boolean Enqueue (AnyType x) {if (thesize = = default_capacity) return false;theitems[rear++] = x;thesize++;if (Rear = = default_capacity)//If the tail is back to 0rear = 0;return true;} Public AnyType dequeue () {if (thesize = = 0) return null;thesize--; AnyType temp = theitems[front];if (++front = = default_capacity)//If add 1 exceeds the array then return to 0;front = 0;return temp;} Returns the element that precedes the queue public AnyType element () {if (IsEmpty ()) return Null;return Theitems[front];}} /** * Class Name: Linkedqueue * Description: Linked list implementation queue */class Linkedqueue<anytype>{private static class Node<anytype> {Node ( AnyType data, node<anytype> next) {This.datA = Data;this.next = Next;} Private AnyType data;private node<anytype> next;} Private node<anytype> front;private node<anytype> rear;public linkedqueue () {clear ();} public void Clear () {front = Null;rear = null;} public Boolean isEmpty () {return front = = null;} public void Enqueue (AnyType x) {if (front = = Null&&rear = = null) {//at the beginning rear = new node<anytype> (x,null); Front = rear;} else{node<anytype> p = new node<anytype> (x,null); rear.next = P;rear = P;;}} Public AnyType dequeue () {if (!isempty ()) {AnyType x = Front.data;front = Front.next;return x;} return null;} Public AnyType Element () {if (!isempty ()) return Front.data;return null;}} /** * Class Name: Queuetext * Description: Array and list implementation of queue and array implementation of loop queue */public class Queuetext {/** * method Name: Main * Description: Test */public static void main (St Ring[] args) {//TODO auto-generated method stub/**** array queue Test *****//*arrayqueue<integer> q = new arrayqueue< Integer> (); Q.enqueue (1); Q.enqueue (2); Q.enqueue (3); Q.enqueue (4); System.out.println (Q.element ()); while (!q.isempty ()) System.out.println (Q.dequeue ());/********* List queue test **********//*linkedqueue<integer> Q2 = new linkedqueue<integer> (); Q2.enqueue (1); Q2.enqueue (2); Q2.enqueue (3); Q2.enqueue (4); System.out.println (Q2.element ()), while (!q2.isempty ()) System.out.println (Q2.dequeue ());/*********** cyclic queue test ****** /cirarrqueue<integer> q3 = new cirarrqueue<integer> () Q3.enqueue (1); Q3.enqueue (2); Q3.enqueue (3);                               Q3.dequeue (); Q3.enqueue (4);//Set the default capacity of 3, start 3, the queue is full, and then delete the first, the queue array in the first empty out, 4 entered, but still meet the principle of FIFO, plus Size control so that it does not overwrite the back of the Q3.enqueue (5);//does not enter System.out.println (Q3.element ()); while (!q3.isempty ()) System.out.println (Q3.dequeue ());}}

Java implementation array queue, loop array queue, chained queue

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.