0% found this document useful (0 votes)
15 views3 pages

Dsa Lab 2

Data structures and algorithms lab experiment 2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Dsa Lab 2

Data structures and algorithms lab experiment 2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment-2

// main.c
// InfixToPostfix

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100

char stack[MAX];
char infix[MAX], postfix[MAX];
int top = -1;

void push(char);
char pop();
int isEmpty();
void inToPost();
void print();
int precedence(char);

int main()
{
printf("Enter the infix expression: ");
gets(infix);
inToPost();
print();
return 0;
}

void inToPost()
{
int i, j = 0;
char symbol, next;
for (i = 0; i < strlen(infix); i++)
{
symbol = infix[i];
switch (symbol)
{
case '(':
push(symbol);
break;
case ')':
while ((next = pop()) != '(')
postfix[j++] = next;
break;
case '+':
case '-':
case '*':
case '/':
case '^':
while (!isEmpty() && precedence(stack[top]) >=
precedence(symbol))
postfix[j++] = pop();
push(symbol);
break;
default:
postfix[j++] = symbol;
}
}

while (!isEmpty())
postfix[j++] = pop();
postfix[j] = '\0';
}

int precedence(char symbol)


{
switch (symbol)
{
// Higher value means higher precedence
case '^':
return 3;
case '/':
case '*':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
}
}

void print()
{
int i = 0;
printf("The equivalent postfix expression is: ");
while (postfix[i])
{
printf("%c", postfix[i++]);
}
printf("\n");
}

void push(char c)
{
if (top == MAX - 1)
{
printf("Stack Overflow\n");
return;
}
top++;
stack[top] = c;
}

char pop()
{
char c;
if (top == -1)
{
printf("Stack Underflow\n");
exit(1);
}
c = stack[top];
top = top - 1;
return c;
}

int isEmpty()
{
if (top == -1)
return 1;
else
return 0;
}

————————————————————————-……………………………………………————————————————————————

Following questions to be answered in lab record:

1. Why is postfix notation (also known as Reverse Polish Notation)


preferred in stack-based calculations?
2. Given an expression with multiple nested parentheses, how does
the algorithm ensure that the operations inside the deepest
parentheses are handled first?
3. How would you modify the algorithm to handle unary operators
(such as unary minus)?
4. How would you analyze and resolve ambiguity in operator
precedence during infix to postfix conversion?

You might also like