日期:2014-05-20  浏览次数:20783 次

链表的查找
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() {