Question: public class ArrayBoundedQueue implements QueueInterface { protected final int DEFCAP = 100; // default capacity protected T[] elements; // array that holds queue elements protected

 public class ArrayBoundedQueue implements QueueInterface { protected final int DEFCAP =

public class ArrayBoundedQueue implements QueueInterface { protected final int DEFCAP = 100; // default capacity protected T[] elements; // array that holds queue elements protected int numElements = 0; // number of elements in this queue protected int front = 0; // index of front of queue protected int rear; // index of rear of queue

public ArrayBoundedQueue() { elements = (T[]) new Object[DEFCAP]; rear = DEFCAP - 1; }

public ArrayBoundedQueue(int maxSize) { elements = (T[]) new Object[maxSize]; rear = maxSize - 1; }

public void enqueue(T element) // Throws QueueOverflowException if this queue is full; // otherwise, adds element to the rear of this queue. { if (isFull()) throw new QueueOverflowException("Enqueue attempted on a full queue."); else { rear = (rear + 1) % elements.length; elements[rear] = element; numElements = numElements + 1; } }

public T dequeue() // Throws QueueUnderflowException if this queue is empty; // otherwise, removes front element from this queue and returns it. { if (isEmpty()) throw new QueueUnderflowException("Dequeue attempted on empty queue."); else { T toReturn = elements[front]; elements[front] = null; front = (front + 1) % elements.length; numElements = numElements - 1; return toReturn; } }

public boolean isEmpty() // Returns true if this queue is empty; otherwise, returns false. { return (numElements == 0); }

public boolean isFull() // Returns true if this queue is full; otherwise, returns false. { return (numElements == elements.length); } public int size() // Returns the number of elements in this queue. { return numElements; } }

package ch04.queues;

import support.LLNode;

public class LinkedQueue implements QueueInterface { protected LLNode front; // reference to the front of this queue protected LLNode rear; // reference to the rear of this queue protected int numElements = 0; // number of elements in this queue

public LinkedQueue() { front = null; rear = null; }

public void enqueue(T element) // Adds element to the rear of this queue. { LLNode newNode = new LLNode(element); if (rear == null) front = newNode; else rear.setLink(newNode); rear = newNode; numElements++; }

public T dequeue() // Throws QueueUnderflowException if this queue is empty; // otherwise, removes front element from this queue and returns it. { if (isEmpty()) throw new QueueUnderflowException("Dequeue attempted on empty queue."); else { T element; element = front.getInfo(); front = front.getLink(); if (front == null) rear = null; numElements--; return element; } }

public boolean isEmpty() // Returns true if this queue is empty; otherwise, returns false. { return (front == null); } public boolean isFull() // Returns false - a linked queue is never full. { return false; }

public int size() // Returns the number of elements in this queue. { return numElements; }

}

30. As discussed in this section, instead of having our queue methods throw exceptions in the case of "erroneous" invocations, we could have the queue methods handle the situation themselves. We define the following two "safe" methods: - boolean safeEnqueue (T element) adds element to the rear of the queue; returns true if element successfully added, false otherwise - T safeDequeue ( ) returns nu11 if this queue is empty; otherwise removes front element from this queue and returns it a. Add these operations to the ArrayBoundedQueue class. Create a test driver application to demonstrate that the added code works correctly. b. Add these operations to the LinkedQueue class. Create a test driver application to demonstrate that the added code works correctly

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!