1 / 16

Other Linked Structures

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.

kieve
Télécharger la présentation

Other Linked Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Other Linked Structures Chapter 9

  2. 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

  3. 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

  4. 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())

  5. 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

  6. Hash Tables • Collisions • When val1 != val2 buth(val1) == h(val2) • Strategy : Linear probing • Look "next door" until find match or empty

  7. 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

  8. 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

  9. 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

  10. 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)

  11. 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

  12. Improvements to Linear Probing Use chaining • Hash table is an array (vector) of linked lists • Each list is a list of synonyms

  13. 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

  14. 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

  15. 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)

  16. 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?

More Related