0

Returns true if the string represented - by the object it is on the same string is represented by the object str. Otherwise false is returned. This system must write the recursion without using any loops.

So I did it and it looks like this:

 public boolean equals ( StringList str ) 
    {
       if ((str._head == null) && (_head == null) )
       return  true;
       else 

        if( str._head.getData()==_head.getData() && str._head.getValue() == _head.getValue())
            {
            _head = _head.getNext();
            str._head = str._head.getNext();

               return  equals( str);  
           }
      return false;

    }

The problem is that I lose the head of the list and the entire list that as I progress I run over it .

Edited by YaLeon

2
Contributors
6
Replies
7
Views
4 Years
Discussion Span
Last Post by mvmalderen
0

example of Linked List
if I have string like :"aabb" so linked list will be " (head)a,2=> b,2=> null
if I have string like "abba" so linked list will be (head)a,1=>b,2=>a,1=> null

0

The quick and dirty way to fix it would probably be creating a separate instance variable to help in the recursion.

Edit: Bad approach, don't use it. See later posts.

Edited by mvmalderen

0

The problem is if you will do that every time recursevile it will be changed.

0

The problem is if you will do that every time recursevile it will be changed.

Yes, that's one of the reasons why I said "quick and dirty".
In your current version it is happening too though.

But I'm wondering, can't you create a private helper method that works on the node level?

Perhaps something like the following?

private boolean recursiveEquals(Node a, Node b) {
    // Make this your recursive method.
}

public boolean equals(StringList list) {
    return recursiveEquals(this._head, list._head);
}

Edited by mvmalderen

0

yeahh overloading will help here i'm pretty sure
i just tried to do that with out it.

thanks.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.