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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

102 Chap. 4 Lists, Stacks, <strong>and</strong> Queuesheadcurrtailhead20 23 12 15(a)currtail2023101215(b)Figure 4.5 Illustr<strong>at</strong>ion of a faulty linked-list implement<strong>at</strong>ion where curr pointsdirectly to the current node. (a) Linked list prior to inserting element withvalue 10. (b) Desired effect of inserting element with value 10.constructor the same for both variants. Because the linked list class does not needto declare a fixed-size array when the list is cre<strong>at</strong>ed, this parameter is unnecessaryfor linked lists. It is ignored by the implement<strong>at</strong>ion.A key design decision for the linked list implement<strong>at</strong>ion is how to representthe current position. The most reasonable choices appear to be a pointer to thecurrent element. But there is a big advantage to making curr point to the elementpreceding the current element.Figure 4.5(a) shows the list’s curr pointer pointing to the current element. Thevertical line between the nodes containing 23 <strong>and</strong> 12 indic<strong>at</strong>es the logical positionof the current element. Consider wh<strong>at</strong> happens if we wish to insert a new node withvalue 10 into the list. The result should be as shown in Figure 4.5(b). However,there is a problem. To “splice” the list node containing the new element into thelist, the list node storing 23 must have its next pointer changed to point to the newnode. Unfortun<strong>at</strong>ely, there is no convenient access to the node preceding the onepointed to by curr.There is an easy solution to this problem. If we set curr to point directly tothe preceding element, there is no difficulty in adding a new element after curr.Figure 4.6 shows how the list looks when pointer variable curr is set to point to thenode preceding the physical current node. See Exercise 4.5 for further discussionof why making curr point directly to the current element fails.We encounter a number of potential special cases when the list is empty, orwhen the current position is <strong>at</strong> an end of the list. In particular, when the list is emptywe have no element for head, tail, <strong>and</strong> curr to point to. Implementing specialcases for insert <strong>and</strong> remove increases code complexity, making it harder tounderst<strong>and</strong>, <strong>and</strong> thus increases the chance of introducing a programming bug.These special cases can be elimin<strong>at</strong>ed by implementing linked lists with anadditional header node as the first node of the list. This header node is a link

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

Saved successfully!

Ooh no, something went wrong!