20 - CMPSCI 187: Programming With Data Structures Lecture...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPSCI 187: Programming With Data Structures Lecture #20: Implementing Linked Lists 25 October 2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Implementing Linked Lists • L&C’s Single-Link LinkedList Class • The remove Operation for LinkedList • Doubly Linked Lists and the New remove Using find • The LinkedIterator Class • Linked Ordered Lists • Reviewing the DLDeque From Discussion #6
Background image of page 2
L&C’s Single-Link LinkedList Class • We can use our existing LinearNode class to implement a list, whether ordered or unordered. The Java library uses doubly-linked nodes, as we will see soon, but let’s fnd out what happens with singly-linked nodes. • As we saw when looking at deques, we can add or remove From the Front, or add to the rear, or peek at either, in O(1) time. But the ListADT interFace also needs us to remove an object From the list, including removing From the rear, and to create an iterator object. public class LinkedList<T> implements ListADT<T>, Iterable<T> { protected int count; protected LinearNode<T> head, tail; public LinkedList( ) { count = 0; head = tail = null;}
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The remove Operation for LinkedList • We can’t break out the find part of remove , because we need the node before the one we are looking for. public T remove (T target ) throws EmptyCollectionException, ElementNotFoundException { if (isEmpty( )) throw new EmptyCollectionException (“List”); boolean found = false; LinearNode<T> previous = null; LinearNode<T> current = head; if (target.equals (current.getElement( )) found = true;
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

20 - CMPSCI 187: Programming With Data Structures Lecture...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online