The following code fragment is an implementation of the count-controlled traversal of a singly linked list from Section3.4.5. Does it work? If not, explain the problem and how to fix it. (Note: this.length is the number of data nodes in the linked list and assume the visit() method is implemented and works properly.)

```
SLNode cursor = head.getSuccessor();
for ( int count = 0; count <= this.length; count++ ) {
visit(cursor);
cursor = cursor.getSuccessor();
}


```

No, it does not work. The problem is with the condition of the for loop, count <= this.length. By doing this the loop will be performed 1 more than the end of the list because this.length gives the number of nodes starting from 1 but count starts from 0. Therefore there will be an error since cursor will be null. It can be corrected by making the condition count < this.length.

Computer Science & Information Technology

You might also like to view...

The protocol of the World Wide Web is ____.

A. SMTP B. FTP C. HTTP D. HTML

Computer Science & Information Technology

Identify the letter of the choice that best matches the phrase or definition.

A. The appearance of text.  B. Formatting feature you can apply to a font to change its appearance. C. Designs of type.  D. Helps you enhance or clarify your text by using effects such as shadows and superscripts.  E. The height of characters in units called points.

Computer Science & Information Technology