Hi math01-ga,
Glad to have you back! This time, instead of posting the source
code for your answer here, I have provided links to the java source
files containing the code. All you have to do is download the file and
compile it. As usual, all code has been tested with JDK 1.4
On to the answers:
+-------------------------------------------------+
Algorithm for Parenthesis Matching:
- Parse the input string character by character, from left to
right
- When we encounter a opening bracket "(", we push it into the
stack
- When we encounter a closing bracket ")", we pop the a bracket
from the stack. As long as there exists a corresponding "(" in the
stack for every ")" we encounter, the string is properly parenthesised
else its mismatched.
- If we have finished parsing the string, but the stack is still
not empty, then there is a mismatch.(Too many opening brackets)
=================================================
Please download the file indicated below to see an implementation of
the above Parentheses Matching algorithm in Java.
- MatchingTest.java
( http://member.isavvix.com/theta_ga/MatchingTest.java )
From your question, it was not clear whether you were interested in
the implementation of the algorithm only, or both algoritm and a Stack
class. To save time, I have used Java's built in Stack class from the
package java.util. You can find the help documentation for this class
here:
- java.util.Stack
( http://java.sun.com/j2se/1.4/docs/api/java/util/Stack.html )
If, perchance, you wanted an implementation a stack class also, just
ask for a clarification, and I will post the code for you here.
+-------------------------------------------------+
You can download the java files containing the code for the solution
to the Queues question from the locations linked below:
- Queue.java
( http://member.isavvix.com/theta_ga/Queue.java )
- QueueTest.java
( http://member.isavvix.com/theta_ga/QueueTest.java )
You can easily find the code I have added in the above files, as my
additions are preceeded by the following comment line:
" // ***** NEW ADDITION ***** "
The code has ample comments, so it should be easily understandable.
However, if you have any confusion or doubts, please ask for a
clarification, and I will provide a more in depth explaination.
+-------------------------------------------------+
Hope this helps.
If you need any clarifications, just ask!
Regards,
Theta-ga
:-)
==================================
Google Search Terms Used:
java util stack |
Clarification of Answer by
theta-ga
on
27 Feb 2003 07:33 PST
Hi math01-ga,
The nth() method, as required by your question, is declared at the
very end of the file Queue.java. The supporting code to test the
method can be found at the end of the main() method in the file
QueueTest.java
As for the MatchingTest program, I tested it again, and it works as
asked. Perhaps you could provide more detail about the problems you
are experiencing?
The file (MatchingTest.java) contains two methods. They are:
- main(): The main method asks the user to input a parenthesised
string. It then passes this input string to the method IsMatching(),
and then displays IsMatching()'s return value on the screen.
- IsMatching(String): This method takes a parenthesised string as
its argument. It returns true if the string is properly parenthesised
and false if it is not. This is the method which implements the
algorithm that was outlined in my earlier answer.
This is the output I got when I tested out MatchingTest:
Enter the parenthesised string: (()()((())()))
Algorithm returned true
As you can see, this is exactly what the questioned asked
:"Implement your algorithm to see if a string is properly
parenthesized. It should return true if it is and false if it is not."
Hope this helps.
If you have any other queries, just ask!
Happy Coding!
Theta-ga
:-)
|