100 likes | 308 Views
Data Organization. Example 1: Heap storage management Keep track of free chunks of memory Example 2: A simple text editor Maintain a sequence of lines Example 3: Stable matching problem Maintain the set of currently free men Keep track of each person's preferences
E N D
Data Organization • Example 1: Heap storage management • Keep track of free chunks of memory • Example 2: A simple text editor • Maintain a sequence of lines • 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
Data Organization • All these examples have a common organizational model: • A sequence of similar items • (memory blocks, lines, men/women) • Certain desired operations • find, insert, delete
The list ADT • Data: • A collection of homogeneous elements arranged in a sequence • Operations • Insert • Delete • Find • Update • Retrieve • Length
The list ADT • 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?
The list data structure • 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.
The list data structure • 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?
Other list flavors • Doubly-linked list • Each node has a pointer to its successor and its predecessor. • Faster insert/delete, but more space. • Circular list • The last node points back to the head. • Sorted list • Items stored in sorted order. • Which implementation provides faster operations? Array or linked memory?
Other list flavors • 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...
Other list flavors • 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.
Other list flavors • 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.