Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

Application Of Stack Balancing the Paranthesis Aim: To write a c++ program to implement balancing the paranthesis using stack

Algorithm: Start the program Create a header the implementation for the pop and member function Create a source fill by including the above header file Create empty stack Read an expression character by character by character If the character in the open paranthesis pop an items. At the end if the stack is empty the expression is valid. If it is not empty, it is an invalid expression 3 Stop the program

/*Balancing the paranthesis*/ Program: #includestack.h void main(void) { char item; char ans,bracket[10]; int I; st,top=-1; clrscr(); printf(\n\tEnter the expression & put s at the end n); scanf(%s,bracket); i=0; if(bracket[i]==/) printf(\n The expression is invalid); else { do { i++; while(bracket[i]==c) { push(bracket[i]); i++; }

while(bracket[i]); i++; } while(bracket[i]==/) { item pop(); i++; } } while(bracket[i]!=$); if(!stempty()) printf(\nThe expressin has well formed paranthesis); else printf(\n The expression is invalid); getch(); } /*Header file(stack.H)*/ #include<stdio.h> #include<conio.h> #include<stdio.h> #dfine size 5 struct stack { char s[size]; int top; } st; void push(char item) { st.top++i; st.s[st.top]-item; } int stempty() { if(st.top1) return 1; else return(); } char pop() } char item; item-st.s[st.top];

st.top--; return(item); }

Result: Thus the program is written, executed and output is verified.

Output: Enter the expression and put $ at the end ((a+b)*c)$ The expression is valid Enter the expression and put $ at the end (((a+b)*c)$ The expression is invalid.

You might also like