










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











Department of Computer Science โ University of San Francisco โ p.1/
11-0:^ More on linked lists
Recall that a linked list can be thought of as a chain ofListItems.^ Each ListItem points to the next member in the list. The LinkedList class keeps a pointer to the head of the list.
Department of Computer Science โ University of San Francisco โ p.2/
11-2:^ LinkedList
public^ class
LinkedList
public^ ListItem
head^ = null; public^ void
insert(String
public^ void
insertAt(String
o,^ int^ index)
public^ Object
get(int^ index)
public^ Object
remove(int
public^ int
find(String
Department of Computer Science โ University of San Francisco โ p.4/
11-3:^ Adding elements
To add an element at the front, we point the new elementโsโnextโ pointer to whatever head is pointing to, then pointhead to point to the new element. public^ void
insert(String
ListItem^ l =
new^ ListItem(o);
Department of Computer Science โ University of San Francisco โ p.5/
11-5:^ Exercise
So how do we iterate through a list and create a stringrepresenting all the list contents? ListItems? public^ String
toString()
String^ result
ListItem^ current
=^ head; while^ (current
!=^ null)^
result^ +=^
current.data; current^ =^
current.next;
Department of Computer Science โ University of San Francisco โ p.7/
11-6:^ Adding elements
What if we want to add an element in the middle of thelist? First, we find where to put it, then we insert as if this wasthe front.
Department of Computer Science โ University of San Francisco โ p.8/
11-8:^ Adding elements by position
Is there any way that this code could fail?
Department of Computer Science โ University of San Francisco โ p.10/
11-9:^ Adding elements by position
Is there any way that this code could fail? What if^ index
is larger than the number of elements in the list?
Department of Computer Science โ University of San Francisco โ p.11/
11-11:^ Adding by order
What if I want to add based on order?^ Say we want to keep the list alphabetized? Look through until I find the right place. But: I need to be careful - I canโt โback upโ in the list.
Department of Computer Science โ University of San Francisco โ p.13/
11-12:^ Adding by order
public^ void
insertAlpha(String
ListItem^ newItem
=^ new^ ListItem(name); ListItem^ current
=^ head; if^ (newItem.data.compareTo(current.data)
current^ =^
current.next;
Department of Computer Science โ University of San Francisco โ p.14/
11-14:^ Removing an element
Removing is similar to finding by order. Conceptually, we need to link the item before the one tobe removed to the item after the one to be removed. Remember: we can only go in one direction!
Department of Computer Science โ University of San Francisco โ p.16/
11-15:^ Removing an element
public^ String
remove(String
name) ListItem^ current
=^ head; if^ (current.data.equals(name))
!=^ null)^
!(current.next.data.equals(name)))
current^ =^
current.next;
=^ current.next.data; current.next
=^ current.next.next;
Department of Computer Science โ University of San Francisco โ p.17/