Professional Documents
Culture Documents
Application of Stack
Application of 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); }
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.