Question
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
public LinkedQueue() { front = null; rear = null; }
public void enqueue(T element) // Adds element to the rear of this queue. { LLNode
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started