coding (1)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

NAME:-SAYANI MITRA

ROLL:-35000120003
DEPARTMENT:-COMPUTER SCIENCE
TOPIC:- STACK
1.Conversion of infix expression to postfix expression using stack.
Ans:-
#include<stdio.h>
#include<ctype.h>
char stack[20];
int top=-1;
void push(char x)
{
stack[++top]=x;
}
char pop()
{
if(top==-1)
return -1;
else
return stack[top--];
}
int priority(char x)

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

}
int main()
{
char exp[20];
char *e,x;
printf("Enter the expression\n");
scanf("%s",exp);
e=exp;
while(*e!='\0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e=='(')
push(*e);
else if(*e==')')
{
while((x=pop())!='(')
printf("%c",x);

}
else
{
while(priority(stack[top])>=priority(*e))
printf("%c",pop());
push(*e);
}
e++;
}
while(top!=-1)
{
printf("%c",pop());
}
}

Output:-

2. Evaluate postfix expression using stack.


Ans:-
The Postfix notation is used to represent algebraic expressions.
The expressions written in postfix form are evaluated faster
compared to infix notation as parenthesis are not required in postfix.
We have discussed infix to postfix conversion. In this post,
evaluation of postfix expressions is discussed.
Following is algorithm for evaluation postfix expressions.
1) Create a stack to store operands (or values).
2) Scan the given expression and do the following for every
scanned element.
a)If the element is a number, push it into the stack
b) If the element is a operator, pop operands for the operator
from stack. Evaluate the operator and push the result back to
the stack
3) When the expression is ended, the number in the stack is
the final answer.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

struct Stack
{
int top;
unsigned capacity;
int* array;
};

struct Stack* createStack( unsigned capacity )


{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));

if (!stack) return NULL;

stack->top = -1;
stack->capacity = capacity;
stack->array = (int*) malloc(stack->capacity * sizeof(int));

if (!stack->array) return NULL;


return stack;
}

int isEmpty(struct Stack* stack)


{
return stack->top == -1 ;
}

char peek(struct Stack* stack)


{
return stack->array[stack->top];
}

char pop(struct Stack* stack)


{
if (!isEmpty(stack))
return stack->array[stack->top--] ;
return '$';
}

void push(struct Stack* stack, char op)


{
stack->array[++stack->top] = op;
}

int evaluatePostfix(char* exp)


{
struct Stack* stack = createStack(strlen(exp));
int i;

if (!stack) return -1;

for (i = 0; exp[i]; ++i)


{
if (isdigit(exp[i]))
push(stack, exp[i] - '0');

else
{
int val1 = pop(stack);
int val2 = pop(stack);
switch (exp[i])
{
case '+': push(stack, val2 + val1); break;
case '-': push(stack, val2 - val1); break;
case '*': push(stack, val2 * val1); break;
case '/': push(stack, val2/val1); break;
}
}
}
return pop(stack);
}

int main()
{
char exp[] = "475*+9-";
printf ("postfix evaluation: %d", evaluatePostfix(exp));
return 0;
}

Output:-

……………………………..X………………………………………

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy