21. Stack 2
18 Jun 2020 | Algorithm
Infix to postfix Conversion
struct Node
{
char data;
struct Node* next;
}*top=NULL;
void push(char x)
{
struct Node* t;
t = new Node;
if(t=NULL)
{
printf("stack is full \n");
}
else
{
t->data = x;
t->next = top;
top = t;
}
}
int isOperand(char x)
{
if(x=='+' || x=='-' || x=='*' || x=='/')
{
return 0;
}
else
return 1;
}
char* IntoPost (char* infix)
{
int i=0, j=0;
char* postfix;
int len=strlen(infix);
postfix = new char[len+2]
while(infix[i]!='\0')
{
if(isOperand(infix[i]))
{
postfix[j++] = infix[i++];
}
else
{
if(pre(infix[i])>pre(top->data))
{
push(infix[i++]);
}
else
{
postfix[j++] = pop();
}
}
}
while(top!=NULL)
{
postfix[j++]=pop();
}
postfix[j] = '\0';
return postfix;
}
char pop()
{
struct Node* t;
char x=1;
if (top==NULL)
{
printf("Stack is Empthy")
}
else
{
t= top;
top = top->next;
x = t->data;
free(t);
}
return x;
}
int pre(char x)
{
if(x=='+' || x=='-')
retrun 1;
else if(x=='*' || x=='/')
return 2;
return 0;
}
int main()
{
char* infix="a+b*c-d/e";
push('#');
char* postfix = InToPost(infix);
printf("%s", postfix);
return 0;
}
Infix to postfix with Associativity and parenthesis
- Associativity
- Unary(1진법) Operation
#include <iostream>
#include<cstring>
#include <stack>
using namespace std;
int isOperand(char x)
{
if (x == '+' || x == '-' || x == '*' || x == '/' ||
x == '^' || x == '(' || x == ')'){
return 0;
}
return 1;
}
int outPrecedence(char x)
{
if (x == '+' || x == '-'){
return 2;
} else if (x == '*' || x == '/'){
return 4;
} else if (x == '^'){
return 5;
} else if (x == '('){
return 0;
}
return -1;
}
int inPrecedence(char x){
if (x == '+' || x == '-'){
return 2;
} else if (x == '*' || x == '/'){
return 4;
} else if (x == '^'){
return 5;
} else if (x == '('){
return 0;
}
return -1;
}
char* convert(char* infix)
{
char* postfix = new char[strlen(infix)+1];
stack<char> stk;
int i = 0;
int j = 0;
while(infix[i]!='\0')
{
if(isOperand(infix[i]))
{
postfix[j++] = infix[i++];
}
else
{
if(stk.empty() || outPrecedence(infix[i])>inPrecedence(stk.top()))
{
stk.push(infix[i++]);
}
else if (outPrecedence(infix[i]) == inPrecedence(stk.top()))
{
stk.pop();
}
else
{
postfix[j++] = stk.top();
stk.pop();
}
}
}
while(!stk.empty()&&stk.top()!=')')
{
postfix[j++] = stk.top();
stk.pop();
}
postfix[j] = '\0';
return postfix;
}
int main()
{
char infix[] = "((a+b)* c)-d^e^f";
cout << convert(infix) << endl;
return 0;
}
Evaluation of Post fix
#include <iostream>
#include<cstring>
#include <stack>
using namespace std;
int isOperand(char x)
{
if(x == '+' || x=='-'|| x=='*'|| x=='/')
{
return 0;
}
return 1;
}
int operation(char op, int x, int y)
{
if (op == '+'){
return x + y;
} else if (op == '-'){
return x - y;
} else if (op == '*'){
return x * y;
} else if (op == '/'){
return x / y;
}
}
int Evaluate(char* postfix)
{
stack<char> stk;
int x, y, result;
for(int i =0; postfix[i]!='\0';i++)
{
if(isOperand(postfix[i]))
{
// int typecast would not work because of char so subtract '0'
stk.push(postfix[i]-'0');
}
else
{
y=stk.top();
stk.pop();
x=stk.top();
stk.pop();
result = operation(postfix[i], x,y);
stk.push(result);
}
}
result = stk.top();
stk.pop();
return result;
}
int main()
{
char postfix[] = "35*62/+4-";
cout << Evaluate(postfix) << endl;
cout << postfixEvaluate(postfix) << endl
}
Quiz
Infix to postfix Conversion
struct Node
{
char data;
struct Node* next;
}*top=NULL;
void push(char x)
{
struct Node* t;
t = new Node;
if(t=NULL)
{
printf("stack is full \n");
}
else
{
t->data = x;
t->next = top;
top = t;
}
}
int isOperand(char x)
{
if(x=='+' || x=='-' || x=='*' || x=='/')
{
return 0;
}
else
return 1;
}
char* IntoPost (char* infix)
{
int i=0, j=0;
char* postfix;
int len=strlen(infix);
postfix = new char[len+2]
while(infix[i]!='\0')
{
if(isOperand(infix[i]))
{
postfix[j++] = infix[i++];
}
else
{
if(pre(infix[i])>pre(top->data))
{
push(infix[i++]);
}
else
{
postfix[j++] = pop();
}
}
}
while(top!=NULL)
{
postfix[j++]=pop();
}
postfix[j] = '\0';
return postfix;
}
char pop()
{
struct Node* t;
char x=1;
if (top==NULL)
{
printf("Stack is Empthy")
}
else
{
t= top;
top = top->next;
x = t->data;
free(t);
}
return x;
}
int pre(char x)
{
if(x=='+' || x=='-')
retrun 1;
else if(x=='*' || x=='/')
return 2;
return 0;
}
int main()
{
char* infix="a+b*c-d/e";
push('#');
char* postfix = InToPost(infix);
printf("%s", postfix);
return 0;
}
Infix to postfix with Associativity and parenthesis
- Associativity
- Unary(1진법) Operation
#include <iostream>
#include<cstring>
#include <stack>
using namespace std;
int isOperand(char x)
{
if (x == '+' || x == '-' || x == '*' || x == '/' ||
x == '^' || x == '(' || x == ')'){
return 0;
}
return 1;
}
int outPrecedence(char x)
{
if (x == '+' || x == '-'){
return 2;
} else if (x == '*' || x == '/'){
return 4;
} else if (x == '^'){
return 5;
} else if (x == '('){
return 0;
}
return -1;
}
int inPrecedence(char x){
if (x == '+' || x == '-'){
return 2;
} else if (x == '*' || x == '/'){
return 4;
} else if (x == '^'){
return 5;
} else if (x == '('){
return 0;
}
return -1;
}
char* convert(char* infix)
{
char* postfix = new char[strlen(infix)+1];
stack<char> stk;
int i = 0;
int j = 0;
while(infix[i]!='\0')
{
if(isOperand(infix[i]))
{
postfix[j++] = infix[i++];
}
else
{
if(stk.empty() || outPrecedence(infix[i])>inPrecedence(stk.top()))
{
stk.push(infix[i++]);
}
else if (outPrecedence(infix[i]) == inPrecedence(stk.top()))
{
stk.pop();
}
else
{
postfix[j++] = stk.top();
stk.pop();
}
}
}
while(!stk.empty()&&stk.top()!=')')
{
postfix[j++] = stk.top();
stk.pop();
}
postfix[j] = '\0';
return postfix;
}
int main()
{
char infix[] = "((a+b)* c)-d^e^f";
cout << convert(infix) << endl;
return 0;
}
Evaluation of Post fix
#include <iostream>
#include<cstring>
#include <stack>
using namespace std;
int isOperand(char x)
{
if(x == '+' || x=='-'|| x=='*'|| x=='/')
{
return 0;
}
return 1;
}
int operation(char op, int x, int y)
{
if (op == '+'){
return x + y;
} else if (op == '-'){
return x - y;
} else if (op == '*'){
return x * y;
} else if (op == '/'){
return x / y;
}
}
int Evaluate(char* postfix)
{
stack<char> stk;
int x, y, result;
for(int i =0; postfix[i]!='\0';i++)
{
if(isOperand(postfix[i]))
{
// int typecast would not work because of char so subtract '0'
stk.push(postfix[i]-'0');
}
else
{
y=stk.top();
stk.pop();
x=stk.top();
stk.pop();
result = operation(postfix[i], x,y);
stk.push(result);
}
}
result = stk.top();
stk.pop();
return result;
}
int main()
{
char postfix[] = "35*62/+4-";
cout << Evaluate(postfix) << endl;
cout << postfixEvaluate(postfix) << endl
}
Comments