🗂️
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
  • Procedure :
  • C Program :

Was this helpful?

  1. Stack

Evaluation of Postfix Expression

Procedure to evaluate a postfix expression using stack.

Procedure :

  • Iterate through each character of the given postfix expression.

  • If the character is an operand (0 to 9), push it to stack after properly typecasting into integer value from char value by subtracting 48.

  • If the character is an operator, pop 2 items from the stack. The element to the popped will be num2 and the next element will be num1.

  • Evaluate the operation using switch case.

  • Push the result of evaluation to stack.

  • The result will be at stack top after the whole process.

C Program :

int isOperand(char);

// Main function
int main() {

    char postfix[30];
    int stack[30];
    int num1, num2, res, i, top=-1;

    printf("Enter a postfix expression : \n");
    scanf("%s",postfix);

    // Iterate through each character of postfix expression
    for(i=0;postfix[i]!='\0';i++) {

        // If character is an operand, push it to stack
        if(isOperand(postfix[i])==1) {
            top++;
            stack[top] = (int)(postfix[i]-48);
        }

        // If character is an operator, pop 2 items from stack and perform operation
        else {
            
            // Popping two items from stack
            num2 = stack[top];
            top--;
            num1 = stack[top];
            top--;

            // Switch case to choose operation
            switch(postfix[i]) {

                case '+' : res = num1+num2;
                break;

                case '-' : res = num1-num2;
                break;

                case '*' : res = num1*num2;
                break;

                case '/' : res= num1/num2;
                break;

            }

            // Add result to stack top
            top++;
            stack[top] = res;

        }
    }

    // Getting result
    printf("Answer of expression : %d\n",stack[top]);
    return 0;

}

// isOperand function
int isOperand(char x) {

    if(x>='0' && x<='9') {
        return 1;
    }
    else {
        return 0;
    }

}

Contributed by Nitin Ranganath

PreviousInfix to PostfixNextQueue using Array

Last updated 4 years ago

Was this helpful?