Docsity

Linked Lists in Programming II, Study notes of Computer Science

Partial preview of the text

Download Linked Lists in Programming II and more Study notes Computer Science in PDF only on Docsity!

Intro to Programming II^ Linked Lists

Chris BrooksDepartment of Computer ScienceUniversity of San Francisco

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

o)^ {^ ...^

public^ void

insertAt(String

o,^ int^ index)

{^ ...^ }

public^ Object

get(int^ index)

{^ ...^ }

public^ Object

remove(int

index)^ {^

...^ }

public^ int

find(String

o)^ {^ ...^

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

o)^ {

ListItem^ l =

new^ ListItem(o);

l.next^ =^ head;head^ =^ l; }

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

name)^ {

ListItem^ newItem

=^ new^ ListItem(name); ListItem^ current

=^ head; if^ (newItem.data.compareTo(current.data)

<=^ 0)^ {

insert(name); } while^ (current.next.data.compareTo(newItem.data)

<^ 0)^ {

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))

head^ =^ head.next;return^ current.data; } else^ {^ while^ ((current.next

!=^ null)^

!(current.next.data.equals(name)))

current^ =^

current.next;

} String^ rval

=^ current.next.data; current.next

=^ current.next.next;

return^ rval; }

Department of Computer Science โ€” University of San Francisco โ€“ p.17/