Java writes queue using Arrays

Source: Internet
Author: User
package com.alogrithm.structure;import java.util.NoSuchElementException;/** * Make an Queue with group * @author Tim.Feng * * @param <E> */public class ArrayQueue<E> implements Cloneable {private E[]data;// the stucture contains elementsprivate int manyItems;// the number of elementsprivate int front;// the begin index of the dataprivate int rear;// the end index of the datapublic ArrayQueue(){final int INITIAL_CAPACITY = 10;manyItems = 0;data = (E[])new Object[INITIAL_CAPACITY];}public ArrayQueue(int initialCapacity){if(initialCapacity <0){ throw new IllegalArgumentException("initialCapacity is negative" + initialCapacity);} manyItems = 0; data =(E[])new Object[initialCapacity];}public void add(E item){if(manyItems == data.length){// change the capacity of the structure to his twice// But when manyItems*2+1 is bigger than Interger.Max_value will cause stack overflowensureCapacity(manyItems*2 +1);}if(manyItems ==0){front = 0;rear = 0;}else{rear = nextInddex(rear);}data[rear] = item;manyItems++;}public ArrayQueue<E>clone(){ArrayQueue<E> answer;try{answer = (ArrayQueue<E>)super.clone();}catch(CloneNotSupportedException e){ throw new IllegalArgumentException("This class does not implement Cloneable." ); }answer.data = data.clone();return answer;}public int getCapacity(){return data.length;}public boolean isEmpty(){return (manyItems == 0);}// change the capacity of structurepublic void ensureCapacity(int minimumCapacity){E[]biggerArray;int n1,n2;if(data.length>=minimumCapacity){return;}else if(manyItems == 0){data = (E[])new Object[minimumCapacity];}else if(front <= rear){biggerArray = (E[])new Object[minimumCapacity];System.arraycopy(data, front, biggerArray, front, manyItems);data = biggerArray;}else{// the frrst part is from data[front] to the end// the second part is  form data[0] to data[rear]biggerArray = (E[])new Object[minimumCapacity];n1 = data.length - front;n2 = rear + 1;System.arraycopy(data, front, biggerArray, 0, n1);System.arraycopy(data, 0, biggerArray, n1, n2);front = 0;rear = manyItems - 1;data = biggerArray;}}/** * remove the element from queue * @return */public E remove(){E answer;if(manyItems == 0)throw new NoSuchElementException("Queue Underflow:");answer = data[front];front  = nextInddex(front);manyItems --;return  answer;}// return the size of elementspublic int size(){return manyItems;}/** * return the index after rear2 * @param rear2 * @return */private int nextInddex(int rear2) {if(rear2+1 == data.length){return 0;}return rear2+1;}//Trim structure to number of elementpublic void trimToSize(){if(data.length == manyItems){return;}else if(manyItems  == 0){data = (E[])new Object[0];}else if(front<rear){E[]biggerArray = (E[])new Object[manyItems];System.arraycopy(data, front, biggerArray, 0, manyItems);data = biggerArray;}else{E[]biggerArray = (E[])new Object[manyItems];int n1 = data.length - front;int n2 = rear + 1;System.arraycopy(data, front, biggerArray, 0, n1);System.arraycopy(data, 0, biggerArray, n1, n2);front = 0;rear = manyItems -1;data = biggerArray;}}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub}}

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.