Last active
December 16, 2015 11:23
-
-
Save AnnaBoro/e2488f9800c2ad840f15 to your computer and use it in GitHub Desktop.
MyList5 + remove() in Iterator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson7.mylist; | |
import java.util.Iterator; | |
public class Launcher { | |
public static void main(String[] args) { | |
SimpleLinkedList sl = new SimpleLinkedList(); | |
sl.addFirst("object3"); | |
sl.addFirst("object2"); | |
sl.addFirst("object1"); | |
sl.addLast("object4"); | |
sl.addLast("object5"); | |
sl.addAfter("object4", "fff"); | |
sl.addAfter("object5", "aaa"); | |
sl.addLast("object4"); | |
for (Object o : sl) { | |
System.out.println(o); | |
} | |
System.out.println("---------------------"); | |
Iterator itr = sl.iterator(); | |
while(itr.hasNext()) { | |
Object obj = itr.next(); | |
if ( ((String)obj).equalsIgnoreCase("object1")) { | |
itr.remove(); | |
} | |
} | |
for (Object o : sl) { | |
System.out.println(o); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson7.mylist; | |
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
public class SimpleLinkedList implements Iterable<Object> { | |
private Node root; | |
private int size; | |
public SimpleLinkedList() { | |
size = 0; | |
} | |
public void addFirst(Object o) { | |
Node node = new Node(); | |
node.object = o; | |
if(root != null) { | |
node.node = root; | |
} | |
root = node; | |
size++; | |
} | |
public void addLast(Object o) { | |
Node nodeNew = new Node(); | |
nodeNew.object = o; | |
if (root != null) { | |
Node gapNode = root.node; | |
Node gap = root.node; | |
while (gapNode != null) { | |
gap = gapNode; | |
gapNode = gapNode.node; | |
} | |
gap.node = nodeNew; | |
size++; | |
} | |
else addFirst(o); | |
} | |
public void addAfter(Object after, Object newObj) throws IllegalStateException { | |
Node nodeNew = new Node(); | |
nodeNew.object = newObj; | |
if (root != null) { | |
Node gapNode = root.node; | |
Node gap = root.node; | |
boolean isTrueObject = false; | |
while (gapNode != null) { | |
if (gapNode.object.equals(after)) { | |
isTrueObject = true; | |
break; | |
} | |
gap = gapNode; | |
gapNode = gapNode.node; | |
} | |
if (isTrueObject == false) { | |
throw new IllegalStateException("There is not such element"); | |
} | |
else if (gap.node == null) { | |
addLast(newObj); | |
} | |
else { | |
Node afterNew = gapNode.node; | |
gapNode.node = nodeNew; | |
nodeNew.node = afterNew; | |
size++; | |
} | |
} | |
else addFirst(newObj); | |
} | |
public int getSize() { | |
return size; | |
} | |
public void printList(Iterator iterator) { | |
while(iterator.hasNext()) { | |
System.out.println(iterator.next()); | |
} | |
} | |
@Override | |
public Iterator<Object> iterator() { | |
return new SLLIterator(); | |
} | |
private class Node { | |
private Object object; | |
private Node node; | |
public Node() { | |
} | |
} | |
private class SLLIterator implements Iterator<Object> { | |
private Node current; | |
private Node previous; | |
public SLLIterator() { | |
} | |
@Override | |
public boolean hasNext() { | |
if ((current != null && current.node != null) || (current == null && root != null)) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
@Override | |
public Object next() { | |
if (current == null) { | |
current = root; | |
previous = null; | |
return current.object; | |
} | |
else if (hasNext()) { | |
previous = current; | |
current = current.node; | |
return current.object; | |
} | |
else { | |
throw new NoSuchElementException(); | |
} | |
} | |
@Override | |
public void remove() { | |
if (current.node != null) { | |
if (current != null) { | |
current.object = current.node.object; | |
current.node = current.node.node; | |
} | |
} | |
else if (!hasNext() && previous == null) { | |
current = null; | |
root = null; | |
} | |
else { | |
previous.node = null; | |
} | |
size--; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment