Lists 2010 Goodrich Tamassia Lists 1 Position ADT

  • Slides: 9
Download presentation
Lists © 2010 Goodrich, Tamassia Lists 1

Lists © 2010 Goodrich, Tamassia Lists 1

Position ADT q q The Position ADT models the notion of place within a

Position ADT q q The Position ADT models the notion of place within a data structure where a single object is stored It gives a unified view of diverse ways of storing data, such as n n q a cell of an array a node of a linked list Just one method: n object element(): returns the element stored at the position © 2010 Goodrich, Tamassia Lists 2

Node List ADT q q q The Node List ADT models a sequence of

Node List ADT q q q The Node List ADT models a sequence of positions storing arbitrary objects It establishes a before/after relation between positions Generic methods: n Accessor methods: n n q Update methods: n n n size(), is. Empty() n © 2010 Goodrich, Tamassia Lists first(), last() prev(p), next(p) set(p, e) add. Before(p, e), add. After(p, e), add. First(e), add. Last(e) remove(p) 3

Doubly Linked List q q A doubly linked list provides a natural implementation of

Doubly Linked List q q A doubly linked list provides a natural implementation of the Node List ADT Nodes implement Position and store: n n n q element link to the previous node link to the next node prev next elem node Special trailer and header nodes/positions header trailer elements © 2010 Goodrich, Tamassia Lists 4

Insertion q We visualize operation insert. After(p, X), which returns position q p A

Insertion q We visualize operation insert. After(p, X), which returns position q p A B C p A q B C X p A © 2010 Goodrich, Tamassia q B Lists X C 5

Insertion Algorithm add. After(p, e): Create a new node v v. set. Element(e) v.

Insertion Algorithm add. After(p, e): Create a new node v v. set. Element(e) v. set. Prev(p) {link v to its predecessor} v. set. Next(p. get. Next()) {link v to its successor} (p. get. Next()). set. Prev(v) {link p’s old successor to v} p. set. Next(v) {link p to its new successor, v} return v {the position for the element e} © 2010 Goodrich, Tamassia Lists 6

Deletion q We visualize remove(p), where p = last() A B C p D

Deletion q We visualize remove(p), where p = last() A B C p D A © 2010 Goodrich, Tamassia B Lists C 7

Deletion Algorithm remove(p): t = p. element {a temporary variable to hold the return

Deletion Algorithm remove(p): t = p. element {a temporary variable to hold the return value} (p. get. Prev()). set. Next(p. get. Next()) {linking out p} (p. get. Next()). set. Prev(p. get. Prev()) p. set. Prev(null) {invalidating the position p} p. set. Next(null) return t © 2010 Goodrich, Tamassia Lists 8

Performance q In the implementation of the List ADT by means of a doubly

Performance q In the implementation of the List ADT by means of a doubly linked list n n The space used by a list with n elements is O(n) The space used by each position of the list is O(1) All the operations of the List ADT run in O(1) time Operation element() of the Position ADT runs in O(1) time © 2010 Goodrich, Tamassia Lists 9