I. Consider the definition of LinkedBag\'s add method that appears in Segment 3.
ID: 3908419 • Letter: I
Question
I. Consider the definition of LinkedBag's add method that appears in Segment 3.12 in the book. Interchange the second and third statements in the method's body, as follows: firstNode newNode newNode.next firstNode; a. What is displayed by the following statements in a client of the modified LinkedBag? Baginterface myBag = new LinkedBag?(); nyBag. add ("30myBag.add("40")7 myBag.add50") myBag. add ("10") myBag.add("60") myBag.add(20") int numberofEntries myBag.getCurrentsize object[] entries nyBag.toArray for int index 0: indexExplanation / Answer
firstNode = newNode; Now original firstNode is gone after the above statement and firstNode of class points to the newNode. newNode.next = firstNode; It means the next pointer of newNode start pointing to the firstNode, but do note that in first statement, firstNode become same as newNode only.. so It means, newNode starts pointing to itself. After both of the above statements, there remains only one node and that starts pointing to itself. a) So in mybag, the last add statement which puts 20 in the list, will only remain. The all other values which was kept earlier, gets vanished due to above code issue. you have not provided the complete code of linkedBag class. so its tough to answer your further question. still i am trying: If inside add method, we increment size each time, then size of bag will be shown as 6. As there is just one entry , myBag.arrray will give one element array containing only 20. When we iterate for 6 entries, we will get ArrayIndexOutOfBound exception, because there is only 1 element in array and count is 6. b) As shown above, getCurrentSize method is affected.