package ru.job4j.set.setonalinkedlist;
import java.util.Iterator;
import java.util.NoSuchElementException;


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

    private Node<E> last;
    private Node<E> first;
    private int size = 0;

    public void add(E el) {
        final Node<E> lt = last;
        boolean isAdd = true;
        Iterator it = this.iterator();
        while (it.hasNext()) {
            if (it.next().equals(el)) {
                isAdd = false;
                break;
            }
        }
        if (isAdd) {
            final Node<E> newNode = new Node<>(lt, el, null);
            this.last = newNode;
            if (lt == null)
                first = newNode;
            else
                lt.next = newNode;
            size++;
        }
    }

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

    private static class Node<E>{
        Node<E> next;
        Node<E> prev;
        E item;
        int count = 0;

        public Node(Node<E> prev, E item, Node<E> next) {
            this.prev = prev;
            this.item = item;
            this.next = next;
        }

    }

    public class Iter implements Iterator<E> {

        private SimpleSet.Node<E> next;
        private int nextIndex;

        @Override
        public boolean hasNext() {
            return nextIndex < size;
        }

        @Override
        public E next() {
            if (!hasNext())
                throw new NoSuchElementException();
           if (nextIndex == 0)
               next = first;
           else
               next = next.next;
           nextIndex++;
           return next.item;
        }
    }
}

@@ -306,139 +306,151 @@
     
-boolean isAdd = true;%0A        Iterator it = this.iterator();%0A        while (it.hasNext()) %7B%0A            if (it.next().equals(el)) %7B
+if (!contains(el)) %7B%0A            final Node%3CE%3E newNode = new Node%3C%3E(lt, el, null);%0A            this.last = newNode;%0A            if (lt == null)
 %0A   
@@ -466,20 +466,22 @@
     
-isAdd = fals
+first = newNod
 e;%0A 
@@ -495,19 +495,17 @@
     
-    break;%0A
+else%0A    
     
@@ -516,163 +516,209 @@
     
-%7D%0A  
+lt.next = newNode;%0A
       
-%7D%0A
       
-  if (isAdd) %7B%0A
+size++;%0A   
     
+ %7D%0A
     
+%7D%0A%0A
     
-final Node%3CE%3E newNode = new Node%3C%3E(lt, el, null);%0A            this.last = newNode;%0A            if (lt == null)
+public boolean contains(E el) %7B%0A        Iterator it = this.iterator();%0A        while (it.hasNext()) %7B%0A            if (it.next().equals(el)) %7B
 %0A   
@@ -726,38 +726,34 @@
             
-first = newNod
+return tru
 e;%0A         
@@ -747,36 +747,33 @@
 ue;%0A            
-else
+%7D
 %0A               
@@ -761,32 +761,34 @@
   %7D%0A        
+%7D%0A
         
 lt.next = ne
@@ -779,56 +779,21 @@
     
-lt.next = newNode;%0A            size++;%0A        %7D
+return false;
 %0A   

task 997