1

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.

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;
    }
newbie
  • 109
  • 1
  • 10
  • 7
    Don't use `==` to compare Strings. Use `equals`. – Alexis C. Mar 23 '14 at 17:39
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Jason C Mar 23 '14 at 17:40
  • Still getting wrong output. Anything else ? – newbie Mar 23 '14 at 17:40
  • 1
    @newbie Yes. After comparing strings correctly, please post details of your actual vs. expected output ("not returning the right output" is not helpful) as well as any information that you discovered while debugging (e.g. when you print the contents of your list, do you see the item you expect to be there?). Your `hasElement()` looks fine otherwise. I don't see you actually adding "9" or "4" to your list, by the way. – Jason C Mar 23 '14 at 17:41
  • I edited my question.Please check???@JasonC – newbie Mar 23 '14 at 18:02
  • Please, tell us the result you get and the result you expect. – Benoît Guédas Mar 23 '14 at 18:29

1 Answers1

0

I do not know what your wrong output is, but I guess your ListNode only contains a single element: 3.

This only creates a ListNode with a single node containing testcase1[0] (which is "3"):

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

So, to initialize your ListNode, you should write something like that:

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.

EDIT

There is also a problem within your elements method. You're adding the ListNode instead of its contents, so replace

v.addElement(temp);

with

v.addElement(temp.data);

EDIT 2

Moreover, the BasicList1 design is quite bad: it is not really object oriented. 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"));
    }
}
Benoît Guédas
  • 801
  • 7
  • 25
  • No effect.Still getting the same output.It is only checking the data with the first value of linked list. – newbie Mar 23 '14 at 20:39