어차피 add, getHead, search method는 모든 searchMethod에 필요할듯...
수정본
--------------------------
package algorithm.search;
public abstract class SearchMethod {
protected Node<?> head = null;
protected Node<?> tail = null;
public abstract void move(Node<?> data) throws Exception;
public void add(Node<?> node) {
if (head == null) {
head = node;
tail = node;
} else {
node.prev = tail;
tail.next = node;
}
tail = node;
}
public Node<?> search(Node<?> data) throws Exception {
if (isEmpty()) {
throw new Exception("링크드리스트가 비어있습니다.");
} else {
Node<?> temp = head;
while (true) {
if (temp.data.equals(data.data)) {
return temp;
} else {
temp = temp.next;
if (temp == null){
return null;
}
}
}
}
}
public Node<?> getHead() throws Exception{
if (isEmpty()){
throw new Exception("링크드리스트가 비어있습니다.");
}else{
return head;
}
}
public boolean isEmpty(){
if (head == null){
return true;
}else{
return false;
}
}
}
'ALGORITHM' 카테고리의 다른 글
BINARY SEARCH (1) | 2011.04.18 |
---|---|
TRANSPOSE METHOD (0) | 2011.04.18 |
MOVE TO FRONT METHOD (0) | 2011.04.18 |
SEARCH ABSTRACT CLASS & NODE CLASS (0) | 2011.04.18 |
QUICK SORT (0) | 2011.04.16 |