Practical 4
Practical 4
Lab Manual
2CEIT304: Data Structures
Name:Dhruv lakhani
Enrollement no.20012011055
Batch:-AB1
Branch:-CE
Practical:-4
#include<stdio.h>
int main()
char expression[50];
printf("\nEnter an expression:-");
scanf("%s", expression);
while(expression[i]!= '\0')
if(expression[i]=='{')
x++;
else if(expression[i]=='}')
x--;
if(x<0)
break;
i++;
if(x==0)
printf("Expression is balanced");
else
printf("Expression is unbalanced");
return 0;
}
Output:-
#include<stdio.h>
int main()
char expression[50];
scanf("%s", expression);
while(expression[i]!= '\0')
if(expression[i]=='(')
x++;
else if(expression[i]==')')
x--;
if(x<0)
break;
if(expression[i]=='{')
x++;
else if(expression[i]=='}')
x--;
if(x<0)
break;
if(expression[i]=='[')
{
x++;
else if(expression[i]==']')
x--;
if(x<0)
break;
i++;
if(x==0)
printf("Expression is balanced");
else
printf("Expression is unbalanced");
return 0;
}
Output:-
3. Write a program to convert unparenthized and parenthized infix expression
to postfix.
Input code:-
#include <stdio.h>
#include <conio.h>
#include <string.h>
printf("Stack overflow");
return;
*top = *top + 1;
s[*top] = x;
if (*top == -1)
return 0;
*top = *top - 1;
}
int iprec(char c)
switch (c)
case '+':
case '-':
return 2;
case '*':
case '/':
case '%':
return 4;
case '(':
return 8;
case '#':
return 0;
default:
return -1;
int sprec(char c)
switch (c)
case '+':
case '-':
return 2;
case '*':
case '/':
case '%':
return 4;
case '(':
return 1;
case '#':
return 0;
default:
return -1;
int isopd(char c)
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
return 1;
return 0;
int isopr(char c)
switch (c)
{
case '+':
case '-':
case '*':
case '/':
case '%':
return 1;
return 0;
int i, n, k = 0, top = 0;
n = strlen(a);
s[top] = '#';
if (isopd(a[i]))
pstfx[k++] = a[i];
pstfx[k++] = c;
pstfx[k] = '\0';
char s[20], c;
n = strlen(a);
return 0;
}
for (i = 0; i < n; i++)
if (a[i] == '(')
c = pop(s, &top);
if (c == '~')
return 0;
if (top == -1)
return 1;
else
return 0;
void main()
char a[80];
gets(a);
if (chk_exp(a))
infix2postfix(a);
}
else
printf("Invalid expression.");
getch();
}
Output:-
#include <stdio.h>
#include <conio.h>
#include <string.h>
printf("Stack overflow\n");
return;
*top = *top + 1;
s[*top] = x;
if (*top == -1)
return 0;
*top = *top - 1;
int isopd(char c)
return 1;
return 0;
int isopr(char c)
switch (c)
case '+':
case '-':
case '*':
case '/':
case '%':
return 1;
return 0;
}
int iprec(char c)
switch (c)
case '+':
case '-':
return 2;
case '*':
case '/':
case '%':
return 4;
case '(':
return 8;
case '#':
return 0;
default:
return -1;
int sprec(char c)
switch (c)
case '+':
case '-':
return 2;
case '*':
case '/':
case '%':
return 4;
case '(':
return 1;
case '#':
return 0;
default:
return -1;
int i, n, k = 0, top = 0;
n = strlen(a);
s[top] = '#';
if (isopd(a[i]))
pstfx[k++] = a[i];
{
while (iprec(a[i]) <= sprec(s[top]))
pstfx[k++] = c;
pstfx[k] = '\0';
strcpy(a, pstfx);
char s[20], c;
n = strlen(a);
return 0;
{
if (a[i] == '(')
c = pop(s, &top);
if (c == '~')
return 0;
if (top == -1)
return 1;
else
return 0;
n = strlen(a);
if (isopd(a[i]))
else if (isopr(a[i]))
if (top <= 0)
printf("Invalid postfix\n");
return 1949;
switch (a[i])
case '+':
r = op1 + op2;
break;
case '-':
r = op1 - op2;
break;
case '*':
r = op1 * op2;
break;
case '/':
r = op1 / op2;
break;
case '%':
r = op1 % op2;
break;
}
if (top == 0)
return s[top];
else
return 1949;
int res;
if (chk_exp(a))
infix2postfix(a);
res = postfix_evaluator(a);
else
printf("Invalid Expression.\n");
void main()
char a[80];
printf("Enter expression:\n");
gets(a);
evaluate(a);
}
Output:-
#include <stdio.h>
if (n > 0)
TOH(n - 1, x, z, y);
TOH(n - 1, z, y, x);
int main()
int n;
scanf("%d", &n);
Output:-