Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Check for Balanced Parentheses in an expression

Given a parenthesis ,check parentheses in an expression are balanced or not. few examples are given.

Example

What a logic?

Step 1: If any open Parenthesis are find then insert that parentheses to stack.

Step 2: if get close parenthesis then compared to stack Top element ASCII value are open expression. That means close parentheses are valid.then perform pop operation on stack.if not valid that means parentheses are not balanced.

View pointers and code execution process.

Stack Areamainroot(pointer)balancei (int) =8root(pointer)status (int) =1temp(pointer) Heap Areaarray (char) [0] [ [1] | [2] ] [3] [ [4] ] [5] { [6] { [7] | [8] } [9] } [10] ( [11] ( [12] 9 [13] ) [14] ) [15] \0struct Stackdata (char)= {next (pointer)= NULLstruct Stackdata (char)= {next (pointer)

Try it Yourself

Stack Areamainroot(pointer)balancei (int) =5root(pointer)status (int) =1temp(pointer)poptemp(pointer) Heap Areaarray (char) [0] [ [1] ] [2] { [3] { [4] A [5] } [6] } [7] ( [8] ( [9] ) [10] ) [11] [ [12] [ [13] [ [14] { [15] ] [16] } [17] \0struct Stackdata (char)= {next (pointer)= NULL

Try it Yourself

C program to check balanced parentheses in an expression. time complexity O(n).


Output

Spread the post

Recommended Posts: