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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!