Created
August 3, 2013 02:58
-
-
Save charsyam/6144964 to your computer and use it in GitHub Desktop.
Java Generic LIst
This file contains 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
public class MyList<Element> { | |
public class ElementNode<Element> { | |
Element node; | |
ElementNode<Element> prev,next; | |
public ElementNode() { | |
clear(); | |
} | |
public ElementNode(Element node) { | |
setNode(node); | |
setPrev(null); | |
setNext(null); | |
} | |
public void setNode(Element node) { | |
this.node = node; | |
} | |
public void setPrev(ElementNode<Element> prev) { | |
this.prev = prev; | |
} | |
public void setNext(ElementNode<Element> next) { | |
this.next = next; | |
} | |
public Element getNode() { | |
return this.node; | |
} | |
public boolean hasNext() { | |
boolean ret = true; | |
if (this.next == null) { | |
ret = false; | |
} | |
return ret; | |
} | |
public ElementNode<Element> getPrev() { | |
return this.prev; | |
} | |
public ElementNode<Element> getNext() { | |
return this.next; | |
} | |
public void clear() { | |
setNode(null); | |
setPrev(null); | |
setNext(null); | |
} | |
} | |
public MyList() { | |
head = tail = null; | |
length = 0; | |
} | |
public int getLength() { | |
return length; | |
} | |
public ElementNode<Element> getFront() { | |
return head; | |
} | |
public boolean contains(Element element) { | |
ElementNode<Element> node = head; | |
while(node != null) { | |
if (true == node.getNode().equals(element)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
public boolean remove(int indexOf) { | |
if (indexOf >= length || length == 0 || indexOf < 0) { | |
return false; | |
} | |
return _remove(indexOf); | |
} | |
private boolean _remove(int indexOf) { | |
int count = 0; | |
ElementNode<Element> node = head; | |
if (length == 1) { | |
head.clear(); | |
head = tail = null; | |
length = 0; | |
return true; | |
} | |
while(node != null) { | |
if (count == indexOf) { | |
if (node == head) { | |
head = node.getNext(); | |
head.setPrev(null); | |
} else if (node == tail) { | |
tail = node.getPrev(); | |
tail.setNext(null); | |
} else { | |
ElementNode<Element> tmp_prev = node.getPrev(); | |
ElementNode<Element> tmp_next = node.getNext(); | |
node.getPrev().setNext(tmp_next); | |
node.getNext().setPrev(tmp_prev); | |
} | |
node.clear(); | |
length--; | |
} | |
count++; | |
node = node.getNext(); | |
} | |
return false; | |
} | |
public void addFront(Element data) { | |
ElementNode<Element> node = new ElementNode<Element>(data); | |
if (head == null) { | |
head = tail = node; | |
} else { | |
head.setPrev(node); | |
node.setNext(head); | |
head = node; | |
} | |
length++; | |
} | |
public void addLast(Element data) { | |
ElementNode<Element> node = new ElementNode<Element>(data); | |
if (head == null) { | |
head = tail = node; | |
} else { | |
node.setPrev(tail); | |
tail.setNext(node); | |
tail = node; | |
} | |
length++; | |
} | |
ElementNode<Element> head, tail; | |
int length; | |
} |
This file contains 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
import static org.junit.Assert.*; | |
import org.junit.Test; | |
public class MyListTest { | |
@Test | |
public void MyListDeclareTest() { | |
MyList<String> list = new MyList<String>(); | |
} | |
@Test | |
public void MyListAddFrontTest() { | |
MyList<String> list = new MyList<String>(); | |
list.addFront("hell"); | |
} | |
@Test | |
public void MyListAddLastTest() { | |
MyList<String> list = new MyList<String>(); | |
list.addLast("hell"); | |
} | |
@Test | |
public void MyListTest() { | |
MyList<String> list = new MyList<String>(); | |
list.addLast("hell"); | |
list.addLast("test"); | |
list.addLast("tail"); | |
MyList<String>.ElementNode<String> node = list.getFront(); | |
assertTrue(node.getNode().equals("hell")); | |
node = node.getNext(); | |
assertTrue(node.getNode().equals("test")); | |
node = node.getNext(); | |
assertTrue(node.getNode().equals("tail")); | |
node = node.getNext(); | |
} | |
@Test | |
public void MyListRemoveTest1() { | |
MyList<String> list = new MyList<String>(); | |
list.addLast("hell"); | |
list.addLast("test"); | |
list.addLast("tail"); | |
list.remove(0); | |
MyList<String>.ElementNode<String> node = list.getFront(); | |
assertTrue(node.getNode().equals("test")); | |
node = node.getNext(); | |
assertTrue(node.getNode().equals("tail")); | |
node = node.getNext(); | |
} | |
@Test | |
public void MyListRemoveTest2() { | |
MyList<String> list = new MyList<String>(); | |
list.addLast("hell"); | |
list.addLast("test"); | |
list.addLast("tail"); | |
list.remove(1); | |
MyList<String>.ElementNode<String> node = list.getFront(); | |
assertTrue(node.getNode().equals("hell")); | |
node = node.getNext(); | |
assertTrue(node.getNode().equals("tail")); | |
node = node.getNext(); | |
} | |
@Test | |
public void MyListRemoveTest3() { | |
MyList<String> list = new MyList<String>(); | |
list.addLast("hell"); | |
list.addLast("test"); | |
list.addLast("tail"); | |
list.remove(2); | |
MyList<String>.ElementNode<String> node = list.getFront(); | |
assertTrue(node.getNode().equals("hell")); | |
node = node.getNext(); | |
assertTrue(node.getNode().equals("test")); | |
node = node.getNext(); | |
assertTrue(node == null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
public boolean hasNext() {
boolean ret = true;
if (this.next == null) {
ret = false;
}
return ret;
}
=> 이렇게 바꾸면 어떨가요 :)
public boolean hasNext() {
return this.next != null;
}