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

DSA Assignment Infix Postfix Prefix

The document contains a C program that converts infix expressions to postfix and prefix notations using a stack data structure. It includes a header file defining stack operations and a main file implementing the conversion logic. The program processes a sample infix expression and outputs the corresponding postfix and prefix forms.

Uploaded by

samkaushik108
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)
5 views3 pages

DSA Assignment Infix Postfix Prefix

The document contains a C program that converts infix expressions to postfix and prefix notations using a stack data structure. It includes a header file defining stack operations and a main file implementing the conversion logic. The program processes a sample infix expression and outputs the corresponding postfix and prefix forms.

Uploaded by

samkaushik108
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

DSA Assignment-2025UCA1927 (infix to

prefix and postfix)

Header File (stackfunc.h):-


#ifndef STACKFUNC_H

#define STACKFUNC_H

#define MAX 50

char stack[MAX];

int top;

void push(char x) {

stack[++top] = x;

char pop() {

return stack[top--];

char peek() {

return stack[top];

int empty() {

return top == -1;

#endif

Main File(program.c):-
#include <stdio.h>

#include "stackfunc.h"

char infix[] = "(a+b-c)*(d/f+g-b)-m";


char postfix[50], prefix[50];

int priority(char op) {

if (op == '+' || op == '-') return 1;

if (op == '*' || op == '/') return 2;

return 0;

void infixToPostfix(char infix[], char postfix[]) {

int i = 0, k = 0;

char ch;

top = -1;

while ((ch = infix[i++]) != '\0') {

if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {

postfix[k++] = ch;

else if (ch == '(') {

push(ch);

else if (ch == ')') {

while (peek() != '(')

postfix[k++] = pop();

pop();

else {

while (!empty() && priority(peek()) >= priority(ch))

postfix[k++] = pop();

push(ch);

while (!empty())
postfix[k++] = pop();

postfix[k] = '\0';

void infixToPrefix(char infix[], char prefix[]) {

char temp[50], postfix[50];

int i, j = 0;

for (i = 0; infix[i] != '\0'; i++);

for (i = i - 1; i >= 0; i--) {

if (infix[i] == '(') temp[j++] = ')';

else if (infix[i] == ')') temp[j++] = '(';

else temp[j++] = infix[i];

temp[j] = '\0';

infixToPostfix(temp, postfix);

for (i = 0; postfix[i] != '\0'; i++);

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

prefix[j] = postfix[i - j - 1];

prefix[i] = '\0';

int main() {

infixToPostfix(infix, postfix);

infixToPrefix(infix, prefix);

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

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

printf("Prefix (Output) : %s\n", prefix);

return 0;}

You might also like