07: Linked Lists

Submit this homework using Gradescope. You can type up your answers, or write legibly and scan them. Do not attempt to submit a paper copy in class, as it will not be accepted.


A. (3 points) Assume you have List containing a million elements of type String. Would you generally expect the following operations be faster with an array-based implementation, or faster with a linked-list-based implementation (with only a head pointer, no other instance variables)?

Assume the array-based implementation’s backing array is large enough to hold the new element (no enlarge() needed). Assume each of the following operations takes the same amount of time: array lookups, setting elements of arrays, creating new nodes, traversing a single link in a linked list, and assigning a reference (like, calling setNext on a node).

  1. calling size()
  2. adding an element to the end of the list (add(Marc))
  3. adding an element to the front of the list (add("Marc", 0))

B. (2 points) Write a public int indexOf(String s) method that searches a StringLinkedList for the first occurrence of a value .equal to s, and returns its index. Return -1 if the value is not found. For this question, write the method assuming that it’s part of the StringLinkedList we started in lecture. (The notes are on the schedule page, if you missed class.)