Data Organization n Example 1 Heap storage management

  • Slides: 10
Download presentation
Data Organization n Example 1: Heap storage management – Keep track of free chunks

Data Organization n Example 1: Heap storage management – Keep track of free chunks of memory n Example 2: A simple text editor – Maintain a sequence of lines n Example 3: Stable matching problem – Maintain the set of currently free men – Keep track of each person's preferences • Quickly find the highest-ranked woman to whom a man has not proposed yet – Quickly find whether a woman is engaged and to whom 1

Data Organization n All these examples have a common organizational model: – A sequence

Data Organization n All these examples have a common organizational model: – A sequence of similar items • (memory blocks, lines, men/women) – Certain desired operations • find, insert, delete 2

The list ADT n Data: – A collection of homogeneous elements arranged in a

The list ADT n Data: – A collection of homogeneous elements arranged in a sequence n Operations – Insert – Delete – Find – Update – Retrieve – Length 3

The list ADT n Most operations refer to a certain position in the list.

The list ADT n Most operations refer to a certain position in the list. How will this be designed? – Maintain a "current" position? – Specify an index? – Specify a pointer to an element? – Specify a generalized pointer to an element? 4

The list data structure n Implementation 1: Contiguous memory – Use a dynamic array

The list data structure n Implementation 1: Contiguous memory – Use a dynamic array – How is each operation implemented? – How efficient is each operation? • Random access capability is good for retrieval if we use an index for element access – Important: The list ADT does not provide random access. • We need to shift elements every time we insert or delete • We need to reallocate whenever the array fills up. 5

The list data structure n Implementation 2: Linked memory – Use a node structure

The list data structure n Implementation 2: Linked memory – Use a node structure to store the data and a pointer to the next node, to create a chain of nodes. – Uses more space than the array (due to the pointers) but insert/delete do not require shifting. – However, deleting requires us to traverse the whole list in order to access the predecessor of the node to be deleted. • Easy solution: keep in mind the abstract image of a linked list! – Move the next node's contents into the one to be deleted, and then physically remove the next node. – We can use a similar trick for the insert operation – Does this work in all cases? 6

Other list flavors n Doubly-linked list – Each node has a pointer to its

Other list flavors n Doubly-linked list – Each node has a pointer to its successor and its predecessor. • Faster insert/delete, but more space. n Circular list – The last node points back to the head. n Sorted list – Items stored in sorted order. – Which implementation provides faster operations? Array or linked memory? 7

Other list flavors n XOR list – A space saving list – Instead of

Other list flavors n XOR list – A space saving list – Instead of both a previous and next pointer, store the XOR of the predecessor and successor. • Node B stores &A XOR &C • If you are at B and know the address of A, you can compute the address of C. • The list can be traversed in any direction, provided you know where you came from. – Interesting, but not very useful. . . 8

Other list flavors n Unrolled linked list – A space saving list – Store

Other list flavors n Unrolled linked list – A space saving list – Store k data items in each node • • It's a list of arrays Reduces cache misses Each node should be at least half-full If a node is too empty after a delete, merge it with a neighbor. • If a node overflows after an insert, split it. 9

Other list flavors n Satellite list – An easily reversible list – Developed for

Other list flavors n Satellite list – An easily reversible list – Developed for use in TSP algorithms – Imagine each node as having two "satellites", north and south. A chain of pointers links all the northern satellites and another chain links all the southern ones. – Reversing part of the list requires changing only 4 pointers. • Compare this to reversing a doubly-linked list. 10