In: Computer Science
Add The following methods to the LinkedQueue class and create a test driver for each to show 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. a. 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 tha
t use the class. Assue each queued element already provides its own reasonable toString method. b. void remove (int count) removes the front count elements from the queue; throws QueueUnderflowException if less than count elements are in the queue. c. boolean swapStart () returns false if less than two elements are in the queue, otherwise reverse the order of the front two elements in the queue and returns true. d. 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.
LinkedQueue.java:
package ch04.queues;
import support.LLNode;
public class LinkedQueue<T> implements QueueInterface<T> {
protected LLNode<T> front;
protected LLNode<T> rear;
protected int numElements = 0;
public LinkedQueue(){
front = null; rear = null;
}
public void enqueue(T element){
LLNode<T> newNode = new LLNode<T>(element);
if (rear == null)
front = newNode;
else
rear.setLink(newNode);
rear = newNode;
numElements++;
}
public T dequeue(){
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;
}
}
}
LLNode.java:
//----------------------------------------------------------------------------
// LLNode.java by Dale/Joyce/Weems Chapter 2
//
// Implements <T> nodes for a Linked List.
//----------------------------------------------------------------------------
package support;
public class LLNode<T>{
private LLNode<T> link;
private T info;
public LLNode(T info)
{
this.info = info;
link = null;
}
public void setInfo(T info){
// Sets info of this LLNode.
this.info = info;
}
public T getInfo()
// Returns info of this LLONode.
{
return info;
}
public void setLink(LLNode<T> link)
// Sets link of this LLNode.
{
this.link = link;
}
public LLNode<T> getLink()
// Returns link of this LLNode.
{
return link;
}
}
QueueInterface.java
package ch04.queues;
public interface QueueInterface<T> {
void enqueue(T element) throws QueueOverflowException1;
T dequeue() throws QueueUnderflowException;
boolean isFull();
boolean isEmpty();
int size();
}
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
1. QueueInterface.java
package ch04.queues;
public interface QueueInterface<T> {
void remove(int count) throws QueueUnderflowException;
boolean swapStart();
boolean swapEnds();
}
2. LLNode.java
package support;
public class LLNode<T> {
private T element;
private LLNode<T> next;
public LLNode(T element) {
this.element = element;
}
public void setNext(LLNode<T> node) {
this.next = node;
}
public T getElement() {
return element;
}
/**
* @param element the element to set
*/
public void setElement(T element) {
this.element = element;
}
public LLNode<T> getNext() {
return next;
}
}
3. LinkedQueue.java
package ch04.queues;
import support.LLNode;
public class LinkedQueue<T> implements QueueInterface<T> {
protected LLNode<T> front; // reference to the front of this queue
protected LLNode<T> 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) {
LLNode<T> node = new LLNode<T>(element);
if (isEmpty())
front = node;
else
rear.setNext(node);
rear = node;
numElements++;
}
/**
* The remove method removes the front count elements from the queue
*
* @param count
* @throws QueueUnderflowException
*/
@Override
public void remove(int count) throws QueueUnderflowException {
for (int i = 1; i <= count; i++) {
dequeue();
}
}
/**
* The swapStart method 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.
*
* @return
* @throws QueueUnderflowException
*/
public boolean swapStart() {
if (numElements < 2)
return false;
else {
// swap
T firstElement = front.getElement();
T lastElement = front.getNext().getElement();
front.setElement(lastElement);
front.getNext().setElement(firstElement);
return true;
}
}
/**
* The swapEnds method 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
*
* @return
*/
public boolean swapEnds() {
if (numElements < 2)
return false;
else {
T firstElement = front.getElement();
T lastElement = rear.getElement();
front.setElement(lastElement);
rear.setElement(firstElement);
return true;
}
}
public T dequeue() throws QueueUnderflowException {
if (isEmpty())
throw new QueueUnderflowException("Queue Underflow!");
T result = front.getElement();
front = front.getNext();
numElements--;
if (isEmpty())
rear = null;
return result;
}
public boolean isEmpty() {
return (numElements == 0);
}
public String toString() {
String result = "";
LLNode<T> current = front;
while (current != null) {
result = result + (current.getElement()).toString() + " ";
current = current.getNext();
}
return result;
}
}
4. QueueUnderflowException.java
package ch04.queues;
@SuppressWarnings("serial")
public class QueueUnderflowException extends Exception {
private String message;
public QueueUnderflowException(String message) {
this.message=message;
System.out.println(this.message);
}
}
5. LinkedQueueDriver.java
package ch04.queues;
public class LinkedQueueDriver {
public static void main(String[] args) {
try {
LinkedQueue<Integer> linkedQueue = new LinkedQueue<Integer>();
linkedQueue.enqueue(10);
linkedQueue.enqueue(20);
linkedQueue.enqueue(30);
linkedQueue.enqueue(40);
linkedQueue.enqueue(50);
System.out.println("Queue");
System.out.println(linkedQueue);
System.out.println("after swapStart");
linkedQueue.swapStart();
System.out.println(linkedQueue);
System.out.println("after swapEnds");
linkedQueue.swapEnds();
System.out.println(linkedQueue);
System.out.println("after remove 2 count");
linkedQueue.remove(2);
System.out.println(linkedQueue);
linkedQueue.remove(3);
System.out.println("after remove 3 count");
System.out.println(linkedQueue);
linkedQueue.remove(1);
} catch (QueueUnderflowException e) {
e.printStackTrace();
}
}
}