5

for homework I was asked to write a contain method for a custom linked list. I know that the recursive method should have a base case and then the recursive case.However, I am having some trouble understanding how to write the recursive case of the method. So far this is what I have written, but my code is executing the base case more than once. Can you please give me some guidance?

public class OrderedList {

private Node first;

//Constructor
public OrderedList() {
    this.first = null;
}

//Return the number of items in the list
public int size() {
    int counter = 0;
    Node pointer = this.first;
    while (pointer != null) {
        counter++;
        pointer = pointer.next;
    }
    return counter;
}

//Return an array of copies of the stored elements
public Comparable[] getStore() {

    Comparable[] elements = new Comparable[size()];
    Node pointer = this.first;
    if (this.first == null) {
        return elements;
    } else {
        int i = 0;
        while (pointer != null) {
            elements[i] = pointer.data;
            pointer = pointer.next;
            i++;
        }
        return elements;
    }

}
//true iff item matches a stored element
//Recursive

public boolean contains(Comparable item) {

    //Base case
    if (this.first == null) {

        return false;
    }
    Node pointer = this.first;
    this.first = this.first.next;

    if (pointer.data.compareTo(item) == 0) {

        return true;

    } 
    //Recursive case

    else {

        boolean info = contains(item);
        pointer.next = this.first;
        this.first = pointer;

        return info;
    }
}
1
  • Why are you changing the class variable in that method? You should be using a passed in Node, not this.first. You are changing the top of the list with every call of that method. You are destroying your list! Commented Jul 22, 2013 at 18:19

3 Answers 3

3

First of all I like to do something like this:

public boolean contains(Comparable item)
{
     return containsHelper(this.first, Comparable item);
}

private boolean containsHelper(Node node, Comparable item)
{
    //base case
    if(node == null)
    {   
         return false;
    }
    else
    {
         if(node.data.compareTo(item) == 0)
         {
             return true;
         }

         return containsHelper(node.next, item);
    }


}

This hides implementation details from the user and it stops your list from getting overridden when you run that method.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way for me to keep the method from overriding the list without having to implement a helper method?
@user1166061 Not really, this is the standard approach. Otherwise you would have to rely on the user of your code to pass in the first node which is ruining encapsulation. I mean you can see that I have reduced the amount of code you had so I am not sure why you would want to avoid the helper!
0

To implement a recursive solution, you need an auxiliary method for contains. The auxiliary method should have an additional argument that is the Node from which to start testing. The public contains method should call the auxiliary method and pass this.first as the start node. The rest of the logic should then be pretty simple for you to figure out.

4 Comments

Is there a way for me to be able to conserve the list by using this method only?
@user1166061 - The auxiliary method can be written to not modify anything. I don't think you can write a recursive contains method without an auxiliary method.
@TedHopp I think the only way would be requiring the client to use code like list.contains(list.getFirst(), item) which is no good imo
@thatidiotguy - I agree that it would be no good to do it that way. It would also change the public interface, which would probably mean that the assignment was not done (even if the method worked correctly).
0

From what I am seeing, your code will return true once the else statemnet have been executed once. I think what you need to do is to set the boolean value to false everytime because recursion acts very much like a while loop and if the values are not updated, the base case would be executed over and over again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.