Binary Search Tree using the Iterator Interface - java

I am trying to create a class iNode that implements an Iterator to traverse a Binary Search Tree. The Iterator searches in a PreOrder traversal and can pick from any node in a tree, this node has the value of the int value.
I am familiar with using Stacks on BSTs, but am having a bit of trouble with the Iterator's hasNext() and next() methods.
I understand that logically, hasNext must check the current iNode and see if it has children. Then while that is true, the next() function will iterate through the tree starting with the "root" or value, then favoring the left children, and finally the right children. I believe that this is a simple matter of syntax and would greatly appreciate a few tips.
Expected behavior:
should return an Iterable (type Integer) of [8,3,5,6,4,2]
import java.util.Iterator;
import java.util.NoSuchElementException;
public class iNode implements Iterable<Integer> {
public final Integer value;
public final iNode left, right;
public iNode(Integer value) {
this.value = value;
this.right = null;
this.left = null;
// a node with no children / root
}
public iNode(Integer value, iNode left, iNode right) {
this.value = value;
this.left = left;
this.right = right;
// with children
}
#Override
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
private iNode next = new iNode(value, left, right);
#Override
public boolean hasNext() {
// should return false when the current node has no children
return next != null;
}
#Override
public Integer next() {
if (!hasNext()) {
throw new NoSuchElementException("limit reached");
}
return next.value;
}
};
}
}

private static void printPreOrder(Node root) {
if(root == null) {
return;
}
System.out.println(root.value + "->");
printPreOrder(root.left);
printPreOrder(root.right);
}
Let recursion handle this . Just write a base case where the node is null.
private static void printPreOrder(Node root, List<Integer> list) {
if(root == null) {
return;
}
list.add(root.value);
printPreOrder(root.left);
printPreOrder(root.right);
}
Iterator<Integer> itr = list.iterator();
while(itr.hasNext()){
int ele = itr.next();
// print element
}
if you want to store them and then use a iterator to iterate the list.

You can still use a Stack when using an iterator. Iterators are supposed to have state so that they know at which point they are at.
// to record how deep in the tree we are at
// this stores the next nodes to visit
// obviously, start with the current node, "this"
private final Deque<iNode> stack = new ArrayDeque<>(List.of(iNode.this));
#Override
public boolean hasNext() {
return !stack.isEmpty(); // has next, iff there are next nodes to visit
}
#Override
public Integer next() {
// pop will throw NSEE if the stack is empty anyway,
// so you don't need to explicitly throw one,
// unless you want a custom message of course
iNode node = stack.pop(); // we are going to return the value of this later
// the next node to visit is the left node, then the right node
// so we should push the right node first, then the left node,
// which causes the left node to be popped *first*
if (node.right != null) stack.push(node.right);
if (node.left != null) stack.push(node.left);
return node.value;
}
Example usage:
var tree = new iNode(
8, new iNode(
3, new iNode(5), new iNode(6)
), new iNode(
4, null, new iNode(2)
)
);
for (var i : tree) {
System.out.println(i);
}
/*
8
3
5
6
4
2
*/

Related

the BST search method ((iteratively)) returning the node containing the searched value and its predecessor

