C Programming and Data Structures: Unit III: b. Linear Data Structures Stacks and Queues

Applications of Stack

with Example C programs

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  

AU: May-15, Marks 12

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. 4.4.1: Write a C program that checks if expression is correctly parenthesized using stack.AU: May-15, Marks

#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; 80

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

 

C Programming and Data Structures: Unit III: b. Linear Data Structures Stacks and Queues : Tag: : with Example C programs - Applications of Stack