package ru.job4j.setNode;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class SetNode<E> implements Iterable<E> {

    private Node<E> head;
    private Node<E> tail;
    int result = 0;

    public void searchElement(E value) {
        Node<E> temp = head;
        while (temp != null) {
            if (value.equals(temp.value)) {
                result = 1;
                break;
            }
            temp = temp.next;
        }
    }

    public void add(E value) {
        if (head == null) {
            head = tail = new Node<>(value);
        } else {
            searchElement(value);

            if (result == 0) {
                tail.next = new Node<>(value);
                tail = tail.next;
            } else {
                throw new NoSuchElementException();
            }
        }
    }

    @Override
    public Iterator<E> iterator() {
        return new SetIterator();
    }

    private class SetIterator implements Iterator<E> {

        Node<E> node = head;

        @Override
        public boolean hasNext() {
            return node != null;
        }

        @Override
        public E next() {
            Node<E> temp = node;
            node = node.next;
            return temp.value;
        }
    }
}


@@ -193,28 +193,8 @@
 ail;
-%0A    int result = 0;
 %0A%0A  
@@ -202,20 +202,23 @@
  public 
-void
+boolean
  searchE
@@ -227,32 +227,64 @@
 ment(E value) %7B%0A
+        boolean result = false;%0A
         Node%3CE%3E 
@@ -400,9 +400,12 @@
 t = 
-1
+true
 ;%0A  
@@ -471,32 +471,55 @@
 next;%0A        %7D%0A
+        return result;%0A
     %7D%0A%0A    publi
@@ -642,16 +642,21 @@
         
+if (!
 searchEl
@@ -671,38 +671,8 @@
 lue)
-;%0A%0A            if (result == 0
 ) %7B%0A
@@ -756,81 +756,8 @@
 xt;%0A
-            %7D else %7B%0A                throw new NoSuchElementException();%0A
     
@@ -1150,25 +1150,146 @@
     
-node = node.next;
+if (temp!=null) %7B%0A                node = node.next;%0A            %7D else %7B%0A                throw new NoSuchElementException();%0A            %7D
 %0A   

task #997