Skip to content

Create Balancing Brackets/Parenthesis using stack #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions Misc/BalancingBracketsUsingStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import java.util.Scanner;

public class BalancedParan
{
static class stack
{
int top=-1; //top refers to the top most value of the stack
char items[] = new char[100]; //Stack array

void push(char x)
{
if (top == 99)
{
System.out.println("Stack full");
}
else
{
items[++top] = x;
}
}

char pop()
{
if (top == -1)
{
System.out.println("Underflow error");
return '\0';
}
else
{
char element = items[top];
top--;
return element;
}
}

boolean isEmpty()
{
return (top == -1) ? true : false;
}
}

/* Returns true if character1 and character2
are matching left and right Parenthesis */
static boolean isMatchingPair(char character1, char character2)
{
if (character1 == '(' && character2 == ')')
return true;
else if (character1 == '{' && character2 == '}')
return true;
else if (character1 == '[' && character2 == ']')
return true;
else
return false;
}

/* Return true if expression has balanced
Parenthesis */
static boolean areParenthesisBalanced(char exp[])
{
/* Declare an empty character stack */
stack st=new stack();

/* Traverse the given expression to
check matching parenthesis */
for(int i=0;i<exp.length;i++)
{
/*If the exp[i] is a starting
parenthesis then push it*/
if (exp[i] == '{' || exp[i] == '(' || exp[i] == '[')
st.push(exp[i]);

/* If exp[i] is an ending parenthesis
then pop from stack and check if the
popped parenthesis is a matching pair*/
if (exp[i] == '}' || exp[i] == ')' || exp[i] == ']')
{

/* If we see an ending parenthesis without
a pair then return false*/
if (st.isEmpty())
{
return false;
}

/* Pop the top element from stack, if
it is not a pair parenthesis of character
then there is a mismatch. This happens for
expressions like {(}) */
else if ( !isMatchingPair(st.pop(), exp[i]) )
{
return false;
}
}

}

/* If there is something left in expression
then there is a starting parenthesis without
a closing parenthesis */

if (st.isEmpty())
return true; /*balanced*/
else
{ /*not balanced*/
return false;
}
}

/* UTILITY FUNCTIONS */
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
String brackets = scanner.nextLine(); // Input eg."{()}["

char[] exp = brackets.toCharArray();
if (areParenthesisBalanced(exp))
System.out.println("Balanced "); // If the Brackets are balanced
else
System.out.println("Not Balanced "); // If the Brackets are unbalanced
}

}
4 changes: 1 addition & 3 deletions Misc/TowerOfHanoiUsingRecursion.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import java.util.Scanner;

class TowerOfHanoi
public class TowerOfHanoi
{
public static void shift(int n, String startPole, String intermediatePole, String endPole)
{
if (n == 0) // if n becomes zero the program returns thus ending the loop.
{
return;
}


// Shift function is called in recursion for swapping the n-1 disc from the startPole to the intermediatePole
shift(n - 1, startPole, endPole, intermediatePole);
System.out.println("\nMove \"" + n + "\" from " + startPole + " --> " + endPole); // Result Printing
Expand Down