9. Iterators

  • Feel free to use your laptop

  • You are strongly encourage to work with others

    • When you get stuck, ask those sitting around you for help

    • Get used to working together in the labs

    • Peer teaching and peer learning has been empirically shown to be very effective

9.1. Playing with Iterators

The goal is to redefine stacks and queues such that the collections are iterable based on the details discussed in the Iterators Topic.

9.1.1. Create an Array Iterator

  1. Create an ArrayIterator class that implements the Iterator interface

Note

Obviously one can simply download the ArrayIterator code, but this defeats the purpose of the lab. Instead, slowly and deliberately implement each method and take the time to understand the details.

9.1.2. Create an Iterable Stack

  1. Download the Stack interface and add it to the project

  2. Modify the interface such that Stack extends Iterable

  3. Add the required iterator method to the interface

  4. Download the ArrayStack class and add it to the project

  5. Modify the ArrayStack such that it implements the iterator method

    • Simply have it return the ArrayIterator

  6. Modify the existing toString method such that it uses an iterator

Note

What order are the elements returned by the iterator for the ArrayStack? Should it start at index 0 or at the top?

9.1.3. Create an Iterable Queue

  1. Download the Queue interface and add it to the project

  2. Modify the interface such that Queue extends Iterable

  3. Add the required iterator method to the interface

  4. Download the ArrayQueue class and add it to the project

  5. Modify the ArrayQueue such that it implements the iterator method

  6. Modify the existing toString method such that it uses an iterator

9.1.4. Make the Linked Stack and Queue Iterable

Repeat the same ideas for the LinkedStack and LinkedQueue.

9.1.5. Improve equals

The current implementations of the Stack and Queue objects are only able to be equal to instances of the exact same type. For example, it is currently only possible to have an ArrayStack be equal to another ArrayStack. However, this is less than ideal as it should be possible to check equality between Stack objects, regardless of their specific implementation. In other words, it should be possible for an ArrayStack to be equal to a LinkedStack.

Modify the existing equals methods within the various stack and queue implementations such that it is possible for Stack and Queue instances to be equal, regardless of their specific implementation.

9.2. Kattis Problems

  1. https://open.kattis.com/problems/quadrant

  2. https://open.kattis.com/problems/judgingmoose

  3. https://open.kattis.com/problems/timeloop

  4. https://open.kattis.com/problems/oddities

  5. https://open.kattis.com/problems/fizzbuzz

  6. https://open.kattis.com/problems/twostones

  7. https://open.kattis.com/problems/spavanac

  8. https://open.kattis.com/problems/cetvrta

  9. https://open.kattis.com/problems/bus

  10. https://open.kattis.com/problems/timeloop

  11. https://open.kattis.com/problems/oddities

  12. https://open.kattis.com/problems/fizzbuzz

  13. https://open.kattis.com/problems/sibice

  14. https://open.kattis.com/problems/datum

  15. https://open.kattis.com/problems/dicecup

  16. https://open.kattis.com/problems/autori

  17. https://open.kattis.com/problems/apaxiaaans

  18. https://open.kattis.com/problems/hissingmicrophone

  19. https://open.kattis.com/problems/trik

  20. https://open.kattis.com/problems/pot

  21. https://open.kattis.com/problems/filip

  22. https://open.kattis.com/problems/reversebinary

  23. https://open.kattis.com/problems/sevenwonders

  24. https://open.kattis.com/problems/zamka

  25. https://open.kattis.com/problems/bijele

  26. https://open.kattis.com/problems/cold

  27. https://open.kattis.com/problems/nastyhacks

  28. https://open.kattis.com/problems/grassseed

  29. https://open.kattis.com/problems/pet

  30. https://open.kattis.com/problems/batterup

  31. https://open.kattis.com/problems/aboveaverage

  32. https://open.kattis.com/problems/icpcawards

  33. https://open.kattis.com/problems/quickbrownfox

  34. https://open.kattis.com/problems/nodup

  35. https://open.kattis.com/problems/conundrum

  36. https://open.kattis.com/problems/bela

  37. https://open.kattis.com/problems/kornislav