Docsity

CS 331 Lab 5: Implementing Iterators for a Singly Linked List, Lab Reports of Data Structures and Algorithms

Toggle sidebar
bg1
Iterators 1 GIVEN FILES
CS 331 Lab 5: Iterators
Fall 2007
Rev : 470
0.1 Objectives
In this lab you will create two iterators for a singly linked list.
•Have experience using the Interface feature of Java.
•Know how to implement a traversal iterator.
•Know how to implement a find iterator.
1 Given Files
You are given 6 files for this lab. They are in the cs331/Iterators directory. Do
an svn update in the cs331 directory to download it.
Inside you will find the following files:
•List.java — A linked list implementation. You will add your iterators to this
class.
•Iterator.java — An interface file for the Iterator.
•Makefile — the scripts needed to compile your files and run the tests. It
supports the following targets:
compile Compiles everything.
compile-list Compiles everything.
compile-fwd Compiles Forward Iterator tests.
compile-find Compiles Find Iterator tests.
tests Tests everything.
test-list Tests everything.
test-fwd Tests Forward Iterator.
test-find Tests Find Iterator.
1
pf3

Partial preview of the text

Download CS 331 Lab 5: Implementing Iterators for a Singly Linked List and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Iterators 1 GIVEN FILES

CS 331 Lab 5: Iterators

Fall 2007

Rev : 470

0.1 Objectives

In this lab you will create two iterators for a singly linked list.

  • Have experience using the Interface feature of Java.
  • Know how to implement a traversal iterator.
  • Know how to implement a find iterator.

1 Given Files

You are given 6 files for this lab. They are in the cs331/Iterators directory. Do an svn update in the cs331 directory to download it. Inside you will find the following files:

  • List.java — A linked list implementation. You will add your iterators to this class.
  • Iterator.java — An interface file for the Iterator.
  • Makefile — the scripts needed to compile your files and run the tests. It supports the following targets:

compile Compiles everything. compile-list Compiles everything. compile-fwd Compiles Forward Iterator tests. compile-find Compiles Find Iterator tests. tests Tests everything. test-list Tests everything. test-fwd Tests Forward Iterator. test-find Tests Find Iterator.

Iterators 2 YOUR WORK

  • TestListIterator.java — a test file for Lists.
  • TestListFwdIterator.java — a skeleton test file.
  • TestListFindIterator.java — another skeleton test file.

2 Your Work

For both of these classes, you must implement the methods listed below, and you must use the List class we’ve provided. You are encouraged to write your test cases before you write your methods; at least start them. You may well realize you need to add more tests as you are coding. In this lab you will write two iterators. As discussed in lecture, iterators have four basic methods. We will implement five.

constructor we need to create them, after all!

E get() Return the current element. It does not advance the cursor! If there is no current element, return null.

void next() Advance the iterator’s cursor. If the iterator is currently invalid, do nothing.

boolean isValid() Return true if the iterator still has data.

void delete() We’ve added this one to the usual mix. It will delete the current element from the original list, and advance the iterator’s cursor. Do nothing if the cursor is invalid.

The behavior of other iterators after a delete has occurred is undefined. You can do anything you like other than throw an exception. The official tests will not test this condition.

You will need a local variable, at least, for the iterator’s cursor.

2.1 The Forward Iterator

Once created, it will allow us to iterate over the list. For testing, you will want to test several things. Remember that in CS there are three numbers: 0, 1, and n.