Question
I need some help in examining the following data structure method explain what it does; write the pre and post conditions. public static boolean balanced(String
I need some help in examining the following data structure method explain what it does; write the pre and post conditions.
public static boolean balanced(String p)
{
final char LEFT_PAREN = '(';
final char RIGHT_PAREN = ')'
final char LEFT_BRACKET = '{';
final char RIGHT_BRACKET = '}';
CharStack s = new CharStack( );
int i;
int length = p.length( );
char next;
for (i = 0; i < length; i++)
{
next = (char) p.charAt(i);
switch (next)
{
case LEFT_PAREN:
case LEFT_BRACKET:
s.push(next);
break;
case RIGHT_PAREN:
if (s.is_empty( ) || s.pop( ) != LEFT_PAREN)
return false;
break;
case RIGHT_BRACKET:
if (s.is_empty( ) || s.pop( ) != LEFT_BRACKET)
return false;
break;
default:
throw new IllegalArgumentException
("Illegal character: " + next);
}
}
return s.isEmpty( );
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started