Okay, I've already written several methods related to my problem. Right now I have two methods that are written using recursion (addBSTRecursion (int element) and searchBSTRecursion (int element)). And now I need implement (iteratively) the BST search method (Pair<BinaryTreeNode, BinaryTreeNode> searchBST(int element)) returning the node containing the searched value and its predecessor.
if the searched element is not in the tree, the first element of the pair is null.
if the first element of the pair is a root (it has no predecessor), then the second element of the pair is null.
Here is my class Pair:
public class Pair<T1, T2> {
public T1 first;
public T2 second;
public Pair(T1 a, T2 b) {
first = a;
second = b;
}
}
This is how I managed to implement method (Pair<BinaryTreeNode, BinaryTreeNode> searchBST(int element)), but unfortunately it does not work as expected.
#Override
public Pair<BinaryTreeNode, BinaryTreeNode> searchBST(int element) {
int current = 0;
while (element != data) {
if (element < data) {
current = left.data;
} else {
current = right.data;
}
}
Pair pair = new Pair(element, current);
return pair;
}
To be honest, I don’t know yet how to correctly implement this method, I will be glad to any hints. And also at the bottom I add all my previous classes.
public abstract class BinaryTreeNode {
protected int data; // value stored in the node
protected BinaryTreeNode left, right; // left and right sub-trees
// constructor
public BinaryTreeNode(int data) {
this.data = data;
}
// recursively adds an item to the BST
// #param new data to be stored in the new node
public abstract void addBSTRecursion(int element);
// prints the tree
// for example, for a tree:
// 7
// 6 8
// 2 7 4 9
//
// write:
//
// 2
// 6
// 7
// 7
// 4
// 8
// 9
// method pseudocode
// if there is a left subtree then print the left one (recursive call)
// write out gaps (depending on level), write out data, go to new line
// if it is right, print the right one (recursive call)
// #param level the distance of the node from the root. We start from 0.
public abstract void print(int level);
// recursive searches the BST.
// returns true if it finds an element with the given value
// #param searched value searched
// #return true if the given value is in the tree, false otherwise
public abstract boolean searchBSTRecursion(int element);
// iterative lookup of values in the BST
// returns a pair: node containing the searched element and its predecessor.
// if the searched element is not in the tree, the first element of the pair is null.
// if the first element of the pair is a root (it has no predecessor), then the second element of the pair is null.
// #param searched value searched
// #return pair of nodes: the first is the node containing the value searched for, or null if not found;
// the second element of the pair is the parent of the node (or null if the value is at the root of the tree)
public abstract Pair < BinaryTreeNode, BinaryTreeNode > searchBST(int element);
}
public class Node extends BinaryTreeNode {
public Node(int data) {
super(data);
}
#Override
public void addBSTRecursion(int element) {
Node node = new Node(element);
if (element < data) {
if (left != null) {
left.addBSTRecursion(element);
} else {
left = node;
}
} else {
if (right != null) {
right.addBSTRecursion(element);
} else {
right = node;
}
}
}
#Override
public void print(int level) {
if (left != null) {
left.print(level + 4);
}
for (int i = 0; i < level; i++) {
System.out.print(" ");
}
System.out.print(data + "\n");
if (right != null) {
right.print(level + 4);
}
}
#Override
public boolean searchBSTRecursion(int element) {
if (element == data) {
return true;
} else if (element < data) {
if (left != null && left.searchBSTRecursion(element)) {
return true;
} else {
return false;
}
} else {
if (right != null && right.searchBSTRecursion(element)) {
return true;
} else {
return false;
}
}
}
#Override
public Pair<BinaryTreeNode, BinaryTreeNode> searchBST(int element) {
int current = 0;
while (element != data) {
if (element < data) {
current = left.data;
} else {
current = right.data;
}
}
Pair pair = new Pair(element, current);
return pair;
}
}
public class Main {
public static void main(String[] args) {
Node node = new Node(8);
node.addBSTRecursion(3);
node.addBSTRecursion(10);
node.addBSTRecursion(1);
node.addBSTRecursion(6);
node.addBSTRecursion(14);
node.addBSTRecursion(4);
node.addBSTRecursion(7);
node.addBSTRecursion(13);
node.print(0);
System.out.print(node.searchBSTRecursion(15));
}
}

Add method java

I want to add a method add(int index, E element) in Java, that inserts a specified element at a specified index in the list and shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). But I guess something is wrong with the indices in my code in the for-loop. Any ideas how to solve it?
public class SingleLinkedList<E> implements ISingleLinkedList<E> {
Node head;
int size = 0;
#Override
public void add(int index, E element) throws IndexOutOfBoundsException {
Node newNode = new Node(element);
if(head == null && index == 0) {
head = newNode;
}
else if (index == 0 && head != null) {
Node tempNode = new Node(element);
tempNode.setmNextNode(head);
head = tempNode;
}
else {
Node tempNode = head;
for(int i = 1; i<index; i++) {
tempNode = tempNode.getmNextNode();
}
/**Node newNode = new Node(element);**/
newNode.setmNextNode(tempNode);
tempNode.setmNextNode(newNode);
}
size++;
}
}
My code for the Node class is:
public class Node<E> {
private E mElement;
private Node<E> mNextNode;
Node(E data) {
this.setmElement(data);
}
public E getmElement() {
return this.mElement;
}
public void setmElement(E element) {
this.mElement = element;
}
public Node<E> getmNextNode()
{
return this.mNextNode;
}
public void setmNextNode(Node<E> node)
{
this.mNextNode = node;
}
The problem is that I have a JUnit test that fails when adding this method and I do not know what more I need to add in order to pass the test.
#Test
public void testAddWithIndexesToListWith5Elements() {
int listSize = 5;
// First create an ArrayList with string elements that constitutes the test data
ArrayList<Object> arrayOfTestData = generateArrayOfTestData(listSize);
// Then create a single linked list consisting of the elements of the ArrayList
ISingleLinkedList<Object> sll = createSingleLinkedListOfTestData(arrayOfTestData);
// Add new elements first, in the middle and last to the ArrayList of test data
// and the single linked list
try {
arrayOfTestData.add(0, 42);
arrayOfTestData.add(3, "addedElement1");
arrayOfTestData.add(7, "addedElement2");
sll.add(0, 42);
sll.add(3, "addedElement1");
sll.add(7, "addedElement2");
}
catch (Exception e) {
fail("testAddWithIndexesToListWith5Elements - add() method failed");
}
// Check that the contents are equal
for (int i = 0; i < sll.size(); i++) {
assertEquals(arrayOfTestData.get(i), sll.get(i));
}
}
newNode.setmNextNode(tempNode);
tempNode.setmNextNode(newNode);
This is just going to create a cycle. It looks like your newNode should point to tempNode.getmNextNode() or something along those lines.
Your question is pretty unclear but I think I can see a problem.
If index is not 0, the you will iterate through the nodes until the index is reached.
If there are not enough elements in the list, you will reach the end of the list before the index where you want to insert the element.
In this case,
tempNode = tempNode.getmNextNode();
will set tempNode to null.
In the next iteration, this line will throw a NullPointerException.
You can bypass this issue by testing if tempNode.getmNextNode(); is null.
If that is the case, the element will just be inserted at the end/that point or will not be inserted.

How to push nodes from a Binary Tree into an Array?

I am struggling with pushing values from a Binary Search Tree into an array, but I also need them to be sorted. Here are the instructions of what is needed.
The toArray method should create and return an array containing every element in the tree in sorted order ("in order"). The capacity of this array should equal the number of elements it contains. This method should make use of the recursive private helper method toArray(BSTNode, List) to generate the array. This array will need be created as an array of Comparable objects and cast to an array of E objects. You can use Collection's toArray(E[]) method to help with the array generation.
Therefore here is my code I have so far:
public E[] toArray()
{
List<E> lista = new ArrayList<E>();
toArray(root, lista);
E[] good = (E[]) lista.toArray();
return good;
}
private void toArray(BSTNode<E> node, List<E> aList)
{
if(node.left != null)
{
aList.add(node.left.data);
}
}
Here is the rest of the code for references, but I am more focused on the toArray methods more than anything. I can't figure out how to sort them into an array. Please help.
public class BinarySearchTree<E extends Comparable<E>>
{
private BSTNode<E> root; // root of overall tree
private int numElements;
// post: constructs an empty search tree
public BinarySearchTree()
{
root = null;
}
// post: value added to tree so as to preserve binary search tree
public void add(E value)
{
root = add(root, value);
}
// post: value added to tree so as to preserve binary search tree
private BSTNode<E> add(BSTNode<E> node, E value)
{
if (node == null)
{
node = new BSTNode<E>(value);
numElements++;
}
else if (node.data.compareTo(value) > 0)
{
node.left = add(node.left, value);
}
else if (node.data.compareTo(value) < 0)
{
node.right = add(node.right, value);
}
return node;
}
// post: returns true if tree contains value, returns false otherwise
public boolean contains(E value)
{
return contains(root, value);
}
// post: returns true if given tree contains value, returns false otherwise
private boolean contains(BSTNode<E> node, E value)
{
if (node == null)
{
return false;
}
else
{
int compare = value.compareTo(node.data);
if (compare == 0)
{
return true;
}
else if (compare < 0)
{
return contains(node.left, value);
}
else
{ // compare > 0
return contains(node.right, value);
}
}
}
public void remove(E value)
{
root = remove(root, value);
}
private BSTNode<E> remove(BSTNode<E> node, E value)
{
if(node == null)
{
return null;
}
else if(node.data.compareTo(value) < 0)
{
node.right = remove(node.right, value);
}
else if(node.data.compareTo(value) > 0)
{
node.left = remove(node.left, value);
}
else
{
if(node.right == null)
{
numElements--;
return node.left;// no R child; replace w/ L
}
else if(node.left == null)
{
numElements--;
return node.right; // no L child; replace w/ R
}
else
{
// both children; replace w/ max from L
node.data = getMax(node.left);
node.left = remove(node.left, node.data);
}
}
return node;
}
private E getMax(BSTNode<E> node)
{
if(node.right == null)
{
return node.data;
}
else
{
return getMax(node.right);
}
}
public void clear()
{
root = null;
numElements--;
}
public boolean isEmpty()
{
if(numElements == 0)
{
return true;
}
else
{
return false;
}
}
public int size()
{
return numElements;
}
//My toArray Methods will go here.
public Iterator<E> iterator()
{
return new Iterator<>(root);
}
public static class Iterator<E>
{
private Stack<BSTNode<E>> stack;
public Iterator(BSTNode<E> node)
{
this.stack = new Stack<>();
while (node != null)
{
stack.push(node);
node = node.left;
}
}
public boolean hasNext()
{
return !stack.isEmpty();
}
public E next()
{
BSTNode<E> goodDays = stack.pop();
E result = goodDays.data;
if (goodDays.right != null)
{
goodDays = goodDays.right;
while (goodDays != null)
{
stack.push(goodDays);
goodDays = goodDays.left;
}
}
return result;
}
}
private static class BSTNode<E>
{
public E data;
public BSTNode<E> left;
public BSTNode<E> right;
public BSTNode(E data)
{
this(data, null, null);
}
public BSTNode(E data, BSTNode<E> left, BSTNode<E> right)
{
this.data = data;
this.left = left;
this.right = right;
}
}
}
Wait, this is a Binary Search Tree so it's already sorted.
Then you need to walk the tree.
Given you have something like:
4
/ \
2 6
\ / \
3 5 9
To insert it you have to:
Given a tree root
A. If the tree is null, there's nothing to insert.
B. If is not null:
B.1 Insert everything on the left
B.2 Insert the tree root
B.3 Insert everything on the right
Which would look like this:
void walkAndInsert(tree, array) {
if (tree == null) {//A
return
} else { //B
walkAndInsert(tree.left) //B.1
array.add(tree.root) //B.2
walkAndInsert(tree.right) //B.3
}
}
So applying these steps on the array:
Is tree null? No, then execute step #B (insert all left, root and all right)
//B
tree =
4
/ \
2 6
\ / \
3 5 9
array =[]
We take the left branch and repeat the process (step #B.1, insert all the left):
Is tree null? No, then execute #B
//B.1
tree =
2
\
3
array =[]
Since the left branch is null, the next execution would like like this:
Is tree null ? yes, then return
//A
tree =
array = []
This will conclude step B.1, we can go now to step B.2, insert root
//B.2
tree =
2
\
3
array =[2]
Followed by step B.3 insert all from right
Is tree null? No (there's a 3 there),
//B.3
tree =
3
array =[2]
Then execute #B.1 on this tree
Is the tree empty? Yes, this concludes this B.1
//A
tree =
array =[2]
Now in B.2 we insert this root
Is tree null? No (there's a 3 there),
//B.2
tree =
3
array =[2,3]
And finally we go to B.3 insert all from right
But there's nothing there, so we just return
//A
tree =
array =[2,3]
This finishes the left branch from our very initial tree.
So after B.1 is finished on our initial tree, we execute B.2 and our data looks like:
// B.2 on the initial tree
tree =
4
/ \
2 6
\ / \
3 5 9
array =[2,3,4]
And we repeat with the right side
Is null? no, then B on the branch with 5, insert 6, and step B on the branch with 9
//B.3
tree =
6
/ \
5 9
array =[2,3,4]
// B.1
tree =
5
array =[2,3,4]
// A
tree =
array =[2,3,4]
// B.2
tree =
5
array =[2,3,4,5]
// B.2
tree =
6
/ \
5 9
array =[2,3,4,5,6]
// B.3
tree =
9
array =[2,3,4,5,6]
// A
tree =
array =[2,3,4,5,6]
// B.2
tree =
9
array =[2,3,4,5,6,9]
Working example of the steps described here
import java.util.*;
import java.lang.reflect.Array;
import static java.lang.System.out;
class Tree<E extends Comparable<E>> {
E root;
Tree<E> left;
Tree<E> right;
void insert(E element) {
if (this.root == null) {
this.root = element;
this.left = new Tree<E>();
this.right = new Tree<E>();
} else if (element.compareTo(this.root) < 0 ) {
left.insert(element);
} else {
right.insert(element);
}
}
E[] toArray() {
List<E> a = new ArrayList<>();
toArray(this, a);
#SuppressWarnings("unchecked")
final E[] r = a.toArray((E[]) Array.newInstance(a.get(0).getClass(), a.size()));
return r;
}
// instance method just to retain the generic type E
private void toArray(Tree<E> t, List<E> list) {
if (t == null || t.root == null) {
return;
} else {
toArray(t.left, list);
list.add(t.root);
toArray(t.right, list);
}
}
public static void main(String ... args) {
Tree<String> t = new Tree<>();
t.insert("hola");
t.insert("adios");
t.insert("fuimonos");
System.out.println(Arrays.toString(t.toArray()));
}
}
I figured it out. I will disclose the code and explain what's going on.
In the public I make a List that will soon be an Array List.
Then I call the toArray helper method (private) to set the values. Root for the top one and lista for the list it will go in.
After make the Array and set the size with numElements. Comparable is in there since at the very top of my code, that's what it extends.
Then put the that array into the lista.
Finally return it.
public E[] toArray()
{
List<E> lista = new ArrayList<E>();
toArray(root, lista);
E[] arr = (E[]) new Comparable[numElements];
lista.toArray(arr);
return arr;
}
In the private I do some recursion.
As long as the node is not empty(null) then the array will search for left nodes continuously until it has no left (left) therefore add that into the array.
Then adds the right ones.
private void toArray(BSTNode<E> node, List<E> aList)
{
if(node != null)
{
toArray(node.left, aList);
aList.add(node.data);
toArray(node.right, aList);
}
}
Sorry if that was hard to understand, I'm not the best at explaining things, however this worked for me.

Similiar functions to traverse a binary tree

I have got a binary tree
public class Node
{
int value;
Node left;
Node right;
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public String getValue() {
return value;
}
}
And in main I have got a function to traverse it.
For tree
5
/ \
3 7
/ \
1 2
First one creates a queue of nodes with breadth first traversal(5,3,7,1,2).
Second one returns value of a node for eg. 7 for number 2 or 2 for number 4.
private void queueOfTreaversed() {
LinkedList<Node> queue = new LinkedList<Node>();
if (root != null)
queue.add(root);
while (!queue.isEmpty()) {
Node temp = queue.removeFirst();
if (temp.getLeft() != null && temp.getRight() != null) {
traversed.add(temp); //there is a difference
queue.add(temp.getLeft());
queue.add(temp.getRight());
}
}
}
public int getValue(int n) {
LinkedList<Node> queue = new LinkedList<Node>();
if (root != null)
queue.add(root);
while (!queue.isEmpty() && n>0) {
Node temp = queue.removeFirst();
if (temp.getLeft() != null && temp.getRight() != null) {
queue.add(temp.getLeft());
queue.add(temp.getRight());
}
}
return queue.peekFirst().getValue(); //there is a difference
}
And I have got duplication of code that I do not how to get rid off.
I use traversed in meantime and pop elements from this queue so elements will not be in this order and traversed cannot be used. Could anyone give any hint?
Once you have got the traversed nodes in traversed, your getValue(int n) function can actually index into traversed to get the value you want. In your getValue(int n) function, just use code like this:
if (n < traversed.size()) {
return traversed.get(n).getValue();
}
throw new Exception("Element not existed");
To be able to use traversed, just return it in your queueOfTreaversed function.

LinkedList - loop not working - Java

I am required to write a method that returns a number - the amount of times an element is found in a linked list. So far I have;
package Question4;
import net.datastructures.Node;
public class SLinkedListExtended<E> extends SLinkedList<E> {
// returns the number of occurrences of the given element in the list
public int count(E elem) {
Node<E> cursor = tail;
int counter = 0;
if ((cursor != null) && (!(cursor.getElement().equals(elem)))) { //tail isnt null and element is not equal to elem
cursor = cursor.getNext(); //go to next node
} else if ((cursor != null) && (cursor.getElement().equals(elem))){ //cursor isn't null and element equals elem
counter++; //increment counter
}
else {
return counter; //return counter
}
return counter;
}
public static void main(String[] args) {
SLinkedListExtended<String> x = new SLinkedListExtended<String>();
x.insertAtTail("abc");
x.insertAtTail("def");
x.insertAtTail("def");
x.insertAtTail("xyz");
System.out.println(x.count("def")); // should print "2"
x.insertAtTail(null);
x.insertAtTail("def");
x.insertAtTail(null);
System.out.println(x.count("def")); // should print "3"
System.out.println(x.count(null)); // should print "2"
}
}
I have extended to a class which compiles correctly, so I know the problem is in my method. I can't figure out what to do, my code returns 0, which is probably the counter integer remaining at 0 and not going through the loop statement. Any ideas are appreciated.
Edit. SLinkedList code:
import net.datastructures.Node;
public class SLinkedList<E> {
protected Node<E> head; // head node of the list
protected Node<E> tail; // tail node of the list (if needed)
protected long size; // number of nodes in the list (if needed)
// default constructor that creates an empty list
public SLinkedList() {
head = null;
tail = null;
size = 0;
}
// update and search methods
public void insertAtHead(E element) {
head = new Node<E>(element, head);
size++;
if (size == 1) {
tail = head;
}
}
public void insertAtTail(E element) {
Node<E> newNode = new Node<E>(element, null);
if (head != null) {
tail.setNext(newNode);
} else {
head = newNode;
}
tail = newNode;
size++;
}
public static void main(String[] args) { // test
SLinkedList<String> list = new SLinkedList<String>();
list.insertAtHead("lol");
}
}
Maybe you should use a while loop instead of an if clause
**while** ((cursor != null) && (!(cursor.getElement().equals(elem)))) {
The code in count is not in a loop, so it'll just return after the first element.
Try this:
public int count(E elem) {
Node<E> cursor = tail;
int counter = 0;
while (true)
{
if ((cursor != null) && (!(cursor.getElement().equals(elem)))) { //tail isnt null and element is not equal to elem
cursor = cursor.getNext(); //go to next node
} else if ((cursor != null) && (cursor.getElement().equals(elem))){ //cursor isn't null and element equals elem
counter++; //increment counter
}
else {
return counter; //return counter
}
}
}
Also, note that cursor.getElement().equals(elem) will return a NullPointerException when cursor.getElement() is null. The easiest way to deal with this is probably to write a separate equals method:
boolean equals(E e1, E e2)
{
if (e1 == null)
return e2 == null;
if (e2 == null)
return false;
return e1.equals(e2);
}
Also, presumably Node<E> cursor = tail; makes it point to the end of the list and presumably you want Node<E> cursor = head; instead.
One of the fundamental things that you were missing was a loop. Since you are essentially searching for something, you want to loop through the entire list. Once you run into an element that matches the one that you are searching for, you want to increment the count by 1. Once you have finished looping through the entire list, you want to return that count. So this is my solution. I keep it simple so you could understand:
import java.util.LinkedList;
public class Duplicates<E> extends LinkedList<E> {
public static void main(String[] args) {
Duplicates<String> duplicates = new Duplicates<String>();
duplicates.add("abc");
duplicates.add("def");
duplicates.add("def");
duplicates.add("xyz");
System.out.println(duplicates.duplicateCount("def"));
duplicates.add(null);
duplicates.add("def");
duplicates.add(null);
System.out.println(duplicates.duplicateCount("def"));
System.out.println(duplicates.duplicateCount(null));
}
public int duplicateCount(E element) {
int count = 0;
for (E e : this) {
if (e == element) {
count++;
}
}
return count;
}
}
Output:
2
3
2
I suggest you combine Martin's answer (which tells you how to count the elements) with this, which tell you how to be able to use foreach - you just have to make your SLinkedListExtended implement Iterable, whioch should be something liek the follwoing (you could do this on SLinkedList, but I'm assuming you were told not to alter the code for that one):
public class SLinkedListExtended<E> extends SLinkedList<E> implements Iterable<E> () {
public Iterator<E> iterator() {
final Node<E> itHead = head;
return new Iterator<E>() {
Node<E> current = itHead;
long position = 0;
public boolean hasNext() {
return current != null && position < size;
}
public E next() {
current = current.getNext();
++position;
return current.getElement();
}
public void remove() {
throw new UnsupportedOperationException("Not supported yet.");
}
};
}
};
I can't vouch for all the details, but this should cover most of it. You may also consider using equals instead of ==, but don't forget to check the elements for nullity.
next should only be called if hasNext is true, so it's not a problem if it throws an exception (but it should be a NoSuchElementException to keep in line with the contract).
Implementing Iterable makes your class compatible with the Collections library, hence the support for foreach, but you can use it to do raw iteration by calling iterator, hasNext and next yourself.

Categories