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;}