Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Algorithm----The task is to determine whether the grouping symbols--parentheses,

ID: 3927010 • Letter: A

Question

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.

Explanation / Answer

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);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote