Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice

Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list coding skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously defined public methods of the class.

1. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each queued element already provides its own reasonable toString method. 2. void remove(int count) removes the front count elements from the queue throws QueueUnderflowException if less than count elements are in the queue. 3. boolean swapStart() returns false if less than two elements are in the queue, otherwise reverses the order of the front two elements in the queue and returns true. 4. boolean swapEnds() returns false if there are less than two elements in the queue, otherwise swaps the first and last elements of the queue and returns true.

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; }

}

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 Systems Introduction To Databases And Data Warehouses

Authors: Nenad Jukic, Susan Vrbsky, Svetlozar Nestorov

1st Edition

1943153191, 978-1943153190

More Books

Students also viewed these Databases questions

Question

=+Understand the fi eld of comparative IHRM.

Answered: 1 week ago