Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I Need the test code fot this ADT queue in Java public class ArrayQueue implements MyQueue { private T[] Q; //used to store data into

I Need the test code fot this ADT queue in Java

public class ArrayQueue implements MyQueue { private T[] Q; //used to store data into this array in a queue manner

private int size; // the total number of elements in the queue private int first; // the index of the first element in the queue private int rear; // the index of the next available element (last one's next) private int front; // the index of the first element in the queue private int back; // the index of the next available element (last one's next) public ArrayQueue() { Q = (T[]) new Object[2]; // initialize the queue as an array of size 2 size = 0; // size is 0 since this is an empty queue // initialize other instance variables: } private void resize(int newLength) {

Q = (T[]) new Object[newLength];

} /** * is empty? */ public boolean isEmpty() { // your implementation here: return size == 0; } /** * @return the number of items in the queue */ public int size() { // your implementation here: return size; }

/** * enqueue: add an element at the tail of queue */ public void enqueue(T item) { // your implementation here: if(size==Q.length); { resize(Q.length*2);} Q[back]=item; back++; back=back%Q.length; size++; }

/** * dequeue: remove and return the head of the queue * @return the deleted value * @throws NoSuchElementException if queue is empty */ public T dequeue() { // your implementation here: if(isEmpty()) {throw new java.util.NoSuchElementException();} T value= Q[front]; front++; front=front%Q.length; size--; return value; }

/** * peek: view the head of queue without removing it. * @return Null if queue is empty */ public T peek() { if (size == 0) {

return null;

}

return Q[front];

} }

public class ListQueue implements MyQueue { private class Node { T item; // public Node next; // null } private Node head = null; private Node tail = null; private int size = 0; /** * is empty? */ public boolean isEmpty() { // your implementation here: return true; } /** * @return the number of items in the queue */ public int size() { // your implementation here: return size; } /** * enqueue: add an element at the tail of queue */ public void enqueue(T item) { Node temp = new Node();

temp.item = item;

if (this.tail == null) {

this.head = this.tail = temp;

return; }

this.tail.next = temp;

this.tail = temp;

size ++; } /** * dequeue: remove and return the head of the queue * @return the deleted value * @throws NoSuchElementException if queue is empty */ public T dequeue() { if (this.head == null)

return null;

Node temp = this.head;

this.head = this.head.next;

if (this.head == null)

this.tail = null;

size --;

return temp.item; } /** * peek: view the head of queue without removing it. * @return Null if queue is empty */ public T peek() { if (head == null) {

return null;

}

return head.item; } }

TEST code

public class Driver{

public static void main(String[] args) { MyQueue q; boolean useList = false; // make it false if you want to array implementation if (useList) q = new ListQueue(); // default setting: use a list to implement the queue. You can change it. else q = new ArrayQueue(); for(int i = 0; i < 1000; i++) // add a large number of items onto the queue { q.enqueue(i); } System.out.println("Now, dequeue items!"); while(!q.isEmpty()) { System.out.print(q.dequeue() + " "); } System.out.println(" End of dequeueing"); // you should fully test/debug all methods!

}

}

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

Records And Database Management

Authors: Jeffrey R Stewart Ed D, Judith S Greene, Judith A Hickey

4th Edition

0070614741, 9780070614741

More Books

Students also viewed these Databases questions