链接列表搜索方法

时间:2016-10-22 18:05:20

标签: linked-list

我正在练习构建包含字符串值的双向链表。

在find方法中,我有一个NullPointer异常

这是我的代码。

package LinkedList;



package LinkedList;

public class LinkedList {

// 노드 클래스
class Node {
    String value;
    Node prev;
    Node next;

    Node(String v, Node p, Node s) {

        value = v;
        prev = p;
        next = s;
    }

    public String getValue() {
        return value;
    }

    public Node getPrev() {
        return prev;
    }

    public Node getNext() {
        return next;
    }

    public void setPrev(Node p) {
        prev = p;
    }

    public void setNext(Node n) {
        next = n;
    }

}

Node head;
Node tail;
int size = 0;

public LinkedList() {
    head = new Node(null, null, null);
    tail = new Node(null, head, null);
    head.setNext(tail);
}

public int size() {
    return size;
}

public boolean isEmpty() {
    return size == 0;
}

public String first() {
    if (isEmpty()) {
        return null;
    }
    return head.getNext().getValue();
}

public String last() {
    if (isEmpty()) {
        return null;
    }
    return tail.getPrev().getValue();
}

public void addFirst(String value) {
    addBetween(value, head, head.getNext());
}

public void addLast(String value) {
    addBetween(value, tail.getPrev(), tail);
}

public void addBetween(String v, Node p, Node s) {
    Node newNode = new Node(v, p, s);
    p.setNext(newNode);
    s.setPrev(newNode);
    size++;
}

public String remove(Node node) {
    Node p = node.getPrev();
    Node s = node.getNext();
    p.setNext(s);
    s.setPrev(p);
    size--;
    return node.getValue();
}

public String removeFirst() {
    if (isEmpty()) {
        return null;
    }
    return remove(head.getNext());
}

public String removeLast() {
    if (isEmpty()) {
        return null;
    }
    return remove(tail.getPrev());
}

public void insert(String value) {
    Node current = head;
    // first
    if (isEmpty()) {
        addFirst(value);
    } else {
        // check
        while (current.getNext() != tail || current.getValue().compareTo(value) > 0) {
            current = current.getNext();
        }
        // last
        if (current.getNext() == tail) {
            addLast(value);
        } else // between
        {
            addBetween(value, current.getNext(), current);
        }

    }
}

/* !!!!!!!!!!!!!! ERORR !!!!!!!!!!!! */
public void find(String value) {

    Node current = head.getNext();

    while ((current != null) || !(current.getValue().equals(value)))

        current = current.getNext();

    if (current.getValue().equals(value)) {
        System.out.println("found " + value);
    } else {
        System.out.println("Not found " + value);
    }

}

// • Traverse the list forwards and print
// 순회
public void fowardTraverse() {
    Node current = head.getNext();

    System.out.print(current.getValue());

    while (current.getNext() != tail) {
        current = current.getNext();
        System.out.print(" -> " + current.getValue());
    }

}

// • Traverse the list backwards and print
// 뒤로 순회
public void backwardTraverse() {

    Node current = tail.getPrev();

    System.out.print(current.getValue());

    while (current.getPrev() != head) {
        current = current.getPrev();
        System.out.print(" -> " + current.getValue());
    }

}

// • Delete a node from the list
     // 지우기
     public String delete(String value) {
        return value;
    }

    // • Delete/destroy the list
    // 파괴하기
    public void destroy() {

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stubs

        LinkedList a = new LinkedList();
        a.insert("a");
        a.insert("b");
        a.insert("c");
        a.insert("d");
        a.insert("e");
        a.insert("f");
        a.insert("g");
        a.insert("h");
        a.insert("i");
        // a.fowardTraverse();
        a.find("a");

    }

我不明白为什么我在行

处得到nullpointException

假设放一个节点包含一个。

2 个答案:

答案 0 :(得分:0)

确保在解除引用前检查非NULL:

Node current = head.getNext();

if (current.getValue().equals(value)) {  

替换为

Node current; 
if(head != NULL) current = head.getNext();

if (current != NULL && current.getValue().equals(value)) {  

答案 1 :(得分:0)

因为你的头是空的......(没有双关语) 在Addfirst调用之前..你的结构: 头= [NULL,NULL,尾],尾= [零,头,空]。 你正在发送(" a",head,tail) 并将其存储在新节点中,使其成为如下结构: 头= [NULL,NULL,newNode] ==> newNode ["",头,尾] ==>尾[NULL,newNode,空]

所以搜索会比较null和a(在find中)给你错误..... ---编辑1: @JanghyupLee,我的坏,没有仔细查看查找方法......但是,我发现在条件为#34;如果"你正在使用条件

 current != null  || ......

在第一行(current = head.next)之后..当前变为非空..

造成条件的同时忽略' ||'的正确部分。 (短路)直到电流值为零...

一旦当前变为null,它将转到下一个语句以检查值。使用空指针异常