APPLICATIONS OF STACK CHECKING WELL FORMEDNESS OF
PARANTHESIS AIM:
To Write a program for checking
Well-Formed of the parenthesis using Linked List
ALGORITHM:
STEP 1: Start the program
STEP 2: Create the User defined Header file with the Name Stack.h
STEP 2: Include the Header file with the Main
program
STEP 3: Initialize the parameters
STEP 4: Get the Expression
STEP 5: Evaluate the Expression Using Balanced parameters
STEP 6: Stop the program execution.
PROGRAM 1:
Stack.h
#include<iostream.h>
#include<conio.h>
#define size 10
class STK_CLASS
{
private:
struct stack
{
char s[size];
int top;
public:
STK_CLASS();
void push(char item);
int stfull();
int stempty();
char pop();
};
STK_CLASS::STK_CLASS()
{
st.top=-1;
}
void STK_CLASS::push(char item)
{
st.top++;
st.s[st.top]=item;
}
int STK_CLASS::stempty()
{
if(st.top==-1)
return 1;
else
return 0;
}
int STK_CLASS::stfull()
{
if(st.top==size)
return 1;
else
return 0;
}
char STK_CLASS::pop()
{
char item;
item=st.s[st.top];
st.top--;
return(item);
}
PROGRAM2:
#include<iostream.h>
#include<conio.h> #include<stdlib.h> #include “stack.h”
#define size 10
void main(void)
{
char item;
char ans,bracket[10];
STK_CLASS obj;
int i;
clrscr();
cout<<”\n\t\t Program for stack application using separate header file”; cout<<”\n Enter the expression and put $ at the end “;
cin>>bracket;
i=0;
if(bracket[i]==’(‘)
cout<<”\n The expression is invalid”;
else
{
do
{
while(bracket[i]==’(‘)
{
obj.push(bracket[i]);
i++;
}
while(bracket[i]==’)’)
{
item=obj.pop();
i++;
}
}while(bracket[i]!=”$”);
if(obj.stempty())
cout<<”\n The expression is invalid”;
else
cout<<”\n The expression has well formed paranthesis”;
}
getch();
}
OUTPUT:
Program for stack application using separate header file.
Enter the expression and put $ at the end (()(()))$
The expression has well formed paranthesis .
0 Comments