2017-02-11 99 views
1

问题在于我的DLinkedList类中的findA方法。 的方法显然改变了我head.next指向tmp.next 我创建了一个列表{0,1,2,3,4,5,6,7,8,9} 我用双链表 - 在不改变我的情况下,磁头正在改变

findA(9)

和我的列表被收缩到{9},虽然功能给出正确的结果给定的值是否在我的名单与否(true或false)

。另一方面,我的查找方法完美地工作,两者之间唯一的区别是我使用Node tmp = head in findA a nd Node tmp = head.next in find

下面是完整的代码片段。我意识到一些实现是非常不专业的。对任何意见,将不胜感激

public class Node <T extends Comparable<T>> { 
T data; 
Node prev; 
Node next; 

Node(){ 
} 

Node(T val){ 
    data = val; 
} } 

public class DLinkedList<T extends Comparable<T>> { 
Node head; 
Node tail; 

DLinkedList(){ 
    head = new Node();  
    tail = new Node(); 
    tail.prev = head; 
} 

void insertInOrder(T value){ 
    Node insert = new Node(value); 
    if(head.next==null){ 
     head.next = insert; 
     insert.prev = head; 
     insert.next = tail; 
     tail.prev = insert; 
    } 
    else{ 
     insert.prev = tail.prev; 
     tail.prev.next = insert; 
     tail.prev = insert; 
     insert.next = tail; 
    } 
} 

boolean find (T value){ 
    boolean result = false; 
    Node tmp = head.next; 
    if (head!=null){ 
     while(tmp!=null){ 
      if(tmp.data.compareTo(value)!=0){ 
       tmp = tmp.next; 
      } 
      else{ 
       result = true; 
       break; 
      } 
     } 
    } 
    return result; 
} 

boolean findA (T value){ 
    boolean result = false; 
    Node tmp = head; 
    if (head!=null){ 
     while(tmp.next!=null){ 
      if(tmp.next.data.compareTo(value)!=0){ 
       tmp.next = tmp.next.next; 
      } 
      else{ 
       result = true; 
       break; 
      } 
     } 
    } 
    return result; 
} 

void deleteA(T value){ 
    Node tmp = head.next; 

    while(tmp.data.compareTo(value)!=0){ 
      tmp = tmp.next; 
    } 
    if(tmp!=tail){ 
     if(tmp==head.next) 
      head = tmp.next; 
     else 
      tmp.prev.next = tmp.next; 

     if (tmp==tail) 
      tail = tmp.prev; 
     else 
      tmp.next.prev = tmp.prev; 
    } 






} 
void delete(T value){ 
    Node tmp = head.next; 
    if(find(value)){ 
     while(tmp!=tail){ 
      if(tmp.data.compareTo(value)!=0){ 
       tmp = tmp.next; 
      } 
      else{ 
       tmp.prev.next = tmp.next; 
       tmp.next.prev = tmp.prev; 

       break; 
      } 
     }    
    } 
} 

@Override 
public String toString(){ 
    Node tmp = head.next; 
    String result = ""; 
     while(tmp!=tail){ 
      System.out.println(tmp.data); 
      tmp = tmp.next; 
     } 
    return result; 
} } 

public class ListCheck { 

public static void main(String[] args) { 
    DLinkedList list = new DLinkedList(); 
    DLinkedList listA = new DLinkedList(); 
    for(int i=0; i<10; i++){ 
     list.insertInOrder(i); 
     listA.insertInOrder(i); 
    } 
    System.out.println(listA.findA(9)); 
    System.out.println(list.find(9)); 
    listA.toString(); 
    System.out.println(""); 
    list.toString(); 
} } 
+0

为什么要改变'tmp.next',而不是'tmp'?还有谁在乎头是空还是'tmp'就是'tail'。从头开始循环直到它为空。 – CollinD

+0

@CollinD因为我启动了我的tmp作为头,并且我的头没有按设计存储数据 – hsnsd

+0

是的,并且更改'tmp.next'将更改'head.next',因为'next'是一个引用。 – CollinD

回答

0

在你findA,您将TMP的方法是通过这种方式,你正在破坏当前指针做

tmp.next = temp.next.next 

,并重新路由到下一个节点( Java的浅拷贝):

tmp--->[node1]--->[node2]变化tmp--->[node2]

因此,在操作结束时,你的链表只有剩下的最后一个节点。

将其更改为tmp = tmp.next将有助于

相关问题