Data structure (Java language)--queue simple implementation

Source: Internet
Author: User
Tags iterable null null

Like stacks, queue queues are also tables. However, when the queue is used, the insertion is made at one end and deleted at the other end.

The basic operation of the queue is Enqueue (queued) and dequeue (out), and the team is inserting an element at the end of the rear (the table), and the exit is the element that is deleted at the head of the team front (the beginning of the table).

As with stacks, any table implementation is possible for the queue, and for each operation, the list implementation and array implementations are fast O (1) time. The following main discussion is the loop array implementation of the queue.

For each queue array structure, we keep an array of thearray[] and positions front and rear, which represent both ends of the queue. We also want to record the number of elements that actually exist in the queue currentsize. To queue an element x, we execute enqueue () to increase the currentsize and rear by 1 and then place the thearray[rear]=x. To make the element out of the queue, we set the return value to Thearray[front], and currentsize minus 1, then the front plus 1. The following are the relevant code implementations:

Import Java.util.iterator;import Java.util.nosuchelementexception;public class Myqueue<anytype> implements iterable<anytype> {private static final int default_capacity = 10;private anytype[] thearray;//storing data in an array private int CurrentSize; The length of the current queue, non-array length private int front; Team head private int rear; Team Tail/** * constructor automatically executes the clear () function */public Myqueue () {clear ();} /** * Allocates memory, initializes each member variable */public void Clear () {ensurecapacity (default_capacity); currentsize = 0;front = Rear = 0;} /** * Returns the current array length * * @return array length */public int size () {return thearray.length;} /** * Current queue is full * * @return */public boolean isfull () {return currentsize = = size ();} /** * Current queue is empty * * @return */public boolean isEmpty () {return currentsize = = 0;} /** * Determines the array length according to the parameters * * @param newcapacity * New size */@SuppressWarnings ("unchecked") public void ensurecapacity (int newcapacity) {//First time (array unassigned) directly allocates memory if (TheArray = = null) {TheArray = (anytype[]) new Object[newcapacity];return;} parameter is less than the current length directly returns if (Newcapacity < size ()) {return;} When the array is expanded, the original data is copied into the newly allocated array anytype[] old = Thearray;thearray = (anytype[]) new object[newcapacity];for (int i = front; I < F Ront + old.length; i++) {Thearray[i-front] = old[i% old.length];} Re-assign each variable front = 0;rear = Old.length-1;currentsize = Old.length;} /** * Queued Operation * * @param value * enqueue element */public void Enqueue (AnyType value) {//check queue is full if (Isfull ()) {Ensurecapaci Ty (Size () * 2);} The first team at the end of the queue without data, directly assigned value. The end of the team is then added with a residual value if (thearray[rear]! = NULL) {rear = (rear + 1)% size ();} Thearray[rear] = value;currentsize++;} /** * Outbound Operation * * @return out team element */public AnyType dequeue () {//If the queue is empty it cannot be out of team if (IsEmpty ()) {throw new NullPointerException ();} Out of the team Operation currentsize--; AnyType value = Thearray[front];thearray[front] = Null;front = (front + 1)% size (); return value;} /** * Implement Iterable interface */public iterator<anytype> Iterator () {return new Queueiterator ();} Private class Queueiterator implements iterator<anytype> {private int current = 0;public Boolean hasnext () {return C urrent < Size ();} PubLic AnyType Next () {if (!hasnext ()) {throw new Nosuchelementexception ();} return thearray[current++];}} public static void Main (string[] args) {myqueue<integer> queue = new myqueue<integer> (); Iterator<integer > Iterator1 = Queue.iterator ();iterator<integer> Iterator2 = Queue.iterator (); Queue.enqueue (1); Queue.enqueue (2); Queue.enqueue (3); Queue.enqueue (4); Queue.enqueue (5); Queue.enqueue (6); Queue.enqueue (7); Queue.enqueue (8); Queue.enqueue (9); Queue.enqueue (ten); Queue.dequeue (); Queue.dequeue (); Queue.dequeue (); Queue.dequeue (); Queue.enqueue (one); Queue.enqueue (12); SYSTEM.OUT.PRINTLN ("Result of queue not exceeding capacity:"); while (Iterator1.hasnext ()) {System.out.print (Iterator1.next () + "");} SYSTEM.OUT.PRINTLN ("The result of" \ n queue exceeds capacity: "); queue.enqueue; queue.enqueue; Queue.enqueue (); while ( Iterator2.hasnext ()) {System.out.print (Iterator2.next () + "");}}}
Execution Result:

Results when the queue does not exceed capacity:
NULL NULL 5 6 7 8 9 10
Results when the queue exceeds capacity:
5 6 7 8 9 Each of them NULL NULL NULL NULL NULL NULL NULL NULL

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

Data structure (Java language)--queue simple implementation

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.