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

In this document, students are provided with instructions and files for creating two iterators (forward iterator and find iterator) for a singly linked list in java. The objectives of the lab include gaining experience using the interface feature, implementing a traversal iterator, and implementing a find iterator. Students are encouraged to write test cases before writing methods and are required to implement the constructor, get(), next(), isvalid(), and delete() methods for each iterator.

Typology: Lab Reports

Pre 2010

Uploaded on 08/18/2009

koofers-user-9xv
koofers-user-9xv 🇺🇸

5

(1)

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Iterators 1 GIVEN FILES
CS 331 Lab: Iterators
Spring 2009
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 iterator-lab directory. Do a git
pull in your repository 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 List files.
compile-fwd Compiles Forward Iterator tests.
compile-find Compiles Find Iterator tests.
tests Tests everything.
test-list Tests List files.
test-fwd Tests Forward Iterator.
test-find Tests Find Iterator.
TestListIterator.java a test file for Lists.
1
pf3

Partial preview of the text

Download CS 331 Lab: 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: Iterators

Spring 2009

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 iterator-lab directory. Do a git pull in your repository 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 List files. compile-fwd Compiles Forward Iterator tests. compile-find Compiles Find Iterator tests. tests Tests everything. test-list Tests List files. test-fwd Tests Forward Iterator. test-find Tests Find Iterator.

  • TestListIterator.java — a test file for Lists.

Iterators 2 YOUR WORK

  • 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, assuming it is a valid node. It does not advance the cursor after returning a node! If there is no current element, return null. It does advance the cursor, though, before returning any data, if the deleted flag is set.

void next() Advance the iterator’s cursor to the next node that does not have the deleted flag set. If the iterator is currently invalid, do nothing.

boolean isValid() Return true if the iterator still has data. If the deleted flag is set in the current cursor, the iterator should advance itself first.

void delete() We’ve added this one to the usual mix. It will delete the current element from the original list, set the cursor’s deleted flag, and advance the iterator’s cursor. Do nothing if the cursor is invalid. Other iterators that are pointing to a deleted element don’t need to do anything special, as long as they honor the deleted flag.

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.