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

QUESTION 19 Cansider a Queue ADT that used a linked structure to store data usin

ID: 3894469 • Letter: Q

Question

QUESTION 19 Cansider a Queue ADT that used a linked structure to store data using the following (standard) node implementation public class LinearNode t private LinearNode next private T element public LinearNodecT elem) (next- nult element-l public LinearNode getNexto (return next public void setNext(LinearNode node) (next-node public T getElemento ( return element: public void setElement(T elem) ( element- elem Implement the method public void enqueueSecond (T element) which enqueues an element into the second position of a LinkedQueue. Assume that your new method will exist inside of a LinkedQueue class which contains typical member variables: front back, and count. Your method must throw appropriate errors for issues that occur

Explanation / Answer

Here is the required code for the method to add an element to the second position of a Queue. This method will display an error if the front node is empty. i.e. we cannot add an element to the second place if there is no element at the first place. For all other cases, the method adds the new element to second place, next to the front node. If you have any doubts, drop a comment. Thanks.

//method to enqueue an element to the second place in a custom queue implementation

public void enqueueSecond(T element) {

                                if (front == null) {

                                                /**

                                                * Can not be added to second place if there is no element in first

                                                * place

                                                */

                                                System.out.println("Queue is empty");

                                } else {

                                                // creating a LinearNode object

                                                LinearNode<T> newNode = new LinearNode<T>(element);

                                                // getting a reference of current next node of the front node

                                                LinearNode<T> temp = front.getNext();

                                                // setting new node as front's next node

                                                front.setNext(newNode);

                                                // setting the old next node of the front as new node's next

                                                newNode.setNext(temp);

                                                if (temp == null || temp == front) {

                                                                // new back node (second entry)

                                                                back = newNode;

                                                                back.setNext(null);//last node has no next node

                                                }

                                                count++;//incrementing the counter

                                }

                }

I cannot provide any output as the other methods and classes are not defined. But I assure you this method will work fine.