Recursion Problem

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2007
Posts: 6
Reputation: VirusTalker is an unknown quantity at this point 
Solved Threads: 1
VirusTalker VirusTalker is offline Offline
Newbie Poster

Recursion Problem

 
0
  #1
Dec 3rd, 2008
	public static boolean member(Integer obj,SimpleList<Integer> l);
	// returns true if obj is a member of l, false otherwise
	{
	
	}

the question is how could I use recursion to show that there is a member of a list?

I thought that maybe
if (l.isEmpty())
{return false}
else
?

thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Recursion Problem

 
0
  #2
Dec 3rd, 2008
Could you detail a bit more.
Last edited by verruckt24; Dec 3rd, 2008 at 12:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: VirusTalker is an unknown quantity at this point 
Solved Threads: 1
VirusTalker VirusTalker is offline Offline
Newbie Poster

Re: Recursion Problem

 
0
  #3
Dec 3rd, 2008
Originally Posted by verruckt24 View Post
Could you detail a bit more.
Basically I have objects in the SimpleList Routines. I need to manipulate them using recursion. Here is the code for SimpleList

public class SimpleList<E> {
    private ListNode<E> list;
    /**
      Constructs an empty list.
     */
    public SimpleList(){
        list = null;
    }
    /**
      Constructs a single-item list containing x.
     */
    public SimpleList(E x) {
        list = new ListNode<E>(x,null);
    }
    /**
      Constructs a list with head x and tail l.
    */
    public SimpleList(E x, SimpleList<E> l) {
        list = new ListNode<E>(x, copyNodes(l.list));
    }
    /**
      Constructs a list containing node n. Used to create a
      SimpleList from a list of ListNodes.
     */
    public SimpleList(ListNode<E> n) {
        list = copyNodes(n);
    }
    /**
      Used to extract the head of a list.
      @throws Exceptions.ItemNotFound if the list is empty
      @return the head of the list
    */
    public E head() throws ItemNotFound { 
        if (isEmpty())
            throw new ItemNotFound("empty list");
        return list.element;
    }
    /**
      Used to extract the tail of a list. 
      @throws Exceptions.ItemNotFound if the list is empty.
      @return the tail of the list
    */
    public SimpleList<E> tail() throws ItemNotFound {
        if (isEmpty())
            throw new ItemNotFound("empty list");
        else 
            // note that the list copy is in 
            // the constructor
            return new SimpleList<E>(list.next);
    }
    private ListNode<E> copyNodes(ListNode<E> l) {
        if (l == null)
            return null;
        else 
            return new ListNode<E>(l.element, copyNodes(l.next));
    }
    /**
     * Test if the list is logically empty.
     * @return true if empty, false otherwise.
     */
    public boolean isEmpty(){ return list == null; }
    /**
     * Make the list logically empty.
     */
    public void makeEmpty() { list = null; }
    /**
      @return A String representation of the list
     */
    public String toString() {
        String s = "[";
        try{
            if (!this.isEmpty())
                s += getString(this.head()) +
                    this.tail().toString_Nodes();
        } catch (ItemNotFound e) {};
        return s + "]";
    }
    private String toString_Nodes(){
        String s = "";
        try{
            if (!this.isEmpty())
                s += "," + getString(this.head()) + 
                    this.tail().toString_Nodes();
        } catch (ItemNotFound e) {};
        return s;
    }
    private String getString(Object obj) {
        return (obj == null ? "null" : obj.toString());
    }
    //=============================================
     private class ListNode<E> {

        E element;
        ListNode<E> next;

        ListNode( E theElement ) {
            this( theElement, null );
        }

        ListNode( E theElement, ListNode<E> n ) {
            element = theElement;
            next    = n;
        }
    }

}

///////////////////////////////////////////////

class ItemNotFound extends RuntimeException {

    public ItemNotFound( String message ) {
        super( message );
    }

}
What I have so far is:
	public static boolean member(Integer obj,SimpleList<Integer> l);
	// returns true if obj is a member of l, false otherwise
	{
		if(l.isEmpty());
			return false;
		else ()
	}
I figured since if the list is empty then it would not have obj as a member. However Im having a hard time trying to understand the logic of this and how to use it in recursion.
Last edited by VirusTalker; Dec 3rd, 2008 at 7:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: mahlerfive is an unknown quantity at this point 
Solved Threads: 16
mahlerfive mahlerfive is offline Offline
Junior Poster in Training

Re: Recursion Problem

 
0
  #4
Dec 3rd, 2008
Generally when you are doing recursion on any kind of collection of items (lists, arrays, even strings) you want to solve the problem for the first element of the collection, then recurse on the remaining part of the collection.

For this example you can look at the first element of the list, check if that is the object you are looking for. If it is, then you are done. If it's not, you recurse on the rest of the list (without that first element).

Now, because your function is only taking the list and search object as a parameter, you have no way of sending a portion of a list to recurse on. What you will have to do is create a second recursive function (often called the auxiliary recursive function) that takes an additional parameter of the location in the list to start searching from. When someone calls your original function, you just call the auxiliary function with the location as 0.

Try coding that up and if you have problems, post both functions.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: VirusTalker is an unknown quantity at this point 
Solved Threads: 1
VirusTalker VirusTalker is offline Offline
Newbie Poster

Re: Recursion Problem

 
0
  #5
Dec 4th, 2008
That really helped my thought process. I figured it out! lol took me a while but i finally got what you where saying. I did it with a try catch block and it worked out perfectly. Thanks again!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC