0

I'm getting a Stack overflow error for the following method at the recursive call. I think I need to make it iterative in order for it to work. How would I write the following method iteratively?

Node myMethod(String foo, Node p) {
   if(p == null) {
       p = new Node(foo);
       p.link = spot;
       spot = p;
       p.calc[bar] = 1;
   return p;
   } else if(foo.equals(p.origin)) {
       p.calc[bar] = p.calc[bar] + 1;
       return p;
   } else { 
         while (p.next == null & foo.equals(p.origin)){
             p = p.next;
         }
       p.next = myMethod(foo, p.next);
       return p;
   }
}

p is a Node class that has String foo, String origin, Node link, Node next, and int Array calc[]. bar is an int. spot is a random node.

This is what I have tried so far

Node myMethod(String foo, Node p) {
if(p == null) {
   p = new Node(foo);
   p.link = spot;
   spot = p;
   p.calc[bar] = 1;
return p;
} else if(foo.equals(p.origin)) {
    p.calc[bar] = p.calc[bar] + 1;
    return p;
} else { 
    while (p.next == null & foo.equals(p.origin)){
         p = p.next;
    }
//instead of doing recursion on: p.next = myMethod(foo, p.next);. I tried the following:
    if (p.next == null){
        p.next = new Node(foo);
        p.next.link = spot;
        spot = p.next;
        p.next.calc[bar] = 1;
    } else if (foo.equals(p.next.origin)){
        p.next.calc[bar] = p.next.calc[bar] + 1;
    } else {
        while (p.next.next == null & foo.equals(p.next.origin)){
        p.next = p.next.next;
        }
      }
   }
 return p;
 }
10
  • 1
    Also, a name like myMethod tells us nothing about the purpose of this method... Commented Apr 20, 2014 at 20:23
  • 1
    @OliCharlesworth see edits. myMethod creates a node (part of a linked list) and returns it. Commented Apr 20, 2014 at 20:39
  • Didn't you mean && in while (p.next == null & foo.equals(p.origin)? It does not appear that you intended to operate bitwise. Commented Apr 20, 2014 at 20:55
  • I replaced the & with && but I'm still getting the Stack Overflow error. Commented Apr 20, 2014 at 21:04
  • That while loop does not make any sense in your recursive algorithm: condition is never true. Should it be: != null and ! equals? Commented Apr 20, 2014 at 21:20

2 Answers 2

1

Although your iterative-try probably could be written better, this is a good try. However, look here:

else { 
    while (p.next == null & foo.equals(p.origin)){ //HERE! Check the while argument.
         p = p.next;
    } //instead of doing recursion on: p.next = myMethod(foo, p.next);. I tried the following:
    …

The while loop will stop if p.next is null or foo does not equal p.origin. Now the basic approach would be to serve three cases here:

  1. what to do when p.next != null (first while condition)
  2. what to do when !foo.equals(p.origin) (second while condition) //you did not write it.
  3. what to do when p.next != null && !foo.equals(p.origin)

If your while conditions are correct (ones you want them to be), you need to serve all cases for which the while loop may have stopped and that should be end of your algorithm. If it is not, rethink your while loop condition.

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

Comments

0

I'm still not entirely shure what you are trying to do and this code looks like bad programming style but maybe it will do what you need.

Node myMethod(String foo, Node p) {
    while(p != null) {
        if(foo.equals(p.origin)) {
            p.calc[bar] = p.calc[bar] + 1;
            break;
        }
        p = p.next;
    }
    if(p == null) {
        p = new Node(foo);
        p.link = spot;
        spot = p;
        p.calc[bar] = 1;
    }
    return p;
}

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.