Homework 06: 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. (2.5 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)?
- calling
size()
- adding an element to the end of the list (
add(Marc)
) - adding an element to the front of the list (
add("Marc", 0)
) - removing an element from the front of the list
- removing an element from the end of the list
B. (2 points) Write a public int indexOf(String s)
method that searches the list for the first occurrence of a value equivalent 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 LinkedStringList
we started in lecture. (The notes are linked to from the Schedule page, if you missed class.)