Assignment 10.docx
Assignment 10.docx
Group D
ASSIGNMENT NO: 10
AIM: In any language program mostly syntax error occurs due to unbalancing delimiter such as
(),{},[]. Write C++ program using stack to check whether given expression is well
Parenthesized or not.
THEORY:-
int top=-1;
The constructor must intialize the stack top, so as to represent an empty stack, to a value that
represent the top of the stack. We cannot initialize it to one of the values in the range of 0 to n-1
because these are the indices of stack array.
2.Empty Stack:-
if(top==-1)
Sandip Foundation’s
Sandip Institute of Engineering and Management,
Mahiravani, Trimbak Road, Nashik- 422 213
then
return 1;
else
return 0;
3.Get_top(s):-
if(top==-1)
cout<<”Stack Underflow”;
else
return (stack(top));
4.push(i,s):-
Add the element ‘i’ on the stack ‘s’ and return the modified stack.
if(top==maxcapacity)
cout<<”Stack overflow”;
else
top++;
stack[top]=element;
5.pop(s):-
Remove the top most element from stack(s) and return the modified stack.
if(top==-1)
cout<<”Stack Empty”;
else
Sandip Foundation’s
Sandip Institute of Engineering and Management,
Mahiravani, Trimbak Road, Nashik- 422 213
return (stack(top));
top--;
For example:-
Z-((X*(X+Y)/5-2)+Y)/3)
Expression such as ((X+Y) or (X+Y)) expression such as (X+Y)) (A-B) or (X-Y) breaks condition
(ii). To solve this problem let us define parathesis count at particular point in an expression as no. of
left parenthesis number of right parathesis that have been encontered in the left to right scanning of
expression at that particular point.
The two conditions that must hoold if the parathesis in an expression from pattern as below:-
A stack may also be used to keep track of parenthesis count. Whenever a left parenthesis is
encountered it is pushed in stack and whenever a right parathesis is encountered the stack is
examine. If the stack is empty then string is declared to be invalid. When end of string is reached the
stack must be empty otherwise string is declared to be invalid.
TEST CASES:-
CONCLUSION:-
The basic concept of stack operation push and pop able to check whether an expression is well
parenthesized or not.