• Post Reply Bookmark Topic Watch Topic
  • New Topic

Check my code for recursion please?  RSS feed

 
Sofia Pineda
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have tried to do the following for matching brackets using recursion, but someone has told me that a stack is not necessary for this but I have no idea of what other methods I could use.... only ArrayDeque, but isn't that like a stack? Hmmm, I also have trouble getting the recursion part correct because I cannot use the values from one method to another, can anyone help me? Thanks!

 
Maxim Karvonen
Ranch Hand
Posts: 121
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Sofia.

but someone has told me that a stack is not necessary for this

you should read this as "You should not need a explicit/manual/handmade stack for this task". Recursion involves usage of "method call stack" by itself. But yes, explicit stack is not needed for this task.

I also have trouble getting the recursion part correct because I cannot use the values from one method to another

You may pass required values as arguments to other methods. You recursiveMatch method already have a value (src) which is passed between methods. Or you may return a value from a method.

I provide a more concrete algorithms to start with.

Write method, which reads "a well-formed bracketed sequence ending with a specified character". It may be implemented as:
1. Read a char
2. If it is an opening char, then you now should read "a well-formed bracketed sequence ending with a (closing bracket for a read char)". See, it's our current method.
3. If it is closing bracket, then check, if it is a correct bracket or not. Produce result if needed.

Try to implement that. Don't try to generate additional information yet (positions, indices, etc...). Just check if a sequence is well-formed. When this will work, implement other task requirements such as saving locations (you will need to pass a storage as an argument, maybe you also need to pass/return stream positions, etc...).
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sofia Pineda wrote:Hi, I have tried to do the following...

Sofia,

Please DontWriteLongLines. It makes your thread very hard to read. I've broken the worst of yours up this time, but for future reference, please remember:
80 characters max.
And that includes string literals and comments.

Thanks.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!