Last active
December 12, 2015 19:19
-
-
Save AnnaBoro/dee2e21306bf11cbc8df to your computer and use it in GitHub Desktop.
MyList5 + addAfter()
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; | |
public class Launcher { | |
public static void main(String[] args) { | |
SimpleLinkedList sl = new SimpleLinkedList(); | |
sl.addFirst("object1"); | |
sl.printList(); | |
sl.addFirst("object2"); | |
sl.printList(); | |
sl.addFirst("object3"); | |
sl.printList(); | |
System.out.println(sl.getSize()); | |
sl.addLast("object4"); | |
sl.printList(); | |
sl.addLast("object5"); | |
sl.printList(); | |
System.out.println(sl.getSize()); | |
sl.addAfter("object4", "fff"); | |
sl.printList(); | |
System.out.println(sl.getSize()); | |
sl.addAfter("object5", "aaa"); | |
sl.printList(); | |
System.out.println(sl.getSize()); | |
} | |
} |
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; | |
public class SimpleLinkedList { | |
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() { | |
Node n = root; | |
if (n == null) { | |
System.out.println("List is empty"); | |
} | |
else { | |
while (n.node != null) { | |
System.out.print(n.object + ", "); | |
n = n.node; | |
} | |
System.out.println(n.object); | |
} | |
} | |
private class Node { | |
private Object object; | |
private Node node; | |
public Node() { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment