(Please use Java Eclipse)
QUESTION 1:
For the question below, assume the following implementation of LinkedQueue:
public static final class LinkedQueue<T> implements
QueueInterface<T> {
private Node<T> firstNode;
private Node<T> lastNode;
public LinkedQueue() {
firstNode = null;
lastNode = null;
}
@Override
public T getFront() {
if (isEmpty()) {
return null;
}
return
firstNode.getData();
}
@Override
public boolean isEmpty() {
return firstNode ==
null;
}
@Override
public void clear() {
firstNode = null;
lastNode = null;
}
public class Node<E> {
private E data; // Entry
in bag
private Node<E>
next; // Link to next node
public Node(E
dataPortion) {
this(dataPortion, null);
} // end constructor
public Node(E
dataPortion, Node<E> nextNode) {
data = dataPortion;
next = nextNode;
} // end constructor
public E getData()
{
if (data != null) {
return data;
}
return null;
}
public Node<E>
getNext() {
return next;
}
public void
setNext(Node<E> newNext) {
next = newNext;
}
} // end LinkedQueue
Below, finish creating the enqueue method that has been started for you.
public void enqueuePractice(T newEntry) {
Node<T> newNode = new Node<T>(newEntry);
if (isEmpty()) {
firstNode = newNode;
}
}
QUESTION 2:
For the question below, assume the following implementation of LinkedQueue:
public static final class LinkedQueue<T> implements
QueueInterface<T> {
private Node<T> firstNode;
private Node<T> lastNode;
public LinkedQueue() {
firstNode = null;
lastNode = null;
}
@Override
public T getFront() {
if (isEmpty()) {
return null;
}
return
firstNode.getData();
}
@Override
public boolean isEmpty() {
return firstNode ==
null;
}
@Override
public void clear() {
firstNode = null;
lastNode = null;
}
public class Node<E> {
private E data; // Entry
in bag
private Node<E>
next; // Link to next node
public Node(E
dataPortion) {
this(dataPortion, null);
} // end constructor
public Node(E
dataPortion, Node<E> nextNode) {
data = dataPortion;
next = nextNode;
} // end constructor
public E getData()
{
if (data != null) {
return data;
}
return null;
}
public Node<E>
getNext() {
return next;
}
public void
setNext(Node<E> newNext) {
next = newNext;
}
} // end LinkedQueue
Your task is to create a "priority" enqueue method. If something is enqueued, it normally goes to the back of the line, but in this method it will go to the front of the line.
For example, if you had a linked queue that looked like:
(firstNode) 1 --> 2 --> 3 (lastNode)
and you ran priorityEnqueue(4)
the result would be:
(firstNode) 4 --> 1 --> 2 --> 3 (lastNode)
public void priorityEnqueue(T newEntry) {
Node<T> newNode = new Node<T>(newEntry);
if (isEmpty()) {
lastNode = newNode;
}
}
QUESTION 3:
For the question below, assume the following implementation of LinkedQueue:
public static final class LinkedQueue<T> implements
QueueInterface<T> {
private Node<T> firstNode;
private Node<T> lastNode;
public LinkedQueue() {
firstNode = null;
lastNode = null;
}
@Override
public T getFront() {
if (isEmpty()) {
return null;
}
return
firstNode.getData();
}
@Override
public boolean isEmpty() {
return firstNode ==
null;
}
@Override
public void clear() {
firstNode = null;
lastNode = null;
}
public class Node<E> {
private E data; // Entry
in bag
private Node<E>
next; // Link to next node
public Node(E
dataPortion) {
this(dataPortion, null);
} // end constructor
public Node(E
dataPortion, Node<E> nextNode) {
data = dataPortion;
next = nextNode;
} // end constructor
public E getData()
{
if (data != null) {
return data;
}
return null;
}
public Node<E>
getNext() {
return next;
}
public void
setNext(Node<E> newNext) {
next = newNext;
}
} // end LinkedQueue
Your task is to create an "almostPriority" enqueue method. If something is enqueued, it normally goes to the back of the queue, but in this method it will be set immediately after the first element in the queue.
For example, if you had a linked queue that looked like:
(firstNode) 1 --> 2 --> 3 (lastNode)
and you ran almostPriorityEnqueue(4)
the result would be:
(firstNode) 1 --> 4 --> 2 --> 3 (lastNode)
@SuppressWarnings("unchecked")
public void almostPriorityEnqueue(T newEntry) {
Node<T> newNode = new Node<T>(newEntry);
if (isEmpty()) {
lastNode = newNode;
firstNode = newNode;
}
}
Here is the completed code for each of the required methods for questions 1, 2 and 3. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
//enqueue method for question 1
public void enqueuePractice(T newEntry) {
// creating a new node
Node<T> newNode = new Node<T>(newEntry);
// if queue is empty, adding as both first and last node
if (isEmpty()) {
firstNode = newNode;
lastNode = newNode;
} else {
// otherwise appending next of last node and setting as new last
// node.
lastNode.setNext(newNode);
lastNode = newNode;
}
}
// priorityEnqueue method for question 2
public void priorityEnqueue(T newEntry) {
Node<T> newNode = new Node<T>(newEntry);
// if queue is empty, adding as both first and last node
if (isEmpty()) {
firstNode = lastNode = newNode;
} else {
// otherwise adding before current firstNode and setting as new
// firstNode
newNode.setNext(firstNode);
firstNode = newNode;
}
}
// almostPriorityEnqueue method for question 3
@SuppressWarnings("unchecked")
public void almostPriorityEnqueue(T newEntry) {
Node<T> newNode = new Node<T>(newEntry);
// if queue is empty, adding as both first and last node
if (isEmpty()) {
lastNode = newNode;
firstNode = newNode;
} else {
// adding between firstNode and firstNode.next
newNode.setNext(firstNode.getNext());
firstNode.setNext(newNode);
// if next is null, setting newNode as last node.
if (newNode.getNext() == null) {
lastNode = newNode;
}
}
}
Get Answers For Free
Most questions answered within 1 hours.