Various applications of stack are 1. Expression conversion,2. Expression evaluation,3. Parsing well formed parenthesis,4. Decimal to binary conversion,5. Reversing a string,6. Storing function calls
Applications of Stack
Various
applications of stack are
1.
Expression conversion
2.
Expression evaluation
3.
Parsing well formed parenthesis
4.
Decimal to binary conversion
5.
Reversing a string
6.
Storing function calls
Ex. 2.5.1: Write a C program that checks if
expression is correctly parenthesized using stack. AU May-15, Marks 12
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define
size 5
/* stack
structure*/
struct
stack {
char
s[size];
int top;
}st;
void
push(char item)
{
st.top++;
st.s[st.top]
= item;
}
int
stempty()
{
if(st.top
==-1)
return
1;
else
return
0;
}
char
pop()
{
char
item;
item=st.s[st.top];
st.top--;
return(item);
}
void
main(void)
{
char
item;
char
ans,bracket[10];
int i;
st.top=-1;
clrscr();
printf("\n\t\t
Enter The expression and put $ at the end");
scanf("%s",
bracket);
i=0;
if(bracket[i]==')')
printf("\n
The expression is invalid");
else
{
do
{
while(bracket[i]=='(')
{
push(bracket[i]);
i++;
}
while(bracket[i]==')')
{
item=pop();
i++;
}
}
while(bracket[i]!='$');
if(!stempty())
printf("\n
The expression is invalid");
else
printf("\n
The expression has well formed parenthesis");
}
getch();
}
Output
Enter
The expression and put $ at the end(()())$
The
expression has well formed parenthesis
Enter
The expression and put $ at the end())$
The
expression is invalid
Data Structure: Unit II (a): Stacks : Tag: : ADT Data Structure - Applications of Stack
Data Structure
CS3301 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation