11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 4.1 Lists 107curr...23 10 12(a)...curr2... 23 10 12 ...it1(b)Figure 4.10 The linked list removal process. (a) The linked list before removingthe node with value 10. (b) The linked list after removal. 1 marks the list nodebeing removed. it is set to point to the element. 2 marks the next field ofthe preceding list node, which is set to point to the node following the one beingdeleted.the node before it, because th<strong>at</strong> is wh<strong>at</strong> we really want). This takes Θ(n) time inthe average <strong>and</strong> worst cases. Implement<strong>at</strong>ion of method moveToPos is similar inth<strong>at</strong> finding the ith position requires marching down i positions from the head ofthe list, taking Θ(i) time.Implement<strong>at</strong>ions for the remaining oper<strong>at</strong>ions each require Θ(1) time.FreelistsThe new oper<strong>at</strong>or is rel<strong>at</strong>ively expensive to use. Garbage collection is also expensive.Section 12.3 discusses how general-purpose memory managers are implemented.The expense comes from the fact th<strong>at</strong> free-store routines must be capableof h<strong>and</strong>ling requests to <strong>and</strong> from free store with no particular p<strong>at</strong>tern, as well asmemory requests of vastly different sizes. This, combined with unpredictable freeingof space by the garbage collector, makes them inefficient compared to wh<strong>at</strong>might be implemented for more controlled p<strong>at</strong>terns of memory access.List nodes are cre<strong>at</strong>ed <strong>and</strong> deleted in a linked list implement<strong>at</strong>ion in a wayth<strong>at</strong> allows the Link class programmer to provide simple but efficient memorymanagement routines. Instead of making repe<strong>at</strong>ed calls to new, the Link classcan h<strong>and</strong>le its own freelist. A freelist holds those list nodes th<strong>at</strong> are not currentlybeing used. When a node is deleted from a linked list, it is placed <strong>at</strong> the head of thefreelist. When a new element is to be added to a linked list, the freelist is checkedto see if a list node is available. If so, the node is taken from the freelist. If thefreelist is empty, the st<strong>and</strong>ard new oper<strong>at</strong>or must then be called.Freelists are particularly useful for linked lists th<strong>at</strong> periodically grow <strong>and</strong> thenshrink. The freelist will never grow larger than the largest size yet reached by the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!