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 1141<br />

RemoveFromFront (lines 57–81) and RemoveFromBack (lines 84–117) are the primary<br />

methods of class CList. Each method uses a SyncLock block <strong>to</strong> ensure that<br />

CList objects are multithread safe when used in a multithreaded program. This means<br />

that, if one thread is modifying the contents of a CList object, no other thread can modify<br />

the same CList object at the same time. Method IsEmpty (lines 120–132) is a predicate<br />

method that determines whether the list is empty (i.e., whether the reference <strong>to</strong> the first node<br />

of the list is Nothing). Predicate methods typically test a condition and do not modify the<br />

object on which they are called. If the list is empty, method IsEmpty returns True; otherwise,<br />

it returns False. Method Print (lines 135–159) displays the list’s contents. Both<br />

method IsEmpty and method Print use SyncLock blocks, ensuring that the state of<br />

the list does not change while the methods are performing their tasks.<br />

Class EmptyListException (Fig. 23.6) defines an exception class <strong>to</strong> handle<br />

illegal operations on an empty CList. For example, an EmptyListException occurs<br />

if the program attempts <strong>to</strong> remove a node from an empty CList.<br />

Module modListTest (Fig. 23.7) uses the linked-list library <strong>to</strong> create and manipulate<br />

a linked list. Line 10 creates an instance of type CList named list. Then, lines 13–<br />

16 create data <strong>to</strong> add <strong>to</strong> the list. Lines 19–29 use CList insertion methods <strong>to</strong> insert these<br />

objects and use CList method Print <strong>to</strong> output the contents of list after each insertion.<br />

The code inside the Try block (lines 35–70) removes objects (using CList deletion<br />

methods), outputs the removed object and outputs list after every deletion. If there is an<br />

attempt <strong>to</strong> remove an object from an empty list, the Catch block (lines 66–68) catches the<br />

EmptyListException. Note that module modListTest uses namespace<br />

LinkedListLibrary (Fig. 23.4); thus, the project containing module modListTest<br />

must contain a reference <strong>to</strong> the LinkedListLibrary class library.<br />

firstNode<br />

H D ... Q<br />

Fig. 23.3 Linked-list graphical representation.<br />

1 ' Fig. 23.04: ListNodes.vb<br />

2 ' Class <strong>to</strong> represent one node in a CList.<br />

3<br />

4 Public Class CListNode<br />

5 Private mData As Object<br />

6 Private mNextNode As CListNode<br />

7<br />

Fig. 23.4 Self-referential class CListNode (part 1 of 2).<br />

lastNode

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

Saved successfully!

Ooh no, something went wrong!