如何检查链接列表中是否存在元素?

时间:2014-03-23 17:38:17

标签: java vector linked-list

我必须检查链接列表中是否存在元素?我写了下面的代码,但它没有返回正确的输出。我无法找到我做错了什么。任何人都可以告诉我在下面的代码中做错了什么?预期输出为false且为true,但我得到的输出为false和false。

package BasicList1;


import java.util.Vector;

public class BasicList1 implements ListInterface{

    static String[] testcase1 = {"3","1","2","6","7","4","5"};

    public static void main (String[] args){
        BasicList1 testInstance = new BasicList1();
        ListNode head = new ListNode(testcase1[0]);
        ListNode node = head;
        System.out.println(testInstance.elements(head));
        System.out.println(testInstance.hasElement(head, "9"));
        System.out.println(testInstance.hasElement(head,"4"));
    }

    public BasicList1 getBasicList(String data){
        return this;
    }

    //write your code here
    public Vector<String> elements(ListNode head){
        ListNode temp=head;
        Vector v=new Vector();
        while(temp!=null){
            v.addElement(temp);
            temp=temp.next;
        }
        return v;
    }

    @Override
    public boolean hasElement(ListNode head, String data) {
        ListNode temp=head;
        while(temp!=null){
            if(temp.data.equals(data)){
                return true;
            }
            temp=temp.next;
        }
        return false;
    }

1 个答案:

答案 0 :(得分:0)

我不知道你输错的是什么,但我猜你的ListNode只包含一个元素:3。

这只会创建一个ListNode,其中包含一个包含testcase1[0]的节点("3"):

ListNode head = new ListNode(testcase1[0]);

因此,要初始化ListNode,您应该写出类似的内容:

ListNode head = new ListNode(testcase1[testcase1.length - 1]); 
for (int i = testcase1.length - 2; i >= 0; i--) {
     final ListNode tail = head;
     head = new ListNode(testcase1[i]);
     head.next = tail;
}

NodeList以相反的顺序构建,以避免遍历整个列表以在最后添加元素。

修改

您的elements方法中也存在问题。您正在添加ListNode而不是其内容,因此请替换

v.addElement(temp);

v.addElement(temp.data);

编辑2

此外,BasicList1设计非常糟糕:它不是真正的面向对象。你应该有这样的东西:

package test;

import java.util.Vector;

public class BasicList1 {

    private ListNode head;

    public BasicList1(String[] data) {

        if (data.length != 0 ) {
            head = new ListNode(data[data.length - 1]);
        }

        for (int i = data.length - 2; i >= 0; i--) {
            final ListNode tail = head;
            head = new ListNode(data[i]);
            head.next = tail;
        }
    }

    public BasicList1 getBasicList() {
        return this;
    }

    //write your code here
    public Vector<String> elements(){
        ListNode temp = head;
        Vector<String> v=new Vector<String>();
        while(temp!=null){
            v.addElement(temp.data);
            temp=temp.next;
        }
        return v;
    }

    @Override
    public boolean hasElement(String data) {
        ListNode temp=head;
        while(temp!=null){
            if(temp.data.equals(data)){
                return true;
            }
            temp=temp.next;
        }
        return false;
    }

    public static void main (String[] args){
        final String[] testcase1 = {"3","1","2","6","7","4","5"};
        final BasicList1 testInstance = new BasicList1(testcase1);

        System.out.println(testInstance.elements());
        System.out.println(testInstance.hasElement("9"));
        System.out.println(testInstance.hasElement("4"));
    }
}