Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The linked implementation of a queue has two data fields. The field firstNode re

ID: 3578382 • Letter: T

Question

The linked implementation of a queue has two data fields. The field firstNode references the queue's front element; lastNode references the element at the back of the queue. Consider the following method where the object item is of the generic type T; isEmpty () is a method that returns true if the queue is empty; setNextNode (newNode) is a setter method of the private class Node of the linked implementation of a queue, which sets a value to the data field next of the node. public void xxxxxx(T item){Node newNode = new Node(item, null); if (isEmpty()) firstNode = newNode; else lastNode.setNextNode(newNode); lastNode - newNode;} What does the method xxxxxx do? Suggest a self-explaining name for the method.

Explanation / Answer

This method is to insert an item into the linked list.

The steps it is performing are:

1.In this method one argument is coming that is an "item" which is to be inserted into the linked list.

2.Secondly an object is created here with constructor parameters as the new item and null.

3.if and else condition is checking that if the linked list is empty by calling isEmpty() method.

4. In case it is empty then item is inserted in first node.

5. If the list is not empty then it is inserting item in a new node I.e. created next to the last node.

6. The last step is, last node is then pointing to that new node.

The most suitable name for this method is " insertNewItem() ".