Explain the evaluation of expressions of stacks in C language



Stack is a linear data structure, that allows elements to be added and removed in a last-in, first-out(LIFO). The main operations are ?

  • Push
  • Pop
  • Display

The Push Operation

A stack adds a new item on top. A stack overflow occurs when the stack is full and cannot specify more items. Given below is an algorithm for Push() ?

Check for stack overflow.

if (top = = n-1)
printf("stack over flow");

Otherwise, insert an element into the stack.

top ++
a[top] = item

The Pop Operation

The pop() operation removes and returns the top items from the stack. To implement it, we need to retrieve the top item decrement the top pointer, and return the item. Given below is an algorithm for Pop() ?

Check for stack underflow.

if ( top = = -1)
printf( "stack under flow");

Otherwise, delete an element from the stack.

item = a[top]
top --

The Display Operation 

The display() function in the stack prints all the elements from top to bottom using a loop. Given below is an algorithm for Display () ?

if (top == -1)
printf ("stack is empty");

Otherwise, follow the below-mentioned algorithm.

for (i=0; i<top; i++)
printf ("%d", a[i]);

Types of Expressions

There are three types of expressions in C language on which the conversions and valuation can be carried out. They are explained below ?

  • Infix expression: Operator is in between the operands. For example, A+B

  • Prefix expression: Operator is before the operands. For example, +AB

  • Postfix expression: Operator is after the operands. For example, AB+

Evaluation of Postfix Expression

Algorithm to scan the input string from left to right. For each input symbol,

  • If it is a digit, push it onto the stack.

  • If it is an operator, pop out the top two elements from the stack, apply the operator to them, and then push the result back onto the stack.

  • If the input symbol is ?\0', clear the stack.

Example: Postfix Expression Evaluation in C

Following is the C program for an evaluation of postfix expression ?

#include<stdio.h>

int top = -1, stack[100];
main() {
   char a[50], ch;
   int i, op1, op2, res, x;
   void push(int);
   int pop();
   int eval(char, int, int);
   printf("enter a postfix expression:");
   gets(a);
   for (i = 0; a[i] != '\0'; i++) {
      ch = a[i];
      if (ch & gt; = '0' & amp; & amp; ch & lt; = '9')
         push('0');
      else {
         op2 = pop();
         op1 = pop();
         res = eval(ch, op1, op2);
         push(res);
      }
   }
   x = pop();
   printf("evaluated value = %d", x);
   getch();
}
void push(int n) {
   top++;
   stack[top] = n;
}
int pop() {
   int res;
   res = stack[top];
   top--;
   return res;
}
int eval(char ch, int op1, int op2) {
   switch (ch) {
   case '+':
      return (op1 + op2);
   case '-':
      return (op1 - op2);
   case '*':
      return (op1 * op2);
   case '/':
      return (op1 / op2);
   }
}

Output

When the above program is executed, it produces the following result ?

Run 1:
enter a postfix expression:45+
evaluated value = 9
Run 2:
enter a postfix expression: 3 5 2 * +
evaluated value = 13
Updated on: 2024-12-06T15:49:44+05:30

21K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements