Presentation is loading. Please wait.

Presentation is loading. Please wait.

IndexedListWithIteratorsViaLinear1Ind

Similar presentations


Presentation on theme: "IndexedListWithIteratorsViaLinear1Ind"— Presentation transcript:

1 IndexedListWithIteratorsViaLinear1Ind
An extension of the IndexedListViaLinear1Ind class with two iterators, frontToRear() and rearToFront().

2 Writing the Code The code itself seems easy enough to write, it simply involves looking at each object in a list starting either from the front or the back and adding the option to remove the last object looked at. Two variables are used to accomplish this, previous and next, which hold the values of the last and current object respectively.

3 FrontToRear Iterator Constructor
The boolean “iterating” is set to true, as the process has now begun. “previous” is a Linear1Ind structure and is given a value of null as to ensure that the remove() method is not called before the next() method is called. “current” is a Linear1Ind structure and is assigned as the list that you wish to run the iteration process on.

4 FrontToRear Iterator Methods
hasNext() next() remove() Returns a value of true while “current” does not point to a null link. The value of “previous” is set to the value of “current.” “current” is set equal to the next link in the list you are iterating. The data of the last object looked at is returned. The current pointer in “previous” is set to the next one. “previous” is then set equal to null to ensure that the remove() method is not called again before the next() method.

5 RearToFront Iterator Constructor
The boolean “iterating” is set to true, as the process has now begun. “current” is a StackViaLinear1Ind structure. It is filled with a for-loop that starts at the beginning of the Indexed List you wish to iterate and proceeds to push() all the objects it contains. “previous” is assigned a null value to ensure that the remove() method is not called before the next() method.

6 RearToFront Methods hasNext() next() remove()
This returns a value of true while “current” is not empty. The item at the top of “current” is popped and assigned to “previous.” The data of the last object looked at is returned. The current pointer in “previous” is set to the next one. “previous” is then set equal to null to ensure that the remove() method is not called again before the next() method.

7 The Driver Two IndexedListWithIteratorsViaLinear1Ind’s are created, each holding 15 items. One list is iterated with the frontToRear() method and the other with the rearToFront() method. In both cases, any occurrences of the number zero are removed to ensure that the remove() method works. The lists are printed both before and after the iterations to make sure that the process has been completed successfully.

8 Conclusion Overall, the implementation of an iterator in the IndexedListViaLinear1Ind class does not seem to be too much of a problem as far as coding is concerned. The timing of the frontToRear() method seems to be fairly good as its linear. However, the timing in the rearToFront() method suffers somewhat as a stack had to be used to properly implement it.


Download ppt "IndexedListWithIteratorsViaLinear1Ind"

Similar presentations


Ads by Google