0% found this document useful (0 votes)
3 views2 pages

Dsa Lab 2.infix

The document contains a C program that converts infix expressions to postfix notation using a stack. It defines functions for stack operations, checking operands, and determining operator precedence. The main function demonstrates the conversion with the expression '((a+(b*c))-d)'.
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)
3 views2 pages

Dsa Lab 2.infix

The document contains a C program that converts infix expressions to postfix notation using a stack. It defines functions for stack operations, checking operands, and determining operator precedence. The main function demonstrates the conversion with the expression '((a+(b*c))-d)'.
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/ 2

Infix to postfix:

#include<limits.h>
#include<stdio.h>
#include<stdlib.h>
#define MAX 20

char stk[20];
int top = -1;

int isEmpty ()
{
return top == -1;
}
int isFull ()
{
return top == MAX - 1;
}
char peek ()
{
return stk[top];
}
char pop ()
{
if (isEmpty ())
return -1;

char ch = stk[top];
top--;
return (ch);
}
void push (char oper)
{
if (isFull ())
printf ("Stack Full!!!!");

else
{
top++;
stk[top] = oper;
}
}
int checkIfOperand (char ch)
{
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}
int precedence (char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;

case '*':
case '/':
return 2;

case '^':
return 3;
}
return -1;
}
int covertInfixToPostfix (char *expression)
{
int i, j;

for (i = 0, j = -1; expression[i]; ++i)


{
if (checkIfOperand (expression[i]))
expression[++j] = expression[i];
else if (expression[i] == '(')
push (expression[i]);
else if (expression[i] == ')')
{
while (!isEmpty () && peek () != '(')
expression[++j] = pop ();
if (!isEmpty () && peek () != '(')
return -1;
else
pop ();
}
else
{
while (!isEmpty () && precedence (expression[i]) <= precedence (peek ()))
expression[++j] = pop ();
push (expression[i]);
}

}
while (!isEmpty ())
expression[++j] = pop ();

expression[++j] = '\0';
printf ("%s", expression);
}

int main ()
{
char expression[] = "((a+(b*c))-d)";
covertInfixToPostfix (expression);
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy