🗂️
Data Structures
  • Data Structures Manual
  • Arrays
    • Array ADT
    • Linear Search
    • Binary Search
    • Some More Basic Operations
    • Reversing an Array
    • Operations in a Sorted Array
    • Merging Two Arrays
    • Set Operations
    • Finding Missing Elements
    • Duplicates in an Array
    • Getting a Pair whose Sum = K
    • Finding Max & Min in Single Scan
  • Strings
    • Finding the Length of a String
    • Changing Cases in a String
    • Finding Number of Vowels, Consonants & Words
    • Reversing a String
    • Checking for Palindrome
    • Duplicates in a String
    • Checking if Strings are Anagrams
    • Permutations of a String
  • Singly Linked List
    • Displaying the Nodes
    • Counting the Nodes
    • Sum of all Nodes
    • Finding the Maximum Element
    • Searching in a Node
    • Inserting a Node
    • Inserting a Node in Sorted List
    • Deleting a Node
    • Checking if List is Sorted
    • Removing Duplicates from a List
    • Reversing a Linked List
    • Concatenating Two Lists
    • Detecting a Loop in Linked List
    • Merge Two Sorted Lists
    • Finding the Middle Node
  • Cirular Linked List
    • Displaying the Nodes
    • Inserting a Node
    • Deleting a Node
  • Doubly Linked List
    • Inserting a Node
    • Deleting a Node
    • Reversing a Doubly Linked List
    • Circular Doubly Linked List
  • Stack
    • Stack Using Array
    • Stack Using Linked List
    • Balancing Parenthesis
    • Infix to Postfix
    • Evaluation of Postfix Expression
  • Queue
    • Queue using Array
    • Queue using Linked List
    • Double Ended Queue
  • Binary Tree
    • Creating a Binary Tree using Queue
    • Recursive Tree Traversals
    • Iterative Tree Traversals
    • Level Order Traversal
    • Counting Nodes in a Binary Tree
    • Finding the Height of Tree
    • Finding Sum of All Nodes
  • Binary Search Tree
    • Searching in a BST
    • Inserting in a BST
    • Deleting in a BST
  • AVL Tree
    • Inserting in an AVL Tree
    • AVL Tree Rotations
    • Deleting in an AVL Tree
  • Heap
    • Inserting in a Heap
    • Deleting in a Heap
    • Heapify
Powered by GitBook
On this page

Was this helpful?

  1. Stack

Infix to Postfix

Program to convert an infix expression to a postfix expression.

C Program :

int priority(char);
int isOperand(char);

// Main function
int main() {

    // Required variables
    char infix[30], postfix[30], stack[30];
    int i;
    int j = -1;     // Used for postfix array
    int top = -1;   // Used for stack

    // Scanning the expression
    printf("Enter the infix expression : \n");
    scanf("%s", infix);

    // Scanning each character one by one
    for (i = 0; infix[i] != '\0'; i++) {

        // If character is an operand, add to postfix expression
        if (isOperand(infix[i]) == 1) {
            j++;
            postfix[j] = infix[i];
        }

        // If character is an operator
        
        // If stack is empty
        else if (top == -1) {
            top++;
            stack[top] = infix[i];
        }

        // If closing bracket is encountered, pop from stack until opening bracket is at stack top
        else if (infix[i] == ')') {
            while (stack[top] != '(') {
                j++;
                postfix[j] = stack[top];
                top--;
            }
            top--;
        }

        // If scanned operator has higher priority than stack top operator, push it to stack
        else if (priority(infix[i]) > priority(stack[top])) {
            top++;
            stack[top] = infix[i];
        }

        // If scanned operator has lower priority than stack top operator, pop from stack until stack top has lower priority
        else if (priority(stack[top]) >= priority(infix[i])) {
            while (priority(stack[top]) >= priority(infix[i]) && top != -1 && stack[top] != '(') {
                j++;
                postfix[j] = stack[top];
                top--;
            }
            top++;
            stack[top] = infix[i];
        }
    }

    // Pop all remaning operators from stack until stack is empty
    while (top != -1) {
        j++;
        postfix[j] = stack[top];
        top--;
    }

    // Set postfix[j+1] as \0 
    postfix[j + 1] = '\0';

    // Output the postfix expression
    puts(postfix);
    return 0;

}

// Priority function
int priority(char x) {

    if (x == '*' || x == '/') {
        return 3;
    }
    else if (x == '+' || x == '-') {
        return 2;
    }
    else if (x == '(' || x == ')') {
        return 1;
    }
    else {
        return 0;
    }

}

// isOperand function
int isOperand(char x) {

    if ((x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z') || (x >= 0 && x <= 9)) {
        return 1;
    }
    else {
        return 0;
    }

}

Contributed by Nitin Ranganath

PreviousBalancing ParenthesisNextEvaluation of Postfix Expression

Last updated 5 years ago

Was this helpful?