首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

链表的查找解决方法

2012-01-24 
链表的查找Java codepackage ShuJuJieGou.Ch05/** * * 双端链表 */class Link4 {public long dDatapubli

链表的查找

Java code
package ShuJuJieGou.Ch05;/** * * 双端链表 */class Link4 {    public long dData;    public Link4 next;    public Link4(long d) {        dData = d;    }    public void displayLink() {        System.out.print(dData + " ");    }}class FirstLastList {    private Link4 first;    private Link4 last;    public FirstLastList() {        first = null;        last = null;    }    public boolean isEmpty() {        return first == null;    }    public void insertFirst(long dd) {        Link4 newlink = new Link4(dd);        if (isEmpty()) {            last = newlink;        }        newlink.next = first;        first = newlink;    }    //first 和 last 是同一个对象,每次插入后,last都指向最后一个值    public void insertlast(long dd) {        Link4 newlink = new Link4(dd);        if (isEmpty()) {            first = newlink;        } else {            last.next = newlink;        }        last = newlink;    }    public long deleteFirst() {        long temp = first.dData;        if (first.next == null) {            last = null;        }        first = first.next;        return temp;    }    //查找方法    public Link4 find(long d) {        Link4 temp = first;        while (first.dData != d) {            if (temp == null) {                return null;            }else{            temp = temp.next;            }        }        return temp;    }    //删除    public Link4 delete(long d) {        Link4 persous = first;        Link4 current = first;        while (current.dData != d) {            if (current == null) {                return null;            } else {                persous = current;                current = current.next;            }        }        if (current == first) {            first = first.next;        } else {            persous.next = current.next;        }        return current;    }    public void displayList() {        System.out.print("List(first-->last):");        Link4 current = first;        while (current != null) {            current.displayLink();            current = current.next;        }        System.out.println("");    }}public class Test4 {    public static void main(String[] args) {        FirstLastList thelist = new FirstLastList();        thelist.insertFirst(22);        thelist.insertFirst(44);        thelist.insertFirst(66);        thelist.insertFirst(88);        thelist.insertlast(11);        thelist.insertlast(33);        thelist.insertlast(55);        thelist.displayList();        thelist.deleteFirst();        thelist.deleteFirst();        thelist.displayList();        //测试删除        thelist.delete(11);        thelist.displayList();        //测试查找                                                错误在这里,我在一个单链表使用这个查找功能正常使用,而在双端链表却不行        Link4 temp=thelist.find(22);        temp.displayLink();    }}


[解决办法]
Java code
 public Link4 find(long d) {        Link4 temp = first;        while (first.dData != d) {            if (temp == null) {                return null;            }else{            temp = temp.next;//这里temp.next是不是为null            }        }        return temp;    }
[解决办法]
Java code
 
class Link4 {

  public long dData;
  public Link4 next;

  public Link4(long d) {
    dData = d;
  }

  public void displayLink() {
 
    System.out.print(dData + " ");
  }
}

class FirstLastList {

  private Link4 first;
  private Link4 last;

  public FirstLastList() {


    first = null;
    last = null;
  }

  public boolean isEmpty() {
    return first == null;
  }

  public void insertFirst(long dd) {
    Link4 newlink = new Link4(dd);

    if (isEmpty()) {
      last = newlink;
    }
    newlink.next = first;
    first = newlink;
  }

  //first 和 last 是同一个对象,每次插入后,last都指向最后一个值
  public void insertlast(long dd) {
    Link4 newlink = new Link4(dd);
    if (isEmpty()) {
      first = newlink;
    } else {
      last.next = newlink;
    }
    last = newlink;
  }

  public long deleteFirst() {
    long temp = first.dData;
    if (first.next == null) {
      last = null;
    }
    first = first.next;
    return temp;
  }

  //查找方法
  public Link4 find(long d) {
    Link4 temp = first;
    while (temp.dData != d) {
      if (temp == null) {
        return null;
      }else{
      temp = temp.next;
      if(temp == null) {//这样改就OK了
      return null;
      }

      }
    }
    return temp;
  }

  //删除
  public Link4 delete(long d) {
    Link4 persous = first;
    Link4 current = first;
    while (current.dData != d) {

      if (current == null) {
        return null;
      } else {
        persous = current;
        current = current.next;
      }
    }
    if (current == first) {
      first = first.next;
    } else {
      persous.next = current.next;
    }
    return current;
  }

  public void displayList() {
    System.out.print("List(first-->last):");
    Link4 current = first;
   
    while (current != null) {
      current.displayLink();
      current = current.next;
    }
    System.out.println("");
  }
}

public class Test4 {

  public static void main(String[] args) {
    FirstLastList thelist = new FirstLastList();

    thelist.insertFirst(22);
    thelist.insertFirst(44);
    thelist.insertFirst(66);
    thelist.insertFirst(88);

    thelist.insertlast(11);
    thelist.insertlast(33);
    thelist.insertlast(55);

    thelist.displayList();

    thelist.deleteFirst();
    thelist.deleteFirst();
    thelist.displayList();


    //测试删除
    thelist.delete(11);
    thelist.displayList();

    //测试查找                        错误在这里,我在一个单链表使用这个查找功能正常使用,而在双端链表却不行
    Link4 temp=thelist.find(66);
    if(temp == null) {
    System.out.println("没有这个数!");


    }else{
    temp.displayLink();
    }
  }
}



[解决办法]
查找方法跟main函数的错误差不多,都需要判断temp是否为null
在main函数最后要判断一下temp要是为null的话就不能引用temp.dData,否则会引发异常

热点排行