Answer the following Java programming question about using stacks to solve a problem. Balance...

Question:

Answer the following Java programming question about using stacks to solve a problem.

Balance Expressions (applying a stack to solve a problem)

Consider an expression that contains grouping symbols. The grouping symbols are parentheses, { }, and [ ]. Expressions are considered to be balanced if their grouping symbols follow the following rules:

• Each symbol must be followed by a right symbol with some data in between (you can't have an empty pair like [ ].)

• If pairs are nested, one pair must be completely nested within another.

Here is an example of a balanced expression:

abc{de(fg){ijk}{l{m[n]}}o[p]}qr

Here is an example where the grouping symbols are not balanced:

abc{(def}}{ghij{kl}m]

Write a method with signature boolean isBalanced(String s) that returns true if the grouping symbols within the string are properly balanced. Use a stack in your solution. You may assume that each symbol is just one character and that there are no spaces, as in the examples above.

Stack:

The stack is a very known data structure. It is work on LIFO (last in first out)and FILO (first in last out)principle. In any programme, the stack will do operations with push and pop means to enter an element to stack and pop means removes the recent element.

Answer and Explanation: 1

Become a Study.com member to unlock this answer!

View this answer

// Code starts here

public class CheckExpression
{
static class stack
{
int top=-1;
char items[] = new char[100];
void push(char x)
{
if (top == 99)
{
System...

See full answer below.


Learn more about this topic:

Loading...
Methods in Java: Definition & Example

from

Chapter 6 / Lesson 4
14K

A method in a Java program provides a set of instructions to be executed. Explore the different components of a method, such as its parameters and ability to return a value, and learn how to create a method.


Related to this Question

Explore our homework questions and answers library