30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

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.

Chapter 23 Data Structures and Collections 1151<br />

3. If the list contains more than one node prior <strong>to</strong> removal, create the CListNode<br />

reference current and assign it firstNode (line 101).<br />

4. Use current <strong>to</strong> traverse the list until current references the node directly preceding<br />

the last node. The While loop (lines 104–106) assigns current.NextNode<br />

<strong>to</strong> reference current as long as current.NextNode is<br />

not equal <strong>to</strong> lastNode.<br />

5. After locating the second-<strong>to</strong>-last node, assign current <strong>to</strong> lastNode (line 109)<br />

<strong>to</strong> dethread the last node from the list.<br />

6. Set current.NextNode <strong>to</strong> Nothing (line 110) in the new last node of the list<br />

<strong>to</strong> ensure proper list termination.<br />

7. Return the removeItem reference (line 113).<br />

Figure 23.11 illustrates method RemoveFromBack. Part a) illustrates the list before<br />

the removal operation. Part b) portrays the actual reference manipulations.<br />

Method Print (Fig. 23.5, lines 135–159) first determines whether the list is empty (line<br />

139). If so, Print displays a String consisting of "Empty " and the list’s name and then<br />

returns control <strong>to</strong> the calling method. Otherwise, Print outputs the data in the list. The<br />

method prints a String consisting of the string "The ", the name of the list and the string<br />

" is: ". Then, line 147 creates CListNode reference current and initializes it with<br />

firstNode. While current is not Nothing, there are more items in the list. Therefore,<br />

the method prints current.Data (line 151) then assigns current.NextNode <strong>to</strong> current<br />

(line 152) thus moving <strong>to</strong> the next node in the list. Note that, if the link in the last node<br />

of the list is not Nothing, the printing algorithm will erroneously attempt <strong>to</strong> print past the<br />

end of the list. The printing algorithm is identical for linked lists, stacks and queues.<br />

(a)<br />

(b)<br />

firstNode<br />

12<br />

firstNode<br />

7 11<br />

current<br />

Fig. 23.11 RemoveFromBack graphical representation.<br />

lastNode<br />

5<br />

lastNode<br />

12 7 11<br />

5<br />

removeItem

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

Saved successfully!

Ooh no, something went wrong!