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

11.1) #1 [RA] Consider the following code which implements the add method in the

ID: 3776751 • Letter: 1

Question

11.1) #1

[RA] Consider the following code which implements the add method in the D List class from the course videos: public void add(int plndex, Integer pData) throws Index0ut0F_3oundsException {if (plndex getSize()) throw new IndexCutOF_3oundsException(); if (plndex == getSize ()) {//adding node to the end of the list Node newNode new Node(pData, getTail(), null); if (isEmpty()) setHead(newNode); else getTail().setNext(newNode); setTail(newNode);) else {Node node getNodeAt(plndex); Node newNode new Node(pData, node.getPrev(), node); if (plndex != 0) node.getPrev().setNext(newNode); node.setPrev(newNode); if (plndex == 0) setHead(newNode); setSize(getSize () + 1); Explain what will happen if the lines indicated are swapped.

Explanation / Answer

In the above setHead (node) function is used to set node as Head node for the list. whereas getTail() returns the last node in the list. And also node.setNext(new) will set new as next node for the node.. These are the main functions involved in the given program

First we will explain what the above given code is doing:

2.1 If the List is empty then newnode will be the head node

Else newnode will be added next to the current Tail node

Else

based on previous node newnode will be placed

But in case if the lines LINE1 and LINE2 are exchanged in the above given code then, the code will be result in

1) If list is empty then getTail().setNext(NewNode) will execute but since the list is empty the getTail() function resutls in NULL and NewNode will be set as the next node for that node. Hence value of first node is NULL and have link to the NewNode this process will be done

2) If list is not empty then Everytime NewNode will be treated as head node such that the generated new node will be the head of list. The last inserted node will be the head all the time for the list,.,..