Write a splice method from the implementation perspective for the LinkedQueue cl
ID: 3714835 • Letter: W
Question
Write a splice method from the implementation perspective for the LinkedQueue class.
The secondQueue should not be altered when the method completes.
Linked Queue:
public class LinkedQueue<T> implements QueueInterface<T>, java.io.Serializable
{
private Node firstNode; // references node at front of queue
private Node lastNode; // references node at back of queue
public LinkedQueue()
{
firstNode = null;
lastNode = null;
} // end default constructor
public void enqueue(T newEntry)
{
Node newNode = new Node(newEntry, null);
if (isEmpty())
firstNode = newNode;
else
lastNode.setNextNode(newNode);
lastNode = newNode;
} // end enqueue
Explanation / Answer
public void splice(LinkedQueue<T> secondQueue)
{
if(isEmpty())
{
firstNode = secondQueue.firstNode;
} else
{
lastNode.next = secondQueue.firstNode;
}
lastNode = secondQueue.lastNode;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.