Skip to content

Commit 51b1611

Browse files
Checking nested parentheses
1 parent bb7d627 commit 51b1611

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed

stack/parentheses/Demo.java

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package parentheses;
7+
import java.util.Scanner;
8+
9+
public class Demo
10+
{
11+
public static void main(String[] args)
12+
{
13+
String expression;
14+
15+
Scanner scan = new Scanner(System.in);
16+
17+
System.out.print("Enter an expression with parentheses : ");
18+
expression = scan.nextLine();
19+
20+
if(isValid(expression))
21+
System.out.println("Valid expression");
22+
else
23+
System.out.println("Invalid expression");
24+
scan.close();
25+
}
26+
27+
28+
public static boolean isValid(String expr)
29+
{
30+
StackA st = new StackA();
31+
32+
char ch;
33+
for(int i=0; i<expr.length(); i++)
34+
{
35+
if(expr.charAt(i)=='(' || expr.charAt(i)=='{' || expr.charAt(i)=='[')
36+
st.push(expr.charAt(i));
37+
38+
if(expr.charAt(i)==')' || expr.charAt(i)=='}' || expr.charAt(i)==']')
39+
if(st.isEmpty())
40+
{
41+
System.out.println("Right parentheses are more than left parentheses");
42+
return false;
43+
}
44+
else
45+
{
46+
ch=st.pop();
47+
if(!matchParentheses(ch,expr.charAt(i)))
48+
{
49+
System.out.println("Mismatched parentheses are : ");
50+
System.out.println(ch + " and " + expr.charAt(i));
51+
return false;
52+
}
53+
}
54+
}
55+
56+
if(st.isEmpty())
57+
{
58+
System.out.println("Balanced Parentheses");
59+
return true;
60+
}
61+
else
62+
{
63+
System.out.println("Left parentheses are more than right parentheses");
64+
return false;
65+
}
66+
67+
}
68+
69+
public static boolean matchParentheses(char leftPar,char rightPar)
70+
{
71+
if(leftPar=='[' && rightPar==']')
72+
return true;
73+
if(leftPar=='{' && rightPar=='}')
74+
return true;
75+
if(leftPar=='(' && rightPar==')')
76+
return true;
77+
return false;
78+
}
79+
}

stack/parentheses/StackA.java

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package parentheses;
7+
8+
import java.util.EmptyStackException;
9+
10+
public class StackA
11+
{
12+
private char[] stackArray;
13+
private int top;
14+
15+
public StackA()
16+
{
17+
stackArray = new char[10];
18+
top = -1;
19+
}
20+
21+
public StackA(int maxSize)
22+
{
23+
stackArray = new char[maxSize];
24+
top=-1;
25+
}
26+
public int size()
27+
{
28+
return top+1;
29+
}
30+
31+
public boolean isEmpty()
32+
{
33+
return (top==-1);
34+
}
35+
36+
public boolean isFull()
37+
{
38+
return (top==stackArray.length-1);
39+
}
40+
41+
public void push(char x)
42+
{
43+
if(isFull())
44+
{
45+
System.out.println("Stack Overflow\n");
46+
return;
47+
}
48+
top=top+1;
49+
stackArray[top]=x;
50+
}
51+
52+
public char pop()
53+
{
54+
char x;
55+
if(isEmpty())
56+
{
57+
System.out.println("Stack Underflow\n");
58+
throw new EmptyStackException();
59+
}
60+
x=stackArray[top];
61+
top=top-1;
62+
return x;
63+
}
64+
65+
public char peek()
66+
{
67+
if(isEmpty())
68+
{
69+
System.out.println("Stack Underflow\n");
70+
throw new EmptyStackException();
71+
}
72+
return stackArray[top];
73+
}
74+
75+
76+
public void display()
77+
{
78+
int i;
79+
80+
System.out.println("top= " + top);
81+
82+
if(isEmpty())
83+
{
84+
System.out.println("Stack is empty");
85+
return;
86+
}
87+
System.out.println("Stack is : ");
88+
for(i=top; i>=0; i--)
89+
System.out.println(stackArray[i] + " ");
90+
System.out.println();
91+
}
92+
93+
94+
}

0 commit comments

Comments
 (0)