Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Q2: 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

Q2: 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Modeling And Design

Authors: Toby J. Teorey, Sam S. Lightstone, Tom Nadeau, H.V. Jagadish

5th Edition

0123820200, 978-0123820204

More Books

Students also viewed these Databases questions