Question: Improve the efficiency of the get and set methods of Exercise P16.1 by storing (orcaching) the last known (node, index) pair. If n is

Improve the efficiency of the get and set methods of Exercise • P16.1 by storing (or“caching”) the last known (node, index) pair. If n is larger than the last known index, start from the corresponding node instead of the front of the list. Be sure to discard the last known pair when it is no longer accurate. (This can happen when another method edits the list).

Data from exercise P16.1 

Add methods Object get (int n) and void set (int n, Object newElement) to the LinkedList class. Use a helper method that starts at first and follows n links:

private Node getNode(int n)

private Node getNode(int n)

Step by Step Solution

3.44 Rating (157 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To improve the efficiency of the get and set methods in your linked list by caching the last accessed node you would follow these general steps 1 Add ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java Programming Questions!