Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Test contains(), indexOf(), and toArray methods. Linked Queue Code: import exceptions.ArrayCapacityException; import exceptions.EmptyCollectionException; import support.LNode; public class LinkedQueue implements QueueADT { private LNode front; private

image text in transcribed

Test contains(), indexOf(), and toArray methods.

Linked Queue Code:

import exceptions.ArrayCapacityException; import exceptions.EmptyCollectionException; import support.LNode;

public class LinkedQueue implements QueueADT {

private LNode front; private LNode rear; private int size;

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

@Override public void enqueue(T element) { // TODO Auto-generated method stub

LNode newNode = new LNode(element); if (isEmpty()) { rear = newNode; front = newNode; } else { rear.setNext(newNode); rear = newNode; } size++; }

@Override public T dequeue() { // TODO Auto-generated method stub T retData; if (isEmpty()) { throw new EmptyCollectionException("linked queue"); } else {

retData = front.getData(); front = front.getNext(); size--; return retData; } }

@Override public T first() { // TODO Auto-generated method stub if (isEmpty()) { throw new EmptyCollectionException("linked queue"); } else {

return front.getData(); } }

@Override public boolean isEmpty() { // TODO Auto-generated method stub return (size == 0); }

public boolean contains(T elem) { LNode it = front; while (it != null) { if (it.getData().equals(elem)) return true; it = it.getNext(); } return false; } public int indexOf(T element) { int index = 0; LNode curr = front; while (curr != null) { if (curr.getData().equals(element)) return index; index++; curr = curr.getNext(); } return -1; } public T[] toArray(T[] anArray) throws ArrayCapacityException { int count_of_queue = size(); int length_of_given_array = anArray.length; if (length_of_given_array == count_of_queue) { } try { if (anArray != null) { for (int i = 0; i

@Override public int size() { // TODO Auto-generated method stub return size; }

public String toString() { String retString = "["; LNode it = front; while (it != null) { retString += it.getData(); retString += " "; it = it.getNext(); } retString += "]"; return retString; }

}

QueueTester Code:

public class QueueTester {

public static void main(String[] args) { //test both queues

//LinkedQueue lq = new LinkedQueue(); ArrayQueue lq = new ArrayQueue(); System.out.println("Queue: " + lq); lq.enqueue(4); System.out.println("First: " + lq.first()); lq.enqueue(5); System.out.println("First: " + lq.first()); lq.enqueue(6); lq.enqueue(7); System.out.println("Queue: " + lq);

System.out.println("Dequeue [4]: " + lq.dequeue()); System.out.println("First [5]: " + lq.first()); System.out.println("Queue [5 6 7]: " + lq); System.out.println("Dequeue [5]: " + lq.dequeue()); System.out.println("First [6]: " + lq.first()); System.out.println("Queue [6 7]: " + lq); lq.enqueue(8); lq.enqueue(9); System.out.println("Queue [6 7 8 9]: " + lq);

}

}

Modify the Stack tester to include scenarios to test the new methods. Make sure you test multiple states in your stack, such as an empty stack, elements occurring or not occurring in the stack, and returning the array. Use a try/catch block to handle the exceptions appropriately

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 Design Application And Administration

Authors: Michael Mannino, Michael V. Mannino

2nd Edition

0072880678, 9780072880670

More Books

Students also viewed these Databases questions

Question

3. What are potential solutions?

Answered: 1 week ago