Add the following methods to the LinkedQueue class and creat
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<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)
// Adds element to the rear of this queue.
{
LLNode<T> newNode = new LLNode<T>(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;
}
}
Solution
HI Friend, You have not provide all details
You have not provided the structure of LLNode and QueueInterface structure.
Although I have implemented all methods.
You can modify them accrding to structure of LLNode class.
Logic will be same.
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)
// Adds element to the rear of this queue.
{
LLNode<T> newNode = new LLNode<T>(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;
}
public String toString(){
LLNode<T> temp;
String result = \"\";
while(temp != null){
result = result + temp.getData()+\" \";
temp = temp.getLink();
}
return result;
}
public void remove(int count) throws QueueUnderflowException{
// if we have less number of items in queue, throw exception
if(size() < count){
throw new QueueUnderflowException();
}
// remove count number of elements from front
for(int i=1; i<=count; i++)
front = front.getLink();
}
public boolean swapStart(){
// if we have lesser than 2 items in queue, return false
if(size() < 2){
return false;
}
// swapping data of first two elements
T item = front.getLink().getData(); // getting data of second element from front
front.getLink().setData(front.getData()); // setting front\'s data to second element from front
front.setData(item); // setting data to front
return true;
}
public boolean swapEnds(){
// if we have lesser than 2 items in queue, return false
if(size() < 2){
return false;
}
LLNode<T> temp = front;
// moving temp until temp next is not rear
while(temp.getLink() != rear)
temp = temp.getLink();
// now temp pointing to second last element
// swapping data of second last element and rear
T item = temp.getData();
temp.setData(rear.getData());
rear.setData(item);
return true;
}
}



