Lists 2004 Goodrich Tamassia Lists 1 Position ADT

  • Slides: 10
Download presentation
Lists © 2004 Goodrich, Tamassia Lists 1

Lists © 2004 Goodrich, Tamassia Lists 1

Position ADT (§ 5. 2. 2) The Position ADT models the notion of place

Position ADT (§ 5. 2. 2) 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 a cell of an array a node of a linked list Just one method: n object element(): returns the element stored at the position © 2004 Goodrich, Tamassia Lists 2

List ADT (§ 5. 2. 3) The List ADT models a sequence of positions

List ADT (§ 5. 2. 3) The List ADT models a sequence of positions storing arbitrary objects It establishes a before/after relation between positions Generic methods: n size(), is. Empty() Accessor methods (all return type Position): n n Update methods: n n n © 2004 Goodrich, Tamassia Lists first(), last() prev(p), next(p) replace(p, e) remove(p) insert. Before(p, e) insert. After(p, e) insert. First(e) insert. Last(e) 3

Implementing Vector ADT with List ADT Vector ADTs: n size() and is. Empty() n

Implementing Vector ADT with List ADT Vector ADTs: n size() and is. Empty() n elem. At. Rank(integer r) n object replace. At. Rank(integer r, object o) n insert. At. Rank(integer r, object o) n object remove. At. Rank(integer r) List ADT: n size(), is. Empty() n n n n © 2004 Goodrich, Tamassia Lists first(), last() prev(p), next(p) replace(p, e) remove(p) insert. Before(p, e) insert. After(p, e) insert. First(e) insert. Last(e) 4

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

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

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

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

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

Insertion Algorithm insert. 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} © 2004 Goodrich, Tamassia Lists 7

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

Deletion We visualize remove(p), where p = last() A B C p D A © 2004 Goodrich, Tamassia B Lists C 8

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 © 2004 Goodrich, Tamassia Lists 9

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

Performance 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 © 2004 Goodrich, Tamassia Lists 10