Map Hash Tables and Dictionaries_Part_2

# Map Hash Tables and Dictionaries_Part_2 - addLast k,v n = n...

This preview shows pages 1–5. Sign up to view the full content.

Last Updated: 06/02/12 10:23 PM CSE 2011 Prof. J. Elder - 6 - A Simple List-Based Map We could implement a map using an unsorted list We store the entries of the map in a doubly-linked list S, in arbitrary order S supports the node list ADT (Section 6.2) trailer header nodes/positions entries 9 c 6 b 5 a 8 d

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Last Updated: 06/02/12 10:23 PM CSE 2011 Prof. J. Elder - 7 - The get(k) Algorithm Algorithm get( k ): B = S. positions() { B is an iterator of the positions in S } while B. hasNext() do p = B. next() // the next position in B if p. element() . getKey() = k then return p. element() . getValue() return null {there is no entry with key equal to k }
Last Updated: 06/02/12 10:23 PM CSE 2011 Prof. J. Elder - 8 - The put(k,v) Algorithm Algorithm put( k,v ): B = S. positions() while B. hasNext() do p = B. next() if p. element() . getKey() = k then t = p. element() . getValue() S. set( p, ( k , v )) return t {return the old value} S.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: addLast(( k,v )) n = n + 1 {increment variable storing number of entries} return null {there was no previous entry with key equal to k } Last Updated: 06/02/12 10:23 PM CSE 2011 Prof. J. Elder - 9 - The remove(k) Algorithm Algorithm remove( k ): B = S .positions() while B. hasNext() do p = B. next() if p. element().getKey() = k then t = p. element() . getValue() S. remove( p ) n = n – 1 {decrement number of entries} return t {return the removed value} return null {there is no entry with key equal to k } Last Updated: 06/02/12 10:23 PM CSE 2011 Prof. J. Elder - 10 - Performance of a List-Based Map Performance: put, get and remove take O ( n ) time since in the worst case (the item is not found) we traverse the entire sequence to look for an item with the given key The unsorted list implementation is effective only for small maps...
View Full Document

{[ snackBarMessage ]}

### Page1 / 5

Map Hash Tables and Dictionaries_Part_2 - addLast k,v n = n...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online