160 likes | 391 Views
Other Linked Structures. Chapter 9. Variants of Singly-Linked Lists. Purpose : to make algorithms for some basic list operations to be simpler more efficient Linked stacks and queues Linked lists with head and/or trailer nodes Circular linked lists. Linked List Stack.
E N D
Other Linked Structures Chapter 9
Variants of Singly-Linked Lists • Purpose : to make algorithms for some basic list operations to be • simpler • more efficient • Linked stacks and queues • Linked lists with head and/or trailer nodes • Circular linked lists
Linked List Stack • Limitation of stack implemented as an array • fixed size of array limits size of stack • Consider a stack class using a linked list • Only data member needed is pointer to node at top of stack • Constructor merely initialize pointer to NULL • Empty function checks for this • Push function inserts node at beginning of list • Pop function removes node from beginning
Linked List Queue • Array implementation of queue • limited by size of array • Queue class implemented as linked list • Extension of linked list implementation of queue • Data members • pointer to first node (for front() and for removeQ()) • pointer to last node (for addQ())
Hash Tables • Recall search algorithms • Linear search of n items O(n) • Binary search of n items O(log2n) • We seek an algorithm with even better O(f(n)) • Provide a "hash" function • h(val) location • Example val % 25 • Stores numbers in an array indexed 0 .. 25
Hash Tables • Collisions • When val1 != val2 buth(val1) == h(val2) • Strategy : Linear probing • Look "next door" until find match or empty
Collision Resolution -- Linear Probing • Adding new elements to a structure • Hash to the record, check if occupied • if occupied go “next door” • Keep going next door until find empty slot • If find a element with same value, then this is an illegal add
Linear Probing -- Fetching Elements • Going back to fetch an element • Seek to slot specified by hash function • If values do not match, this must be a collision • Keep looking “next door” until keys match • Alternatively, if you find a blank slot, then you are searching for an element that does not exist
Linear Probing -- Deleting an Element • What happens when one of a sequence of “next door” elements is deleted? • If we blank the element (set fields to zeros and null strings) • we try to fetch an element, linear probing and hit this element • a blank slot implies the element for which we search does not exist • it might be later on in a sequence of linear probes
Linear Probing -- Deleting an Element • SolutionDo not blank the slot • Set one of the fields to a sentinel valuestrcpy (name, “DELETED”); • Elements so marked could be used again (next time we are adding a record)
Improvements to Linear Probing Three things to do to improve performance • Increase the table capacity • End up eventually with same problem • Use a different collision resolution strategy • Ultimately end up with linear probing • Use a different hash function
Improvements to Linear Probing Use chaining • Hash table is an array (vector) of linked lists • Each list is a list of synonyms
wow pdq xyz Synonym Chaining • Array of linked lists • When an item hashes to a location it gets added to the list • When a collision occurs, the item gets added to the list • R(abc) = 3 • R(xyz) = 4 • R(pdq) = 1 • R(wow) = 3 abc table
Ideal Hash Function • Simple to evaluate • Scatters items throughout the hash table • uniform distribution • For non-numeric items • encode the item as an integer • for a string, use sum of ASCII values of two of the characters • feed resulting integer to the "randomizing" function
prev L last 9 17 22 26 34 first mySize next 5 Doubly-Linked Lists • In many applications, bidirectional movement is necessary • each node has two pointers • one link to a successor (or NULL) • one link to a predecessor (or NULL)
17 22 26 Doubly-Linked Lists • Consider what steps are required to insert a new link into a doubly linked list? • What steps are required to delete a link from a doubly linked list?