MOVE TO FRONT METHOD

ALGORITHM 2011. 4. 18. 14:51


package algorithm.search;

public class MoveToFrontMethod extends SearchMethod {

 public void move(Node<?> node) throws Exception {

  if (isEmpty()) {
   throw new Exception("해당 노드를 링크드리스트에 먼저 추가하세요");
  } else {

   if (tail == node) {
    tail = node.prev;
    tail.next = null;
   } else {
    node.prev.next = node.next;
   }
  }

  node.prev = null;
  node.next = head;
  head.prev = node;

  head = 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 void add(Node<?> node) {
  if (head == null) {
   head = node;
   tail = node;
  } else {
   node.prev = tail;
   tail.next = node;
  }
  tail = node;
 }
 
 public Node<?> getHead(){
  return head;
 }

}

'ALGORITHM' 카테고리의 다른 글

TRANSPOSE METHOD  (0) 2011.04.18
VERSION UP! 0.1  (0) 2011.04.18
SEARCH ABSTRACT CLASS & NODE CLASS  (0) 2011.04.18
QUICK SORT  (0) 2011.04.16
BUBBLE SORT, INSERTION SORT TEST  (0) 2011.04.14
Posted by sangmooni
,