1 Replies - 841 Views - Last Post: 05 February 2012 - 04:54 PM Rate Topic: -----

#1 monster92   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 10-May 11

Problem is with my recursive pseudo code

Posted 05 February 2012 - 03:49 PM

Ok I have written a program samelist(List1, List2). IT is supposed to check two lists for equality. However whenever I try to implement it in Java it doesn't work. A friend told me it is with the pseudo code I have written shown below. There's a mistake there but I cannot see it. Sorry again to be cheeky but if you find a way to correct can you provide an example of how it would work? (please please please don't answer the question in Java)

samelist(L1,L2) {  
if(L1==[] and L2==[]) return true;
	else return (head(L1)==head(L2) and samelist(tail(L1), tail(L2))); 
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem is with my recursive pseudo code

#2 skaoth   User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: Problem is with my recursive pseudo code

Posted 05 February 2012 - 04:54 PM

Something is wrong with your pseudo code.
I'm now sure what tail() returns but something doesn't feel right with
samelist(tail(L1), tail(L2))

The way I look at this problem is that we just simply want to check if each element in
the list is the same.

Pseudo code:
L1 = {1, 2, 3, 4}
L2 = {1, 2, 3, 4}

sameList(L1, L2, 0)

boolean sameList(L1, L2, index) {
// note NOT check for indexing exceptions
return (L1[index] == L2[index] && sameList(L1, L2, index + 1)
}

}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1