Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Queue {
- private Double[] elements;
- private int back;
- private static final int FRONT = 0;
- public static final int DEFAULT_CAPACITY = 10;
- /** Construct a queue with an ad hoc capacity */
- /** Construct a queue with the default capacity */
- /** put a new object at the end of the queue;
- * follow the model of StackOfIntegers to expand the capacity
- * if the queue is full
- */
- int initCapacity;
- public Queue()
- {
- initCapacity = DEFAULT_CAPACITY;
- elements = new Double [initCapacity];
- }
- public Queue(int newValue){
- initCapacity = newValue;
- elements = new Double [initCapacity];
- }
- public void enqueue(Double value) {
- if (back == initCapacity){
- initCapacity = initCapacity * 2;
- Double [] elements2 = new Double [initCapacity];
- for (int i =0; i < elements.length; i++){
- elements2[i] = elements[i];
- }
- elements = elements2;
- }
- elements[back]= value;
- back+=1;
- }
- /** Return and remove the front element from the queue ;
- * this method must call the private method shift()
- */
- public Double dequeue() {
- Double temp = elements[FRONT] ;
- shift();
- back = back - 1;
- // we have to temporarily store the current front of the line
- return temp;
- }
- /** Return the element at the front of the queue for inspection */
- public Double peek() {
- return elements[FRONT];
- }
- /** Test whether the queue is empty */
- public boolean isEmpty() {
- return back == FRONT;
- }
- /** Return the number of elements in the queue */
- public int getSize() {
- return back;
- }
- /** shift everything one position to the front and null out the
- old back of the line */
- private void shift() {
- for ( int i = 1; i < back; i++ )
- elements[i-1] = elements[i] ;
- elements[--back] = null ;
- }
- /** A rough-and-ready way to see what is going on in our queue */
- public void dump() {
- System.out.print( "Elements: " ) ;
- for ( int i = 0; i < elements.length; i++ )
- System.out.print( elements[i] + " " ) ;
- System.out.println() ;
- System.out.println( "Back: " + back ) ;
- }
- }
- public class TestQueueOfDoubles {
- public static void main(String[] args){
- Queue Object = new Queue( 12);
- Queue DefaultObject = new Queue();
- Queue Object2 = new Queue( 14);
- Queue Object3 = new Queue( 9);
- System.out.println(Object.initCapacity);
- System.out.println(DefaultObject.initCapacity);
- System.out.println(Object2.initCapacity);
- System.out.println(Object3.initCapacity);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement