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

DSA Infix To Postfix

The document contains a C program that converts an infix expression to a postfix expression using a stack data structure. It defines functions for stack operations, precedence checking, and the conversion process. The program prompts the user for an infix expression and outputs the corresponding postfix expression.

Uploaded by

Aaryan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

DSA Infix To Postfix

The document contains a C program that converts an infix expression to a postfix expression using a stack data structure. It defines functions for stack operations, precedence checking, and the conversion process. The program prompts the user for an infix expression and outputs the corresponding postfix expression.

Uploaded by

Aaryan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

NAME- ARYAN KUMAR

MIS – 112415033

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define MAX 100

typedef struct {
char arr[MAX];
int top;
} Stack;

void push(Stack *s, char c) {


if (s->top == MAX - 1) {
printf("Stack Overflow\n");
return;
}
s->arr[++s->top] = c;
}

char pop(Stack *s) {


if (s->top == -1) {
printf("Stack Underflow\n");
return -1;
}
return s->arr[s->top--];
}

char peek(Stack *s) {


if (s->top == -1)
return -1;
return s->arr[s->top];
}

int precedence(char c) {
switch (c) {
case '+':
case '-': return 1;
case '*':
case '/': return 2;
case '^': return 3;
default: return 0;
}
}

void ITP(char *infix, char *postfix) {


Stack s;
s.top = -1;
int i, j = 0;
for (i = 0; infix[i] != '\0'; i++) {
if (isalnum(infix[i])) {
postfix[j++] = infix[i];
} else if (infix[i] == '(') {
push(&s, infix[i]);
} else if (infix[i] == ')') {
while (s.top != -1 && peek(&s) != '(')
postfix[j++] = pop(&s);
pop(&s);
} else {
while (s.top != -1 && precedence(peek(&s)) >=
precedence(infix[i]))
postfix[j++] = pop(&s);
push(&s, infix[i]);
}
}
while (s.top != -1)
postfix[j++] = pop(&s);
postfix[j] = '\0';
}

int main() {
char infix[MAX], postfix[MAX];
printf("Enter an infix expression: ");
scanf("%s", infix);
ITP(infix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;
}

OUTPUT:-

Enter an infix expression: (A+B)*(C/(D+E)+F)

Postfix expression: AB+CDE+/F+*

You might also like