Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.

Similar presentations


Presentation on theme: "1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture."— Presentation transcript:

1 1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture 9 Doubly Linked Lists and Ordered Lists

2 2 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures AnnouncementsAnnouncements 1.Final exam is on December 20 at 10:30 (ELAB303). 2.Let me know about conflicts early! 3.Proramming project 2 is up on the Wiki: list implementation 4.The midterm exam is on Thursday October 19th (in class).

3 3 CMPSCI 187 So…… l Most of the interesting work is in the list iterator. l How do we write that? l Make it an inner class……

4 4 CMPSCI 187 The Data Fields of the Iterator

5 5 CMPSCI 187 The KWListIter Class /** Inner class to implement the ListIterator interface */ private class KWListIter implements ListIterator { /** A reference to the next item */ private Node nextItem; /** A reference to the last item returned */ private Node lastItemReturned; /** The index of the current item */ private int index = 0; ………….list iterator methods } //end KWListIter

6 6 CMPSCI 187 The Methods

7 7 CMPSCI 187 A Constructor /** Construct an ListIter that will reference the indexth item @param i - The index of the item to be referenced */ public KWListIter(int i) { // Validate i parameter if (i size) { throw new IndexOutOfBoundsException("Invalid index " + i); } lastItemReturned = null; // Special case of last item if (i == size) { index = size; nextItem = null; } else { // start at the beginning nextItem = head; for (index = 0; index < i; index++) { nextItem = nextItem.next; } Complexity?

8 8 CMPSCI 187 The method ‘add’ l Four separate cases: public void add(Object obj) { if (head == null) { // add to an empty list else if (nextItem == head) { // insert at head else if (nextItem == null) { // insert at tail else { // insert into the middle // Increase size and index size++; index++; }//end add …….code

9 9 CMPSCI 187 Adding to the empty list if (head == null) { // add to an empty list head = new Node(obj); tail = head; }

10 10 CMPSCI 187 Adding to the head New node Step 1Step 2Step 3

11 11 CMPSCI 187 Adding to the head else if (nextItem == head) { // insert at head // Create a new node Node newNode = new Node(obj); // link it to the nextItem newNode.next = nextItem; //step 1 // link nextItem to the new node nextItem.prev = newNode; //step 2 // The new node is now the head head = newNode; //step 3 }

12 12 CMPSCI 187 Adding to the tail New node Step 1 Step 2 Step 3

13 13 CMPSCI 187 Adding to the tail else if (nextItem == null) { // insert at tail // Create a new node Node newNode = new Node(obj); // Link the tail to the new node tail.next = newNode; //step 1 // Link the new node to the tail newNode.prev = tail; //step 2 // The new node is the new tail tail = newNode; //step 3 }

14 14 CMPSCI 187 Adding in the middle Step 1 Step 2 Step 3Step 4 New node

15 15 CMPSCI 187 Adding in the middle else { // insert into the middle // Create a new node Node newNode = new Node(obj); // Link it to nextItem.prev newNode.prev = nextItem.prev; //step 1 nextItem.prev.next = newNode; //step 2 // Link it to the nextItem newNode.next = nextItem; //step 3 nextItem.prev = newNode; //step 4 } Done!

16 16 CMPSCI 187 Advancing the iterator /** Move the iterator forward and return the next item @return the next item in the list @throws NoSuchElementException if there is no such object */ public Object next() { if (!hasNext()) { throw new NoSuchElementException(); } lastItemReturned = nextItem; nextItem = nextItem.next; index++; return lastItemReturned.data; }

17 17 CMPSCI 187 An Application: Ordered Lists l Want to maintain a list of words l Want them in alphabetical order at all times Approach: Develop an OrderedList class F For reuse, good if can work with other types: public interface Comparable { int compareTo(E e); // <0 if this < e // 0 if this == e // >0 if this > e }

18 18 CMPSCI 187 Class Diagram for Ordered Lists (old)

19 19 CMPSCI 187 Skeleton of OrderedList import java.util.*; public class OrderedList > implements Iterable { private LinkedList lst = new LinkedList (); public void add (E e) {...} public E get (int idx) {...} public int size() {...} public boolean remove (E e) {...} public Iterator iterator() {...} }

20 20 CMPSCI 187 Inserting Into an OrderedList l Strategy for inserting new element e: F Find first element in list > e F Insert e before that element l Two cases: F No such element: e goes at the end of the list F Element e2 > e: H Iterator will be positioned after e2, so... H Back up by one and insert e

21 21 CMPSCI 187 Inserting Diagrammed (2)

22 22 CMPSCI 187 Inserting Diagrammed Iterator will be positioned here Should insert new element here

23 23 CMPSCI 187 OrderedList.add public void add (E e) { ListIterator iter = lst.listIterator(); while (iter.hasNext()) { if (e.compareTo(iter.next()) < 0) { // found element > new one iter.previous(); // back up by one iter.add(e); // add new one return; // done } iter.add(e); // will add at end }

24 24 CMPSCI 187 OrderedList.add (variant) public void add (E e) { ListIterator iter = lst.listIterator(); while (iter.hasNext()) { if (e.compareTo(iter.next()) < 0) { // found element > new one iter.previous(); // back up by one break; } iter.add(e); // add where iterator is }

25 25 CMPSCI 187 Other Methods Can Use Delegation public E get (int idx) { return lst.get(idx); } public int size () { return lst.size(); } public boolean remove (E e) { return lst.remove(e); } public Iterator iterator() { return lst.iterator(); }

26 26 CMPSCI 187 Testing OrderedList OrderList test = new OrderedList // Fill with randomly chosen integers Random rand = new Random(); for (int i = 0; i < START_SIZE; ++i) { int val = random.nextInt(MAX_INT); // 0 <= val < MAX_INT test.add(val); } test.add(-1); // adds at beginning test.add(MAX_INT); // adds at end printAndCheck(test);

27 27 CMPSCI 187 Testing OrderedList (2) public static void printAndCheck ( OrderedList test) { int prev = test.get(0); for (int thisOne : test) { System.out.println(thisOne); if (prev > thisOne) System.out.println( “***FAILED, value is “ + thisOne); prev = thisOne; }

28 28 CMPSCI 187 Testing OrderedList (3) // some remove tests: Integer first = test.get(0); Integer last = test.get(test.size()-1); Integer mid = test.get(test.size()/2); test.remove(first); test.remove(last); test.remove(mid); printAndCheck(test);

29 29 CMPSCI 187 The Collection Hierarchy Collection interface, root of the hierarchy implements Iterable AbstractCollection abstract class, holds some shared methods List interface, root of List hierarchy Set interface, root of Set hierarchy Queue interface

30 30 CMPSCI 187 The List Hierarchy: Access By Index List AbstractList abstract class, extends AbstractCollection ArrayList Vector Stack AbstractSequentialList abstract class LinkedList implements Queue

31 31 CMPSCI 187 The Set Hierarchy: Access By Element Set AbstractSet abstract class, extends AbstractCollection HashSet contains(E) is fast (on average) LinkedHashSet iterate in insertion order TreeSet implements SortedSet SortedSet interface, maintains E order

32 32 CMPSCI 187 The Collection Hierarchy

33 33 CMPSCI 187 Common Features of Collections Collection specifies a set of common methods l Fundamental features include: F Collections grow as needed F Collections hold references to objects F Collections have at least two constructors H Create an empty collection of that kind H Create a copy of another collection

34 34 CMPSCI 187 Common Features of Collections

35 35 CMPSCI 187 Implementing a Subclass of Collection Extend AbstractCollection l It implements most operations l You need to implement only: F add(E) F size() F iterator()  An inner class that implements Iterator

36 36 CMPSCI 187 Implementing a Subclass of List Extend AbstractList l You need to implement only: F add(int, E) F get(int) F remove(int) F set(int E) F size() It implements Iterator using the index

37 37 CMPSCI 187 Implementing a Subclass of List (2) Extend AbstractSequentialList l You need to implement only: F listIterator() F size()  An inner class implementing ListIterator


Download ppt "1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture."

Similar presentations


Ads by Google