0% found this document useful (0 votes)
55 views

Practical 4

1) The document provides code examples for solving problems related to data structures, including recognizing balanced strings, checking validity of expressions with multiple brackets, converting infix to postfix notation, and evaluating postfix expressions. 2) A program is provided to implement the Tower of Hanoi problem by recursively moving disks from the initial rod to the final rod using an auxiliary rod. 3) The document appears to be a lab manual for students, providing code examples and instructions to practice common data structures problems.

Uploaded by

Mr Banna
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Practical 4

1) The document provides code examples for solving problems related to data structures, including recognizing balanced strings, checking validity of expressions with multiple brackets, converting infix to postfix notation, and evaluating postfix expressions. 2) A program is provided to implement the Tower of Hanoi problem by recursively moving disks from the initial rod to the final rod using an auxiliary rod. 3) The document appears to be a lab manual for students, providing code examples and instructions to practice common data structures problems.

Uploaded by

Mr Banna
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

GANPAT UNIVERSITY

U.V. PATEL COLLEGE OF ENGINEERING

Lab Manual
2CEIT304: Data Structures

B.Tech Semester: III


Computer Engineering/ Information
Technology
Academic Year: 2021 (EVEN Sem.)

Name:Dhruv lakhani
Enrollement no.20012011055
Batch:-AB1
Branch:-CE

Practical:-4

1. Write a program to recognize the string with language L={wcwR / w takes


multiple occurrences of {a,b}}.
Input code:-

#include<stdio.h>

int main()

char expression[50];

int x=0, i=0;

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:-

2. Write a program to check the validity of expressions, which contains


multiple opening and closing brackets. (i.e., [{(a+b) * c} – d]).
Input code:-

#include<stdio.h>

int main()

char expression[50];

int x=0, i=0;


printf("\nEnter an expression:-");

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>

void push(char s[], int n, int *top, char x)

if (*top >= (n - 1))

printf("Stack overflow");

return;

*top = *top + 1;

s[*top] = x;

char pop(char s[], int *top)

if (*top == -1)

return 0;

*top = *top - 1;

return (s[*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;

void infix2postfix(char a[])

char s[80], c, pstfx[80];

int i, n, k = 0, top = 0;

n = strlen(a);

s[top] = '#';

for (i = 0; i < n; i++)

if (isopd(a[i]))

pstfx[k++] = a[i];

else if (isopr(a[i]) || a[i] == '(')

while (iprec(a[i]) <= sprec(s[top]))


pstfx[k++] = pop(s, &top);

push(s, n, &top, a[i]);

else if (a[i] == ')')

while ((c = pop(s, &top)) != '(')

pstfx[k++] = c;

while (s[top] != '#')

pstfx[k++] = pop(s, &top);

pstfx[k] = '\0';

printf("\ninfix :%s\n", a);

printf("postfix :%s", pstfx);

int chk_exp(char a[])

int i, n, top = -1;

char s[20], c;

n = strlen(a);

for (i = 0; i < (n - 1); i++)

if (isopd(a[i]) && isopd(a[i + 1]) || isopr(a[i]) && isopr(a[i + 1]) || isopr(a[i])


&& a[i + 1] == ')' || isopr(a[n - 1]))

return 0;

}
for (i = 0; i < n; i++)

if (a[i] == '(')

push(s, n, &top, a[i]);

else if (a[i] == ')')

c = pop(s, &top);

if (c == '~')

return 0;

if (top == -1)

return 1;

else

return 0;

void main()

char a[80];

printf("Enter expression :\n");

gets(a);

if (chk_exp(a))

infix2postfix(a);
}

else

printf("Invalid expression.");

getch();
}
Output:-

4. Write a program to evaluate the given postfix expression. 


Input code:-

#include <stdio.h>

#include <conio.h>

#include <string.h>

void push(int s[], int n, int *top, int x)

if (*top >= (n - 1))

printf("Stack overflow\n");

return;

*top = *top + 1;

s[*top] = x;

int pop(int s[], int *top)


{

if (*top == -1)

return 0;

*top = *top - 1;

return (s[*top + 1]);

int isopd(char c)

if (c >= '0' && c <= '9')

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;

void infix2postfix(char a[])

char s[80], c, pstfx[80];

int i, n, k = 0, top = 0;

n = strlen(a);

s[top] = '#';

for (i = 0; i < n; i++)

if (isopd(a[i]))

pstfx[k++] = a[i];

else if (isopr(a[i]) || a[i] == '(')

{
while (iprec(a[i]) <= sprec(s[top]))

pstfx[k++] = pop(s, &top);

push(s, n, &top, a[i]);

else if (a[i] == ')')

while ((c = pop(s, &top)) != '(')

pstfx[k++] = c;

while (s[top] != '#')

pstfx[k++] = pop(s, &top);

pstfx[k] = '\0';

strcpy(a, pstfx);

int chk_exp(char a[])

int i, n, top = -1;

char s[20], c;

n = strlen(a);

for (i = 0; i < (n - 1); i++)

if (isopd(a[i]) && isopd(a[i + 1]) || isopr(a[i]) && isopr(a[i + 1]) || isopr(a[i])


&& a[i + 1] == ')' || isopr(a[n - 1]))

return 0;

for (i = 0; i < n; i++)

{
if (a[i] == '(')

push(s, n, &top, a[i]);

else if (a[i] == ')')

c = pop(s, &top);

if (c == '~')

return 0;

if (top == -1)

return 1;

else

return 0;

int postfix_evaluator(char a[])

int s[80], top = -1, op1, op2, n, i, r;

n = strlen(a);

for (i = 0; i < n; i++)

if (isopd(a[i]))

push(s, n, &top, a[i] - '0');

else if (isopr(a[i]))

if (top <= 0)

printf("Invalid postfix\n");
return 1949;

op2 = pop(s, &top);

op1 = pop(s, &top);

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;

push(s, n, &top, r);

}
if (top == 0)

return s[top];

else

return 1949;

void evaluate(char a[])

int res;

if (chk_exp(a))

printf("Infix : %s\n", a);

infix2postfix(a);

printf("Postfix : %s\n", a);

res = postfix_evaluator(a);

printf("Evaluation : %d\n", res);

else

printf("Invalid Expression.\n");

void main()

char a[80];

printf("Enter expression:\n");

gets(a);

evaluate(a);
}
Output:-

5. Write a program for Tower of Hanoi.


Input code:-

#include <stdio.h>

void TOH(int n, char x, char y, char z)

if (n > 0)

TOH(n - 1, x, z, y);

printf("\n%c to %c", x, y);

TOH(n - 1, z, y, x);

int main()

int n;

printf("Enter a number you want for hanoi step: ");

scanf("%d", &n);

TOH(n, 'A', 'B', 'C');


}

Output:-

You might also like