Download presentation

Presentation is loading. Please wait.

Published byEaston Hindson Modified about 1 year ago

1
CSC 212 – Data Structures Lecture 22: PositionList

2
Question of the Day On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door, the host, opens another door which has a goat. He then asks, "Do you change your selection?” Should you?

3
List ADT Accesses all elements in a Collection Add new element before existing one Get the 3 rd element in the Collection Loop over all elements without removing them Cannot be done with Stack, Queue & Deque List ADTs differ how they provide access

4
IndexList != array Extends idea of an array, but… Does not have constant size Elements’ ranks may change over time Abstracts away implementation details Could be done using college students!

5
Insertion add( r, e ) “shifts” elements to make space Can take O(n) time True for array or linked list implementation S 012n r S 012n r S 012n e r

6
Deletion remove( r ) “shifts” remaining elements to fill hole Also takes O(n) time S 012n r S 012n r S 012n r

7
PositionList ADT Abstracts idea of a linked list No ranks or numerical ordering of data Access elements in relative terms Work from first() & last() elements in list Travel to next() & prev() elements Insert elements based on relative location addFirst(e), addLast(e) -- add e as expected addBefore(p,e), addAfter(p,e) -- add e relative to the existing position p remove() & set() works as normal

8
insertAfter(p, X) ABXC ABC p ABC p X p headertrailer headertrailer header

9
remove(p) ABC p headertrailer X ABC headertrailer ABXC p header

10
PositionList Problem For speed, want to use actual Nodes Implementation may use trained monkeys Do not want to expose implementation details Solution: use Position Interface public interface Position { public E element(); } Node class can implement Position But so can other classes Hides exact implementation details

11
Converting Position to Node private Node checkPosition(Position p) throws InvalidPositionException { if (p == null) { throw new InvalidPositionException(); } if (!(p instanceof Node)) { throw new InvalidPositionException(); } return (Node )p; }

12
Your Turn Get back into groups and do activity

13
Before Next Lecture… Keep up with your reading! Finish Week #9 Assignment Work on Programming Assignment #2

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google