在递归中打印queue.peek()会打印不相关的值

时间:2018-10-04 05:45:00

标签: java recursion queue

我正在尝试使用队列检查链表是否是回文。
 如果链接列表是回文式,solve()函数将返回 true 。即使节点和值相等,将q.peek与Node值等同也会返回 false
 尝试打印q.peek()返回 LList $ Node @ 7852e922
 我做了谷歌它说像队列节点值是在以前的功能调用中使用,并没有得到多少。

public class LList {
    private Node head = null;

    static class Node {
        int data;
        Node next;

        Node(int d) {
            data = d;
            next = null;
        }
    }

    public void push(int d) {
        Node n = new Node(d);
        n.next = head;
        head = n;
    }

    public boolean solve(Node t, Queue q) {
        if (t == null) {
            return true;
        }

        q.add(t.data);
        if (solve(t.next, q)) {
            **System.out.println(q.peek());**//LList$Node@7852e922
            if (q.peek().equals(t.data)) {
                q.remove();
            } else {
                return false;
            }
        } else {
            return false;
        }
        return true;
    }

    public static void main(String args[]) {
        LList lList = new LList();
        lList.push(5);
        lList.push(4);
        lList.push(3);
        lList.push(4);
        lList.push(5);
        Queue<Integer> q = new LinkedList<Integer>();
        System.out.println(lList.solve(lList.head, q));
    }
}

2 个答案:

答案 0 :(得分:0)

您已声明Queue for Integer,但您正在尝试在队列中插入Node。 将比较q.peek().equals(t.data)更改为q.peek().data == t.data,将队列类型更改为Queue<Node> q = new LinkedList<Node>()

工作代码为(已对所做的更改添加了注释):

public class LList {
  //Made head static
  private static Node head = null;

  static class Node {
    int data;
    Node next;

    Node(int d) {
        data = d;
        next = null;
    }
  }

  public void push(int d) {
    Node n = new Node(d);
    n.next = head;
    head = n;
  }

  //changed the parameter type to Queue<Integer>
  public boolean solve(Node t, Queue<Integer> q) {
    if (t == null) {
        return true;
    }
    if (q == null) {
        q = new LinkedList<>();
    }
    q.add(t.data);
    if (solve(t.next, q)) {
        System.out.println(q.peek()); //prints 5 4 3 4 5
        //changed the comparison condition.
        if (q.peek() == t.data) {
            q.remove();
        } else {
            return false;
        }
    } else
        return false;
    return true;
  }

  public static void main(String args[]) {
    LList lList = new LList();
    lList.push(5);
    lList.push(4);
    lList.push(3);
    lList.push(4);
    lList.push(5);
    System.out.println(lList.solve(lList.head, null));
  }
}

输出:

5
4
3
4
5
true

答案 1 :(得分:0)

正如@Pramod所说,您在此处添加一个要排队的节点:q.dd(t)

在这种方法中:

public boolean solve(Node t, Queue q) {
    if (t == null) {
        return true;
    }
    if (q == null) {
        q = new LinkedList<Integer>();
    }
    q.add(t);
    if (solve(t.next, q)) {
        **System.out.println(q.peek());**//LList$Node@7852e922
        if (q.peek().equals(t.data)) {
            q.remove();
        } else {
            return false;
        }
    } else
        return false;
    return true;
}

您是要q.add(t.data)吗?