AlgorithmThe task is to determine whether the grouping symbo
Algorithm----The task is to determine whether the grouping symbols--parentheses, brackets, curly braces, etc.--in an arithmetic expression, such as [(9+2)*9]), match each other. Ignoring all digits and operands, push the left-grouping symbols on the stack. When a right-grouping symbol is encountered, pop the stack. Somehow compare the two characters. If they don\'t match, return false. Somehow, determine the end of this process and return true if the expression has passed all the matches.
Requirements
Define a Stack ADT named CharStack.java, and use a linkedList to implement the ADT. The Stack ADT should have only one data member: LLNode top; (LLNode is provided).
**************************
public class LLNode{
private LLNode link;
private char data;
public LLNode(LLNode link, char data){
setLink(link);
setData(data); }
public LLNode getLink(){
return link; }
public char getData(){
return data;
}
public void setLink(LLNode link){
this.link = link;
}
public void setData(char data){
this.data = data;
}
}******************************************************
Design a tester program named MatchGood.java, within this class, define a static method as specified in the following header to evaluate if a String has matching parentheses. If they don\'t match, return false. Somehow, determine the end of this process and return true if the expression has passed all the matches.
public static boolean checkParentheses(String s)
The String to be tested is passed to the program though command argument list.
Solution
import java.util.*;
public class CharStack {
public static boolean checkParentheses(String str) {
Stack<Character> stack = new Stack<Character>();
char first_char;
for(int i=0; i < str.length(); i++) {
first_char = str.charAt(i);
if(first_char == \'(\')
stack.push(first_char);
else if(first_char == \'{\')
stack.push(first_char);
else if(first_char == \'[\')
stack.push(first_char);
else if(first_char == \')\')
if(stack.empty())
return false;
else if(stack.peek() == \'(\')
stack.pop();
else
return false;
else if(first_char == \'}\')
if(stack.empty())
return false;
else if(stack.peek() == \'{\')
stack.pop();
else
return false;
else if(first_char == \']\')
if(stack.empty())
return false;
else if(stack.peek() == \'[\')
stack.pop();
else
return false;
}
return stack.empty();
}
public static void main(String[] args) {
CharStack ob=new CharStack();
String s=args[0];
boolean result=ob.checkParentheses(s);
System.out.println(\"Result :\"+result);
}
}
![Algorithm----The task is to determine whether the grouping symbols--parentheses, brackets, curly braces, etc.--in an arithmetic expression, such as [(9+2)*9]), Algorithm----The task is to determine whether the grouping symbols--parentheses, brackets, curly braces, etc.--in an arithmetic expression, such as [(9+2)*9]),](/WebImages/20/algorithmthe-task-is-to-determine-whether-the-grouping-symbo-1045211-1761543561-0.webp)
![Algorithm----The task is to determine whether the grouping symbols--parentheses, brackets, curly braces, etc.--in an arithmetic expression, such as [(9+2)*9]), Algorithm----The task is to determine whether the grouping symbols--parentheses, brackets, curly braces, etc.--in an arithmetic expression, such as [(9+2)*9]),](/WebImages/20/algorithmthe-task-is-to-determine-whether-the-grouping-symbo-1045211-1761543561-1.webp)