简体   繁体   English

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

[英]How can I check if an element is present in a linkedlist?

I have to check if an element is present in a linkedlist or not? 我必须检查链表中是否存在某个元素? I wrote the following code but it is not returning the right output.I m unable to find what I did wrong.Can anyone tell what I did wrong in the following code? 我写了下面的代码,但是没有返回正确的输出,我找不到我做错了什么,谁能在下面的代码中告诉我我做错了吗? The expected output is false and true but the output I am getting is false and false. 预期的输出是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;
    }

I do not know what your wrong output is, but I guess your ListNode only contains a single element: 3. 我不知道您的错误输出是什么,但是我想您的ListNode仅包含一个元素:3。

This only creates a ListNode with a single node containing testcase1[0] (which is "3" ): 这只会创建一个ListNode其中的单个节点包含testcase1[0] (它是"3" ):

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

So, to initialize your ListNode , you should write something like that: 因此,要初始化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;
}

The NodeList is built in the reversed order to avoid walking through the whole list to add elements at the end. 以相反的顺序构建NodeList ,以避免遍历整个列表以在末尾添加元素。

EDIT 编辑

There is also a problem within your elements method. 您的elements方法内也存在问题。 You're adding the ListNode instead of its contents, so replace 您要添加ListNode而不是其内容,因此请替换

v.addElement(temp);

with

v.addElement(temp.data);

EDIT 2 编辑2

Moreover, the BasicList1 design is quite bad: it is not really object oriented. 而且, BasicList1设计非常糟糕:它不是真正面向对象的。 You should have something like this: 您应该具有以下内容:

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"));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查LinkedList中的索引处是否存在元素 - How can I check if an element exists at an index in a LinkedList 如何使用 jdom 检查文件中是否存在元素 - How can I use jdom to check wether an element is present in a file or not 如何在添加元素之前检查队列 ( LinkedList ) 中是否存在元素? - How do I check if an element exists in a queue ( LinkedList ) before adding it? 如何互相检查LinkedList中的几个值? - How can I check a LinkedList for several values after eachother? 如果我要检查Selenium Webdriver中是否存在该元素,该怎么办? - What can be used if I want to check if the element is present or not in selenium webdriver? 如何实现LinkedList的可迭代 <LinkedList<obj> &gt; - how can I implement iterable for LinkedList<LinkedList<obj>> Java:如何检查字符串是否是任何LinkedList元素的一部分? - Java: How to check if a string is a part of any LinkedList element? 如何为文件的每一行创建一个带有链表的最后一个元素的新目录 - How Can I create a new Directory with the last element of the linkedList for each line of the file 在REST Assured中,如何检查响应中是否存在字段? - In REST Assured, how can I check if a field is present or not in the response? 如何显示LinkedList的所有内容? - How can I display all the contents of the LinkedList?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM