# CSC 212 – Data Structures Lecture 22: PositionList.

## Presentation on theme: "CSC 212 – Data Structures Lecture 22: PositionList."— Presentation transcript:

CSC 212 – Data Structures Lecture 22: PositionList

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?

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

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!

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

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

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

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

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; }

Your Turn Get back into groups and do activity

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