Question: Data Structures and Algorithms in Java - NetBeans import java.util.*; public class testPrintQueue{ static Scanner console = new Scanner(System.in); public static void main(String[] args) {

Data Structures and Algorithms in Java - NetBeans

import java.util.*;

public class testPrintQueue{

static Scanner console = new Scanner(System.in);

public static void main(String[] args) {

ArrayQueue myQueue = new ArrayQueue(100);

int num;

while(true){

System.out.print("Enter an integer value (999 to stop): ");

num = console.nextInt();

if(num==999)

break;

myQueue.enqueue(num);

}

System.out.println("Content of myQueue befor printing: ");

System.out.println(myQueue.toString());

//recReversePrintQueue(myQueue);

} // End of main

// *********Write the method here************

//************** End of Method ****************

}// end of testPrintQueue

class ArrayQueue {

// instance variables

/** Default array capacity. */

public static final int CAPACITY = 1000;

private int[] data;

private int front = 0; // index of front

private int qSize = 0; // queue size

// constructors

public ArrayQueue() {

data = new int[CAPACITY];

}

public ArrayQueue(int capacity) {

data = new int[capacity];

}

public int size() {

return qSize;

}

public boolean isEmpty() {

return (qSize == 0);

}

public void enqueue(int e) {

int avail = (front + qSize) % data.length;

data[avail] = e;

qSize++;

}

public int first() {

return data[front];

}

public int dequeue() {

int answer = data[front];

front = (front + 1) % data.length;

qSize--;

return answer;

}

public String toString() {

StringBuilder sb = new StringBuilder("(");

int k = front;

for (int j=0; j < qSize; j++) {

if (j > 0)

sb.append(", ");

sb.append(data[k]);

k = (k + 1) % data.length;

}

sb.append(")");

return sb.toString();

}

}// End of ArrayQueue class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!